Detailed nginx configuration and common configuration examples

Nginx configuration file structure

The content of the default nginx configuration file nginx.conf is as follows:

...              #全局块

events {
    
             #events块
   ...
}

http      #http块
{
    
    
    ...   #http全局块
    server        #server块
    {
    
     
        ...       #server全局块
        location [PATTERN]   #location块
        {
    
    
            ...
        }
        location [PATTERN] 
        {
    
    
            ...
        }
    }
    server
    {
    
    
      ...
    }
    
    upstream
    {
    
    
      ...
    }
    ...     #http全局块
}
  • Global block : configure the directives that affect the global nginx. Generally, there are user groups running nginx server, nginx process pid storage path, log storage path, configuration file introduction, number of worker processes allowed, etc.
    • events block : The configuration affects the nginx server or the network connection with the user. There is a maximum number of connections for each process, which event-driven model is selected to process connection requests, whether to allow multiple network connections to be accepted at the same time, and enable multiple network connection serialization.
    • http block : You can nest multiple servers, configure proxy, cache, log definition and most of the functions and configuration of third-party modules. Such as file import, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout period, number of single connection requests, etc.
      • Server block : Configure the relevant parameters of the virtual host. There can be multiple servers in one http.
      • Location block : Configure the routing of the request and the processing of various pages.
      • upstream block : configure load balancing

Detailed common configuration

Common commands

Restart Nginx

./nginx -s reload;

Test whether the Nginx configuration is wrong

 ./nginx -t;

Common plan configuration

Reverse proxy

server {
    
    
    listen 80;
    server_name www.nginx.cn nginx.cn;
 
    location /app {
    
    
       proxy_pass http://127.0.0.1:8080;
    }
}

Load balancing

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    
    

    #设定mime类型,类型由mime.type文件定义
    include             /etc/nginx/mime.types;
    default_type    application/octet-stream;

    #设定日志格式
    access_log        /var/log/nginx/access.log;

    #省略上文有的一些配置节点
    #。。。。。。。。。。

    #设定负载均衡的服务器列表
    upstream mysvr {
    
    
        #weigth参数表示权值,权值越高被分配到的几率越大
        server 192.168.8.1x:3128 weight=5;
        #本机上的Squid开启3128端口,不是必须要squid
        server 192.168.8.2x:80    weight=1;
        server 192.168.8.3x:80    weight=6;
    }
        
    upstream mysvr2 {
    
    
        #weigth参数表示权值,权值越高被分配到的几率越大
        server 192.168.8.x:80    weight=1;
        server 192.168.8.x:80    weight=6;
    }

    #第一个虚拟服务器
    server {
    
    
        #侦听192.168.8.x的80端口
        listen             80;
        server_name    192.168.8.x;

        #对aspx后缀的进行负载均衡请求
        location ~ .*.aspx$ {
    
    
            #定义服务器的默认网站根目录位置
            root     /root; 
            #定义首页索引文件的名称
            index index.php index.html index.htm;
            
            #请求转向mysvr 定义的服务器列表
            proxy_pass    http://mysvr ;

            #以下是一些反向代理的配置可删除.

            proxy_redirect off;

            #后端的Web服务器可以通过X-Forwarded-For获取用户真实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; 

            #缓冲区代理缓冲用户端请求的最大字节数,
            client_body_buffer_size 128k;

            #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_connect_timeout 90;

            #连接成功后,后端服务器响应时间(代理接收超时)
            proxy_read_timeout 90;

            #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffer_size 4k;

            #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
            proxy_buffers 4 32k;

            #高负荷下缓冲大小(proxy_buffers*2)
            proxy_busy_buffers_size 64k; 

            #设定缓存文件夹大小,大于这个值,将从upstream服务器传
            proxy_temp_file_write_size 64k;    

        }
    }
}

common problem

1. File upload, 413 Request Entity Too Large

  • Reason: uploading supports the largest file size of 1M by default
  • Solution: Configureclient_max_body_size
client_max_body_size 20M;#上传大小最大值

2. File download, upstream sent invalid chunked response while reading upstream error

  • Reason: The proxy_http_versiondefault is http1.0 version is not set , and it does not support long connections and block transmission.
  • Solution: Configureproxy_http_version 1.1
proxy_http_version 1.1

Guess you like

Origin blog.csdn.net/abu935009066/article/details/112463873