Getting started with nginx

installation

http://nginx.org/en/download.html

Download and unzip

Directory Structure

  1. Conf: configuration file
  2. html: static files
  3. logs: log files

Common commands

Open a command line window (cmd under the window), enter the installation directory and execute the command

  1. Graceful exit: nginx -s quit
  2. Force exit: nginx -s stop
  3. Restart: nginx -s reopen
  4. Start: start nginx.exe

Configuration case

Scenario 1: Set up proxy

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        /***这里是配置内容-start***/
        location /api/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass   http://165.168.0.70:8000;
            proxy_redirect default ;
        }
        /***这里是配置内容-end***/

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

Scenario 2: Jump to the homepage if the address is not accessible

 server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri /index.html; //这里是配置内容
        }

        location /api/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass   http://165.168.0.70:8000;
            proxy_redirect default ;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

Other references

https://www.nginx.cn/doc/

Guess you like

Origin blog.csdn.net/baidu_38798835/article/details/111869821