Nginx optimization configuration

https://www.cnblogs.com/taiyonghai/p/9402734.html

nginx.conf file

#user nobody;

#==Number of work processes, generally set to 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 {

    #==Maximum number of connections, 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 connection timeout
    keepalive_timeout 65;

    #gzip on;

    #When configuring multiple server nodes, the buffer size of the default server names is not enough, you need to manually set a larger size
    server_names_hash_bucket_size 512;

    #server indicates that the virtual host can be understood as a site, and multiple server nodes can be configured to build multiple sites
    #Each request comes in to determine which server to use is determined by server_name
    server {
        #Site listening port
        listen 8800;
        #Site access domain name
        server_name localhost;
        
        #Encoding format to avoid garbled url parameters
        charset utf-8;

        #access_log logs/host.access.log main;

        #location is used to match the access rules of multiple URIs under the same domain name
        #For example, how to jump to dynamic resources, how to jump to static resources, etc.
        #location followed by / represents the matching rule
        location / {
            #Site root directory, it can be a relative path or an absolute path
            root html;
            #Default homepage
            index index.html index.htm;
            
            #Forwarding the back-end site address, generally used for soft load, polling the back-end server
            #proxy_pass http://10.11.12.237:8080;

            #Reject the request, return 403, generally used to prohibit access to certain 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 backend server
            #Add the client request hostname to the request header
            proxy_set_header Host $host;
            #Add the client IP to the request header
            proxy_set_header X-Real-IP $remote_addr;
            #Add the value of the $remote_addr variable to the back of the client "X-Forwarded-For" request header, separated by commas. If the client request does not carry the "X-Forwarded-For" request header, the value of the $proxy_add_x_forwarded_for variable will be the same as the $remote_addr variable
            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;
            # Will be replaced with the main domain name and port number of the proxy server. If the port is 80, you don't need to add it.
            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.
            #So if you request the A domain name, the server proxy_pass to the B domain name, and then the B server outputs the cookie with Domian=B,
            #The front-end page still stays on the A domain name, so the browser cannot write the cookie.
            
   #Not only is the domain name, the browser also has restrictions on Path. We often proxy_pass to a certain Path of the target server,
            #Do not expose this Path to the browser. At this time, if the target server's Cookie is hard to write to Path, there will also be a problem that the Cookie cannot be written.
            
            #Set the replacement text of the domain attribute in the "Set-Cookie" response header. Its value can be a string, a pattern of regular expressions, or a quoted variable
            #Forwarding the back-end server, if cookies are needed, the cookie domain needs to be converted, otherwise the front-end domain name and the back-end domain name are inconsistent with the cookie and the cookie cannot be accessed
       #Configuration rule: proxy_cookie_domain serverDomain (back-end server domain) nginxDomain (nginx server domain)
            proxy_cookie_domain localhost .testcaigou800.com;
            
            #Cancel all proxy_cookie_domain instructions at the current configuration level
            #proxy_cookie_domain off;
            #The timeout period for establishing a connection with the back-end server. It is generally impossible to be longer than 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;
        }

    }
    
  # When you need to monitor multiple domain names on the same port, use the following configuration. The port is the same and the domain name is different, and the server_name can also be configured using regular
  #But be aware that there are too many servers and you need to manually expand the server_names_hash_bucket_size buffer size
  Server {
     listen 80;
    Server_name www.abc.com;
    Charset utf-8;
    Location / {
      Proxy_pass http://localhost:10001;
    }
  }
  Server {
     listen 80;
    Server_name aaa.abc.com;
    Charset utf-8;
    Location / {
      Proxy_pass http://localhost:20002;
    }
  }
}

#user nobody;

#==Number of work processes, generally set to 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 {

    #==Maximum number of connections, 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 connection timeout
    keepalive_timeout 65;

    #gzip on;

    #When configuring multiple server nodes, the buffer size of the default server names is not enough, you need to manually set a larger size
    server_names_hash_bucket_size 512;

    #server indicates that the virtual host can be understood as a site, and multiple server nodes can be configured to build multiple sites
    #Each request comes in to determine which server to use is determined by server_name
    server {
        #Site listening port
        listen 8800;
        #Site access domain name
        server_name localhost;
        
        #Encoding format to avoid garbled url parameters
        charset utf-8;

        #access_log logs/host.access.log main;

        #location is used to match the access rules of multiple URIs under the same domain name
        #For example, how to jump to dynamic resources, how to jump to static resources, etc.
        #location followed by / represents the matching rule
        location / {
            #Site root directory, it can be a relative path or an absolute path
            root html;
            #Default homepage
            index index.html index.htm;
            
            #Forwarding the back-end site address, generally used for soft load, polling the back-end server
            #proxy_pass http://10.11.12.237:8080;

            #Reject the request, return 403, generally used to prohibit access to certain 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 backend server
            #Add the client request hostname to the request header
            proxy_set_header Host $host;
            #Add the client IP to the request header
            proxy_set_header X-Real-IP $remote_addr;
            #Add the value of the $remote_addr variable to the back of the client "X-Forwarded-For" request header, separated by commas. If the client request does not carry the "X-Forwarded-For" request header, the value of the $proxy_add_x_forwarded_for variable will be the same as the $remote_addr variable
            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;
            # Will be replaced with the main domain name and port number of the proxy server. If the port is 80, you don't need to add it.
            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.
            #So if you request A domain name, the server proxy_pass to B domain name, then B server outputs Domian=B cookie,
            #The front-end page still stays on the A domain name, so the browser cannot write the cookie.
            
   #Not only is the domain name, the browser also has restrictions on Path. We often proxy_pass to a certain Path of the target server,
            #Do not expose this Path to the browser. At this time, if the target server's Cookie is hard to write to Path, there will also be a problem that the Cookie cannot be written.
            
            #Set the replacement text of the domain attribute in the "Set-Cookie" response header. Its value can be a string, a pattern of regular expressions, or a quoted variable
            #Forwarding the back-end server, if cookies are needed, the cookie domain needs to be converted, otherwise the front-end domain name and the back-end domain name are inconsistent with the cookie and the cookie cannot be accessed
       #Configuration rule: proxy_cookie_domain serverDomain (back-end server domain) nginxDomain (nginx server domain)
            proxy_cookie_domain localhost .testcaigou800.com;
            
            #Cancel all proxy_cookie_domain instructions at the current configuration level
            #proxy_cookie_domain off;
            #The timeout period for establishing a connection with the back-end server. It is generally impossible to be longer than 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;
        }

    }
    
  #When you need to monitor multiple domain names on the same port, use the following configuration, the port is the same and the domain name is different, and the server_name can also be configured using regular
  #But be aware that there are too many servers and you need to manually expand the server_names_hash_bucket_size buffer size
  Server {
     listen 80;
    Server_name www.abc.com;
    Charset utf-8;
    Location / {
      Proxy_pass http://localhost:10001;
    }
  }
  Server {
     listen 80;
    Server_name aaa.abc.com;
    Charset utf-8;
    Location / {
      Proxy_pass http://localhost:20002;
    }
  }
}

Leave a Comment

Your email address will not be published.