How to deploy H5 games to cloud servers?

Content outline:

  1. Download and configure nginx
  2. Upload the game build file to the cloud server
  3. Nginx sets up multiple virtual hosts through ports

Development environment:

Alibaba Cloud Server: Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-93-generic x86_64)

nginx: nginx/1.4.6 (Ubuntu)

WinSCP: 5.15.3

Detailed steps:

1. Download and configure nginx

Before we start, let’s briefly talk about what nginx is. Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server. It is issued under a BSD-like protocol. Its characteristics are Occupies less memory and has strong concurrency.

Reverse proxy is to use the proxy server to accept the connection request on the internet, then forward the request to the server on the internal network, and return the result from the server to the client requesting the connection on the internet. At this time, the proxy server is external Behave as a server.

Load balancing is actually to distribute traffic to multiple servers for execution to reduce the pressure on each server. Multiple servers complete work tasks together, thereby increasing data throughput, thereby expanding the bandwidth of network equipment and servers, and increasing throughput , Strengthen network data processing capabilities, improve network flexibility and availability.

Using nginx, we can achieve separation of dynamics and statics, put static resources that have not moved for ten thousand years in nginx, and dynamic resources run in the TomCat server. When accessing static resources, you can directly request nginx instead of requesting TomCat. The pressure on the server is less.

Currently, two installation methods are supported, one is based on APT source installation, the other is to compile and install through source package, but if you want to install the latest version, you must download the source package to compile and install. This article uses the APT source installation method. Those who want to know another installation method can Baidu by themselves.

1.1 Update software source

sudo apt- get update

1.2 Install nginx

sudo apt- get install nginx

Note: The installed file location:

/usr/sbin/nginx: main program

/etc/nginx: store configuration files

/usr/share/nginx: store static files

/var/log/nginx: store logs

1.3 Check whether nginx is installed successfully

nginx -v

1.4 Start nginx

service nginx start

1.5 After startup, enter the public IP of the server in the browser, and you can see the welcome page of nginx, and the nginx installation is successful.


2. Upload the game build file to the cloud server

2.1 Uploading files to the cloud server requires a tool: WinSCP. I have uploaded the software to Baidu Cloud. You can get it by replying to "WinSCP" in the background of the official account, and you can install it without brain operation.

2.2 Before uploading files, you need to create a folder in the cloud server to place the game build files for a while, because I will place two game build files later, so I created two subfolders. The directories are as follows ( Created in the root directory, you can create it yourself according to the actual situation.):



2.3 After creating the folder, you can use WinSCP to upload the game build files. Prepare two built games, select all the build files and right-click to upload to the directory created above:


3.nginx sets up multiple virtual hosts through ports

Before we start, we also briefly explain the configuration file of nginx:

1  ... #global block
3  events {#events block
 4     ...
 5  }
7  http #http block
 8  {
 9      ... #http global block
 10      server #server block
 11      { 
 12          ... #server global Block
 13          location [PATTERN] #location Block
 14          {
 15              ...
 16          }
 17          location [PATTERN] 
 18          {
 19              ...
 20          }
 21     }
 22      server
 23      {
 24        ...
 25      }
 26      ... #httpglobal block
 27 }

Global block : configure the directives that affect the global nginx. Generally, there are user groups running nginx server, nginx process pid storage path, log storage path, configuration file introduction, number of worker processes allowed, etc.

events block : The configuration affects the nginx server or the network connection with the user. There is a maximum number of connections for each process, which event-driven model is selected to process connection requests, whether to allow multiple network connections to be accepted at the same time, and enable multiple network connection serialization.

http block : You can nest multiple servers, configure proxy, cache, log definition and most of the functions and configuration of third-party modules. Such as file import, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout period, number of single connection requests, etc.

Server block : Configure the relevant parameters of the virtual host. There can be multiple servers in one http.

Location block : Configure the routing of the request and the processing of various pages.

Here is a configuration file for everyone, as an understanding:

1  ########### Each instruction must end with a semicolon. #################
 2  #user administrator administrators; #Configure users or groups, the default is nobody nobody.
