1. Install

The installation command is:

yum install -y nginx

Check whether the installation is successful

nginx -v

2. Query the nginx installation directory

rpm -ql nginx

3. Basic nginx.conf file interpretation

    # run user, default is nginx, can not set
    user  nginx;
    #Nginx process, generally set to the same number of CPU cores
    worker_processes  1;   
    # Error log directory
    error_log  /var/log/nginx/error.log warn;
    # Process PID location
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024; # Maximum number of concurrent requests for a single background process
    }
    
    
    http {
        include       /etc/nginx/mime.types;   File extension and type mapping table
        default_type  application/octet-stream;  The default file type
        Set the logging mode
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;   #nginx access log location
    
        sendfile        on;   Enable efficient transmission mode
        #tcp_nopush on; # Reduce the number of network packet segments
    
        keepalive_timeout  65;  # Time to hold a connection, also known as timeout
    
        #gzip on; Enable gzip compression
    
        include /etc/nginx/conf.d/*.conf; # Contains the location and file of the child configuration item
        server {
            listen       80;   Configure the listening portserver_name localhost; // Configure the domain name#charset koi8-r;     
            #access_log /var/log/nginx/host.access.log main;
        
            location / {
                root   /usr/share/nginx/html;     Service default startup directory
                index  index.html index.htm;    Access files by default
            }
        
            #error_page 404 /404.html; # configure the 404 page
        
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;   Error status code display page, need to restart after configurationlocation = /50x.html { root /usr/share/nginx/html; }}}Copy the code

4. The nginx service starts, stops, and restarts

4.1 start

  • In centOS7.4, nginx is available directly

nginx

  • Using the Linux command systemctl(if required)

systemctl start nginx.service

4.2 Querying the Running Status of services

Ps aux | grep nginx or ps – ef | grep nginx

4.3 stop

  • Immediately stop

nginx -s stop

  • Easy to stop

nginx -s quit

  • Linux command

systemctl stop nginx.service

  • Kill (after the process number is queried from 4.2)

Kill + PROCESS ID

4.4 restart

nginx -s reload

5. Permission control

5.1 the IP rights

Note That the permission configuration depends on the writing location. In the first case, all IP addresses are prohibited. In the second case, only the first IP address is allowed to access

    location / {
        deny all;
        allow  11.11.11.11;
    }
Copy the code
Location / {allow 11.11.11.11 deny all}Copy the code

5.2 Folder Permissions

    location = /img {
        allow all
    }
    location = /admin {
        deny all
    }
Copy the code

5.3 Regular Matching

    location ~*\.php$ {
        deny all
    }
Copy the code

6. Configure cross-domain

    add_header Access-Control-Allow-Origin $http_origin;
    add_header Access-Control-Allow-Credentials true;
Copy the code

7. Configure history in vue

    location / {
        try_files $uri $uri/ /index.html
    }
Copy the code

Agent 8.

    location / {
        proxy_pass https://www.baidu.com
    }
Copy the code