1. Use Nginx proxy static resources

Purely static HTML projects, such as a compiled Vue project, can be brokered through a simple Nginx configuration.

Add the configuration file: / etc/nginx/conf. D/default. Conf

server {
    listen 80;
    # 指定访问的域名
    server_name www.abc.com;
    # 指定项目根路径
    root /var/www;
    # 指定首页文件(可选)
    index index.html;
}
Copy the code

More importantly, you can enable Gzip compression to optimize the transfer rate; The location parameter can be used to set access to different paths:

server { listen 80; Server_name *.com *.cn *.com * net; # root/var/www; # specify home file (optional) index index. HTML; # enable compression, optional gzip on; Gzip_min_length 1k; Gzip_comp_level 6; gzip_comp_level 6; Gzip_types text/plain Application /javascript application/ X-javascript text/javascript text/ XML text/ CSS application/json; /static/test.png = /static/test.png = /static/test.png The actual file to be accessed is /var/www_static/static/test. PNG location /static/ {root /var/www_static; }}Copy the code

2. Use Nginx reverse proxy for back-end services

If you have a Node back-end service listening on port 8080, you can use the following Nginx configuration as a reverse proxy:

server { listen 80; server_name *.com *.cn *.com.cn *.net; location / { proxy_pass http://localhost:8080; }}Copy the code

After reloading the nginx configuration with the systemctl reload nginx command, you can access the program listening on port 8080 by domain name.

Usually, we need more parameter Settings, such as passing the client’S IP to the back-end service:

server { listen 80; Server_name *.com *.cn *.com * net; Proxy_set_header Host $Host; Proxy_set_header x-real-ip $remote_addr; Forwarded_forwarded_for ($forwarded_forwarded_for) {proxy_set_header x-Forwarded_for ($forwarded_FORWARded_for); Pass proxy_pass_request_headers on; Proxy_pass http://localhost:8080; }}Copy the code

The above configuration only supports proxy HTTP requests. If the service also contains websockets, add the following parameters:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
Copy the code

3. Configure HTTPS for Nginx

Ali Cloud, Tencent Cloud, etc., will provide free HTTPS certificate, download it, find the Nginx exclusive certificate, usually suffix “key” and “. Pem “two files.

Upload these two files to the server, such as /etc/nginx/ssl, and modify the configuration file as follows:

Server {# port 443 listen 443 SSL; server_name *.com *.cn *.com.cn *.net; # ssl_certificate/etc/nginx/ssl/www.abc.com.pem; specify the certificate file ssl_certificate_key /etc/nginx/ssl/www.abc.com.key; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass_request_headers on; proxy_pass http://localhost:8080; }}Copy the code

4. Use Nginx for simple load balancing

If you have two processes listening on port 8080 and port 8081, you can use the following configuration to let Nginx randomly assign requests to programs on these two ports:

Upstream my-Servers {server localhost:8080; server localhost:8081; } server { listen 80; server_name *.com *.cn *.com.cn *.net; Location / {# set the target of forwarding to proxy_pass http://my-servers; }}Copy the code

Practice environment: CentOS 8.3 Nginx 1.14.1