3 #worker_processes 2 ; #The number of processes allowed to be generated, the default is 1
 4 #pid/nginx/pid/; #Specify the storage address of the nginx process running file 5 error_log log/error.log debug; #Specify the
 log path, level . This setting can be put into the global block, http block, server block, and the level is: debug|info|notice|warn|error|crit|alert|emerg
 6  events {
 7      accept_mutex on; #Set the network connection serialization to prevent Shock group phenomenon occurs, the default is on
 8      multi_accept on; #Set whether a process accepts multiple network connections at the same time, the default is off
 9      #use epoll; #Event-driven model, select|poll|kqueue|epoll|resig|/dev/poll| eventport
 10      worker_connections 1024; #Maximum number of connections, the default is 512
 11  }
 12  http {
 13      include mime.types; #File extension and file type mapping table
 14      default_type application/octet-stream;     #The default file type, the default is text/plain
 15  #access_log off;      #Cancel service log    
 16 log_format myFormat'$remote_addr--$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for' ; #Custom format
 17      access_log log/access.log myFormat; #combined as log The default value of the format is
 18      sendfile on; #Allow sendfile to transfer files, the default is off, which can be in the http block, server block, and location block.
19      sendfile_max_chunk 100k; #The number of transfers per call of each process cannot be greater than the set value, the default is 0, that is, there is no upper limit.
20     keepalive_timeout 65 ; #Connection timeout time, the default is 75s, it can be in http, server, location block.
22      upstream mysvr {   
 23        server ;
 24        server backup; #热备
25      }
 26      error_page 404 https:;
 #error page 27      server {
 28          keepalive_requests 120 ; #Maximum number of single connection requests.
29          listen 4545 ; #Listening port
 30          server_name ; #Listening address       
 31          location ~*^.+$ {#Request URL filtering, regular matching, ~ is case sensitive, ~* is case insensitive.
32             #root path; #根directory
 33             #index vv.txt; #Set the default page
 34             proxy_pass http: mysvr; #Request to go to the server list defined by mysvr 
35             deny ;             #Rejected ip
 36 allow ; #Allowed ip           
 37          } 
 38      }
 39 }

Using virtual host technology, a real host can be divided into many "virtual" hosts. Each virtual host has an independent domain name and IP address, and has a complete Internet server (www, FTP, email) function. Virtual hosts are completely independent. From the outside world, each virtual host is exactly the same as an independent host.

There are three types of virtual hosts: IP-based virtual hosts, port-based virtual hosts and name-based virtual hosts. This article uses port-based setting of multiple virtual hosts. Those who want to know the other two setting methods can do it themselves Baidu.

3.1 Ports 80 and 81 are released in this article. Port 80 is the default port. Before starting, you should open port 81 on the Alibaba Cloud server first:





3.2 After the Alibaba Cloud server is configured, you can remotely log in to the server to see if the port is successfully opened. If the port is not detected, you need to manually open it:

View status:

iptables -L -n


If there is no port 81, you need to open port 81:

Open the port:

iptables -I INPUT -p tcp --dport 81 -j ACCEPT

Close the port:

iptables -D INPUT -p tcp --dport 81 -j ACCEPT

3.3 After opening the port, you need to configure the nginx.conf file after adding it. The nginx.conf configuration file has been briefly introduced above. If you want to set up multiple virtual hosts through the port, you only need to add a server to the newly opened port Just listen:

1      server {
 2          listen 80;     Monitor port 80 
3          server_name;
 4          root/www/80;     Project directory 
6          location/{
 7              index index.html index.htm;
 8          }
10          error_page 500 502 503 504/50x.html;
 11          location =/50x.html {
 12              root html;
 13          }   
 14      }
16      server {
 17         listen 81;     Monitor port 81 
18          server_name;
 19          root/www/81;     Project directory 
21          location/{
 22              index index.html index.htm;
 23          }
25          error_page 500 502 503 504/50x.html;
 26          location =/50x.html {
 27              root html;
 28          }        
 29      }

3.4 After configuring the nginx.conf file, restart nginx to view the effect:

Enter the following command to update the configuration file without closing nginx:

nginx -s reload

3.5 Enter http://public network:port in the editor, you can see the effect as follows:



At last:

So far, all the steps for deploying H5 games to cloud servers have been introduced. Hurry up and share your games with friends for a try!

Reprinted at:

No comments

Related recommendation

No related articles!


How to deploy H5 games to cloud servers?