Nginx nginx.conf configuration file

  • 2020-05-07 20:57:50
  • OfStack


 # Run the user
    user www-data;   
    # Start the process , Usually set to and cpu Is equal to
    worker_processes  1;
 
    # Global error log and PID file
    error_log  /var/log/nginx/error.log;
    pid        /var/run/nginx.pid;
 
    # Working mode and connection limit
    events {
        use   epoll;             #epoll It's multiplexing IO(I/O Multiplexing) In the 1 Kind of way , But only for linux2.6 Above the kernel , Can be greatly improved nginx The performance of the
        worker_connections  1024;# A single background worker process Maximum number of concurrent links for a process
        # multi_accept on;
    }
 
    # set http Server, which provides load balancing support with its reverse proxy function
    http {
         # set mime type , Type by mime.type File defines
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        # Set log format
        access_log    /var/log/nginx/access.log;
 
        #sendfile Directive specifies nginx Whether to call sendfile Function ( zero copy To output the file. For normal applications,
        # Must be set to on, If used to download applications such as disk IO Heavy duty application, can be set to off To balance disk and network I/O Processing speed, slow down the system uptime.
        sendfile        on;
        #tcp_nopush     on;
 
        # Connection timeout
        #keepalive_timeout  0;
        keepalive_timeout  65;
        tcp_nodelay        on;
       
        # open gzip The compression
        gzip  on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 
        # Set request buffer
        client_header_buffer_size    1k;
        large_client_header_buffers  4 4k;
 
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
 
        # Set the list of load balancing servers
         upstream mysvr {
        #weigth The parameter represents the weight, and the higher the weight, the greater the probability of being assigned
        # This machine Squid open 3128 port
        server 192.168.8.1:3128 weight=5;
        server 192.168.8.2:80  weight=1;
        server 192.168.8.3:80  weight=6;
        }
 
 
       server {
        # To listen to 80 port
            listen       80;
            # Define the use www.xx.com access
            server_name  www.xx.com;
 
            # Set the access log of the virtual host
            access_log  logs/www.xx.com.access.log  main;
 
        # The default request
        location / {
              root   /root;      # Defines the default site root directory location for the server
              index index.php index.html index.htm;   # Defines the name of the home page index file
 
              fastcgi_pass  www.xx.com;
             fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
              include /etc/nginx/fastcgi_params;
            }
 
        # Define the error page
        error_page   500 502 503 504 /50x.html; 
            location = /50x.html {
            root   /root;
        }
 
        # Static files, nginx Take care of
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            root /var/www/virtual/htdocs;
            # overdue 30 Day, static file is not much updated, expired can be set big 1 Click, you can make it small if it is updated frequently 1 Points.
            expires 30d;
        }
        #PHP The script requests all forwards to FastCGI To deal with . use FastCGI The default configuration .
        location ~ \.php {
            root /root;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
            include fastcgi_params;
        }
        # Set the view Nginx Address of status
        location /NginxStatus {
            stub_status            on;
            access_log              on;
            auth_basic              "NginxStatus";
            auth_basic_user_file  conf/htpasswd;
        }
        # Blocking access .htxxx file
        location ~ /\.ht {
            deny all;
        }
        
         }
    }
 
The above is 1 Some basic configuration , use Nginx The biggest benefit is load balancing
 
If you want to use load balancing , You can modify the configuration http The nodes are as follows:
 
    # set http Server, which provides load balancing support with its reverse proxy function
    http {
         # set mime type , Type by mime.type File defines
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        # Set log format
        access_log    /var/log/nginx/access.log;
 
        # Omit the above 1 Some configuration nodes
 
        # .
 
        # Set the list of load balancing servers
         upstream mysvr {
        #weigth The parameter represents the weight, and the higher the weight, the greater the probability of being assigned
        server 192.168.8.1x:3128 weight=5;# This machine Squid open 3128 port
        server 192.168.8.2x:80  weight=1;
        server 192.168.8.3x:80  weight=6;
        }
 
       upstream mysvr2 {
        #weigth The parameter represents the weight, and the higher the weight, the greater the probability of being assigned
 
        server 192.168.8.x:80  weight=1;
        server 192.168.8.x:80  weight=6;
        }
 
       # The first 1 2 virtual servers
       server {
        # To listen to 192.168.8.x the 80 port
            listen       80;
            server_name  192.168.8.x;
 
          # right aspx Suffix for load balancing requests
        location ~ .*\.aspx$ {
 
             root   /root;      # Defines the default site root directory location for the server
              index index.php index.html index.htm;   # Defines the name of the home page index file
 
              proxy_pass  http://mysvr ;# The request to mysvr List of defined servers
 
              # The following is a 1 These reverse proxy configurations can be removed .
 
              proxy_redirect off;
 
              # The back end Web The server can go through X-Forwarded-For Get user real IP
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              client_max_body_size 10m;    # Maximum number of bytes per file allowed for client requests
              client_body_buffer_size 128k;  # The buffer agent buffers the maximum number of bytes requested by the client,
              proxy_connect_timeout 90;  #nginx Connection timeout with backend server ( Proxy connection timeout )
              proxy_send_timeout 90;        # Backend server data return time ( Agent send timeout )
              proxy_read_timeout 90;         # Backend server response time after successful connection ( Agent receive timeout )
              proxy_buffer_size 4k;             # Set up the proxy server ( nginx ) the buffer size to hold the user's header information
              proxy_buffers 4 32k;               #proxy_buffers Buffer pages in average 32k Set this as follows
              proxy_busy_buffers_size 64k;    # Buffer size under high load ( proxy_buffers*2 )
              proxy_temp_file_write_size 64k;  # Set the cache folder size, greater than this value, from upstream The server transfer
 
           }
 
         }
    }


Related articles: