The nginx knowledge that the front end should master

Keywords: Nginx

preface

Why understand nginx?

  • First, improve your server deployment capabilities
  • Secondly, it is helpful to understand the back-end interface link

What can ngix do?

  1. Solve cross domain problems
  2. load balancing
  3. Static server
  4. Multi / single page website
  5. gzip

text

Installation & common commands & nginx configuration file structure

Installation (taking ubuntu as an example):

$ sudo apt-get install nginx

For more information: Installing NGINX

View version:

$ sudo nginx -v
# The version information indicates that the installation was successful
nginx version: nginx/1.6.2

Common commands

Start service:

$ nginx

Other commands:

$ nginx -s <SIGNAL>

SIGNAL:

  • quit – normal shutdown service
  • Reload – reload the configuration file to run
  • reopen – opens the log file
  • stop – shut down the service immediately

configuration file

file structure

The main configuration file of nginx is nginx.conf. You can include configuration files in other locations in the main configuration file.
From the above installation:

  • Default configuration file path / etc/nginx/nginx.conf
  • There may be references in this file, such as include /etc/nginx/conf.d/*.conf; In fact, your project configuration files are all. Conf files in the / etc/nginx/conf.d/ folder;
  • Generally, a project (domain name) is equipped with a file. For example, if your domain name is www.baidu.com, your configuration file can be called / etc/nginx/conf.d/www.baidu.com.conf
Configuration description


Main: the global configuration of nginx, which takes effect globally.
events: the configuration affects the nginx server or the network connection with the user.
http: you can nest multiple server s, configure most functions such as proxy, cache and log definition, and configure third-party modules.
server: configure the relevant parameters of the virtual host. There can be multiple servers in one http.
location: configure the routing of requests and the processing of various pages.
upstream: configuring the specific address of the back-end server is an indispensable part of load balancing configuration.

Solve cross domain

Http load balancing

Load balancing strategy: to be supplemented
Configure upstream:

upstream balanceServer {
    server 10.1.22.33:12345;
    server 10.1.22.34:12345;
    server 10.1.22.35:12345;
}

Configure server:

 server {
        server_name  fe.server.com;
        listen 80;
        location /api {
            proxy_pass http://balanceServer;
        }
    }

Static server

/data/static / provides directory browsing:

server{
  listen 80 default_server; 
  server_name www.example.com;

  location ^~ /static {
    root /data/static/; # Set the file directory under the access server
    autoindex on; # Open directory browsing
    access_log  off; # Close access log
    charset utf-8,gbk;     #Prevent Chinese directories from being garbled
    expires     10h;# Set the expiration time to 10 hours
  }       
}

View more: [nginx open directory browsing function and theme beautification
](https://ld246.com/article/156...)

Single page website

 server {
        server_name  fe.server.com;
        listen 80;
        location / {
            root /data/www;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }
  • root
  • index
  • try_files

Multi page website

 server {
        server_name  fe.server.com;
        listen 80;
        location /app {
                    
        }
        location /pc {
                    
        }
        location /api {
                    
        }
        location / {
            root /data/www;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }

Gzip

use Gzip realization HTTP Compression , can improve transmission speed and bandwidth utilization.

    gzip                    on; 
    gzip_http_version       1.1;        
    gzip_comp_level         5;
    gzip_min_length         1000;
    gzip_types text/csv text/xml text/css text/plain text/javascript application/javascript application/x-javascript application/json application/xml;
  • Gzip: determines whether to start the gzip module. On means on and off means off;
  • gzip_http_version: identify the version of HTTP protocol. The early browser may not support gzip self decompression, and users will see garbled code
  • gzip_comp_level: set the gzip compression level. The lower the level is, the faster the compression speed is, the smaller the file compression ratio is, on the contrary, the slower the speed is, the larger the file compression ratio is; level 1-9, the smallest compression is the fastest but consumes cpu
  • gzip_min_length: set the minimum byte of the page allowed to be compressed (obtained from the content length of the header header). gzip will be used for compression only when the returned content is greater than this value. The unit is K. when the value is 0, all pages will be compressed. It is recommended to be greater than 1k
  • gzip_types: set the MIME type to be compressed. If it is not set, it will not be compressed, that is, it matches the compression type

Deploy https

https://cloud.tencent.com/doc...
https on port 443
1.ssl_certificate: crt certificate file and 2.ssl_certificate_key: key private key file are required. Put them in nginx folder
Create a new ssl.conf file in the conf.d folder

# Take cloud.tencent.com deployment as an example
# Certificate: 1_cloud.tencent.com_bundle.crt
# Private key: 2_cloud.tencent.com.key
server {
        #The SSL access port number is 443
        listen 443 ssl; 
        #Fill in the domain name of the binding certificate
        server_name cloud.tencent.com; 
        #Certificate file name
        ssl_certificate 1_cloud.tencent.com_bundle.crt; 
        #Private key file name
        ssl_certificate_key 2_cloud.tencent.com.key; 
        ssl_session_timeout 5m;
        
        #Please configure according to the following protocol
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        #Please configure the encryption suite according to the following suite configuration. The writing method follows the openssl standard.
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;
        
        location / {
           #Home page path of the website. This path is for reference only. Please follow the actual directory for details.
           #For example, if your website running directory is under / etc/www, fill in / etc/www.
            root html; 
            # There is no need to modify here
            index  index.html index.htm;
        }
    }

http redirect to https

http on port 80
http:// cloud.tencent.com -> https:cloud.tencent.com
server {

listen 80;
#Fill in the domain name of the binding certificate
server_name cloud.tencent.com; 
#Convert the domain name request from http to https
return 301 https://$host$request_uri; 

}

  1. nginx built in variable
    $host:
    $request_uri: remove the rest of the first $host from the full url
  2. 301 jump

Reference articles

Posted by cavey5 on Thu, 25 Nov 2021 14:28:20 -0800