Installing and deploying Nginx under Windows

Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. It is a lightweight Web server that can be deployed as an independent server (similar to Tomcat). Its advantage lies in its high performance and low memory consumption structure.

download

  1. Download it first and go directly to the official website nginx.org
  2. Download windows version

Installation deployment

  1. After downloading, unzip and run cmd in the unzipped folder
    Note: you must use the command to operate. Do not double-click nginx exe. If you double-click nginx Exe, which will lead to invalid restart and stop of nginx after modifying the configuration. You need to manually close all nginx processes in the task manager and restart

  2. Add nginx to environment variable

  3. It is normal for nginx service to flash when starting

    start nginx
    
  4. Check whether the task process exists, dos or open the task manager

    tasklist /fi "imagename eq nginx.exe"
    


    If there is no error, it may be an error at startup. Check the log in the logs folder in the nginx directory, error Log is a log file
    Common errors:
    (1) Port number occupied
    (2)nginx folder path contains Chinese
    For other errors, see the description in the log

  5. Modify the configuration file, enter the decompression directory, and click directly into the folder. There is no need to operate from dos

  6. Find nginx.exe in the conf directory Open conf, find the server node and modify the port number. If necessary, you can modify the home page directory. If not, you don't need to modify it

    #user  nobody;
    #==The number of work processes is generally set as the number of cpu cores
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        #==The maximum number of connections is generally set to cpu*2048
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        #==Client link timeout
        keepalive_timeout  65;
    
        #gzip  on;
    
        #When multiple server nodes are configured, the cache size of the default server names is not enough. You need to manually set it larger
        server_names_hash_bucket_size 512;
    
        #Server means that the virtual host can be understood as one site. Multiple server nodes can be configured to build multiple sites
        #Each request comes in to determine which server to use_ Name ok
        server {
            #Site listening port
            listen       8880;
    
            #Site access domain name
            server_name  localhost;
    
            #Encoding format to avoid random code of url parameters
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
            #location is used to match the access rules of multiple URI s under the same domain name
            #For example, how to jump dynamic resources and how to jump static resources
            #location followed by / represents the matching rule
            location / {
                #The site root directory can be a relative path or an absolute path
                root   E:/tomcat/sp;
    
                #Default home page
                index  index.html index.htm;
    
                #Forwarding the back-end site address is generally used for soft load and polling the back-end server
                #proxy_pass http://10.11.12.237:8080;
    
                #Reject the request and return 403, which is generally used to prohibit access to some directories
                #deny all;
                
                #Allow request
                #allow all;
    
                # add_header 'Access-Control-Allow-Origin' '*';
                # add_header 'Access-Control-Allow-Credentials' 'true';
                # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                # add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    
                #Redefine or add the request header sent to the back-end server
                #Add the client request hostname to the request header
                proxy_set_header Host $host;
    
                #Add client IP to request header
                proxy_set_header X-Real-IP $remote_addr;
    
                #Add $remote_ The addr variable value is added after the client "X-Forwarded-For" request header and separated by commas. If the client request does not carry the "X-Forwarded-For" request header, $proxy_ add_ x_ forwarded_ The value of the for variable will be the same as $remote_ The addr variable is the same  
                # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
                #Add the client's Cookie to the request header
                # proxy_set_header Cookie $http_cookie;
    
                #The primary domain name and port number of the proxy server will be used instead. If the port is 80, it can not be added.
                # proxy_redirect off;
    
                
                #The browser has many restrictions on cookies. If the Domain part of the Cookie does not match the Domain of the current page, it cannot be written.
                #Therefore, if A domain name is requested, the server proxy_pass to the B domain name, and then the B server outputs A Cookie with Domian=B,
                #The front page still stays on the A domain name, so the browser cannot write the Cookie.
    
                #Not only the domain name, but also the browser has restrictions on Path. We often use proxy_pass to a Path of the target server,
                #Do not expose this Path to the browser. At this time, the target server cannot write the Cookie.
                
                #Set the replacement text of the domain property in the "set cookie" response header. Its value can be a string, the pattern of a regular expression, or a referenced variable
                #If the forwarding back-end server needs cookies, it needs to convert the cookie domain, otherwise the front-end domain name is inconsistent with the back-end domain name, and the cookies will not be accessible
                #Configuration rule: proxy_ cookie_ Domain serverdomain (backend server domain) nginxDomain(nginx server domain)
                # proxy_cookie_domain localhost .testcaigou800.com;
                
                #Cancels all proxies at the current configuration level_ cookie_ Domain directive
                #proxy_cookie_domain off;
                #Timeout for establishing a connection with the back-end server. Generally, it is impossible to exceed 75 seconds;
                # proxy_connect_timeout 30;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            # error_page   500 502 503 504  /50x.html;
            # location = /50x.html {
            #     root   html;
            # }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
        # HTTPS server
        server {
            listen       8888 ssl;
            server_name  localhost;
    
            ssl_certificate      cert.pem;
            ssl_certificate_key  cert.key;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
        }
    }
    
  7. Save after modification. Use the following command to check whether the configuration file is correct, followed by nginx The relative path of the conf file. successful indicates that it is correct

    nginx -t -c conf/nginx.conf
    

    -t: check the configuration file for syntax errors
    -c filename: set the configuration file

  8. If the program is not started, start nginx directly. If it has been started, use the following command to reload the configuration file and restart

    nginx -s reload
    
  9. After that, open the browser to access the domain name and port just now. The welcome page shows that the deployment is successful

  10. Close nginx

    #Quick stop
    nginx -s stop
    

    or

    #Complete and orderly shutdown
    nginx -s quit
    

Keywords: Nginx

Added by happyneil on Mon, 03 Jan 2022 04:30:42 +0200