01nginx simple configuration details

Time:2021-1-17

1. Nginx command line parameters

-C < / path / to / config > specifies a configuration file for nginx instead of the default.

-T does not run, just tests the configuration file. Nginx will check the syntax of the configuration file for correctness and try to open the file referenced in the configuration file.

-V shows the version of nginx.

-V shows the version, compiler version and configuration parameters of nginx.

Detailed explanation of nginx configuration file

#Running user
user nobody;
#Start the process, usually set to equal the number of CPUs
worker_processes  1;

#Global error log and PID file
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#Working mode and connection limit
events {
    #Epoll is a way of multiplexing IO (I / O multiplexing),
    #Only used for Linux 2.6 or above kernel, can greatly improve the performance of nginx
    use   epoll; 

    #The maximum number of concurrent links of a single background worker process    
    worker_connections  1024;

    #The total concurrency is worker_ Processes and workers_ The product of connections
    #That is max_ clients = worker_ processes * worker_ connections
    #When the reverse proxy is set, max_ clients = worker_ processes * worker_ Connections / 4 why
    #Why should the above reverse proxy be divided by 4? It should be said that it is an empirical value
    #According to the above conditions, under normal circumstances, the maximum number of connections that nginx server can handle is: 4 * 8000 = 32000
    # worker_ The setting of connections value is related to the size of physical memory
    #Because concurrency is Io constrained, max_ The value of clients must be less than the maximum number of files that the system can open
    #The maximum number of files that the system can open is directly proportional to the size of memory. Generally, the number of files that can be opened on a machine with 1GB memory is about 100000
    #Let's take a look at the number of file handles that can be opened by VPS with 360m memory
    # $ cat /proc/sys/fs/file-max
    #Output 34336
    #32000 < 34336, that is, the total number of concurrent connections is less than the total number of file handles that the system can open, which is within the range that the operating system can bear
    #So, worker_ The value of connections should be based on the worker_ The number of processes and the maximum number of files that the system can open are set appropriately
    #Make the concurrency total less than the maximum number of files that the operating system can open
    #Its essence is to configure according to the physical CPU and memory of the host
    #Of course, the total number of concurrency in theory may deviate from the actual, because the host has other work processes that need to consume system resources.
    # ulimit -SHn 65535

}


http {
    #Set the MIME type, which is defined by mime.type Document definition
    include    mime.types;
    default_type  application/octet-stream;
    #Set log format
    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;

    #The sendfile instruction specifies whether nginx calls the sendfile function (zero copy mode) to output the file,
    #For normal applications, it must be set to on,
    #If it is used for download and other applications, disk IO heavy load applications can be set to off,
    #To balance the disk and network I / O processing speed, reduce the uptime of the system
    sendfile     on;
    #tcp_nopush     on;

    #Connection timeout
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;

    #Enable gzip compression
    gzip  on;
    gzip_disable "MSIE [1-6].";

    #Set request buffer
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;


    #Setting virtual host configuration
    server {
        #Listen on port 80
        listen    80;
        #Define usage www.nginx.cn visit
        server_name  www.nginx.cn;

        #Defines the default site root location for the server
        root html;

        #Set the access log of this virtual host
        access_log  logs/nginx.access.log  main;

        #Default request
        location / {
            
            #Defines the name of the first page index file
            index index.php index.html index.htm;   

        }

        #Define error page
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #Static files are handled by nginx itself
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #After 30 days of expiration, the static file is not updated very much, and the expiration can be set larger,
            #If it is updated frequently, it can be set smaller.
            expires 30d;
        }

        #All PHP script requests are forwarded to fastcgi for processing. Fastcgi default configuration is used
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        #Access to the. Htxxx file is prohibited
            location ~ /.ht {
            deny all;
        }

    }
}

Recommended Today

Master git thoroughly (2)

1、 Transmission Mastering git thoroughly (1)Master git thoroughly (3) 2、 Basic concepts ① Work areaThe so-called work area isThe directory you can see on your computerCan be simply understood as our project root directory is the workspace.② Version LibraryThe so-called version library is executed in the root directory of the projectgit initAfter the command, a. […]