Nginx配置说明

2021/6/23 Nginx

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log crit;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 100000;

events {
    worker_connections  1024;

    multi_accept on;
}


http {

    server_tokens on;


    include       /etc/nginx/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  /var/log/nginx/access.log;

#access_log off;


    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  30;


    #gzip  on;
    #gzip_disable "msie6";
    #gzip_min_length 1k;
    #gzip_buffers 16 64k;
    #gzip_http_version 1.1;
    #gzip_comp_level 6;
    #gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    #gzip_vary on;


    #设置用户保存各种key的共享内存的参数,5m指的是5兆
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    #为给定的key设置最大的连接数,这里的key是addr,设定的值是100,就是说允许每一个IP地址最多同时打开100个连接
    limit_conn addr 100;


    #include指在当前文件中包含另一个文件内容
    
    #include mime.types;
    
    #设置文件使用默认的mine-type
    default_type text/html;
    #设置默认字符集
    charset UTF-8;


    #==设置nginx采用gzip压缩的形式发送数据,减少发送数据量,但会增加请求处理时间及CPU处理时间,需要权衡
    gzip on;

    #==加vary给代理服务器使用,针对有的浏览器支持压缩,有个不支持,根据客户端的HTTP头来判断是否需要压缩
    gzip_vary on;
    #nginx在压缩资源之前,先查找是否有预先gzip处理过的资源
    #!gzip_static on; #不知是否版本问题提示无此关键字
    #为指定的客户端禁用gzip功能
    gzip_disable "MSIE[1-6]\.";
    #允许或禁止压缩基于请求和相应的响应流,any代表压缩所有请求
    gzip_proxied any;
    #==设置对数据启用压缩的最少字节数,如果请求小于10240字节则不压缩,会影响请求速度
    gzip_min_length 10240;
    #==设置数据压缩等级,1-9之间,9最慢压缩比最大
    gzip_comp_level 2;
    #设置需要压缩的数据格式
    gzip_types text/plain text/css text/xml text/javascript  application/json application/x-javascript application/xml application/xml+rss;

    #==开发缓存的同时也指定了缓存文件的最大数量,20s如果文件没有请求则删除缓存
    open_file_cache max=100000 inactive=20s;
    #==指多长时间检查一次缓存的有效信息
    open_file_cache_valid 60s;
    #==文件缓存最小的访问次数,只有访问超过5次的才会被缓存
    open_file_cache_min_uses 5;
    #当搜索一个文件时是否缓存错误信息
    
    #==允许客户端请求的最大单文件字节数
    client_max_body_size 300m;

    #==冲区代理缓冲用户端请求的最大字节数
    client_header_buffer_size 32k;

    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;
    
    proxy_set_header Host $host;
    #==nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_connect_timeout 600;
    #==连接成功后,后端服务器响应时间(代理接收超时)
    proxy_read_timeout 600;
    #==后端服务器数据回传时间(代理发送超时)
    proxy_send_timeout 600;
    #==设置代理服务器(nginx)保存用户头信息的缓冲区大小
    #proxy_buffer_size 128k;
    #==设定缓存文件夹大小,大于这个值,将从upstream服务器传
    proxy_temp_file_write_size 300k;
    #==1G内存缓冲空间,3天不用删除,最大磁盘缓冲空间2G
    #proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:256m inactive=3d max_size=512g;


    open_file_cache_errors on;
    
    #配置共享会话缓存大小,视站点访问情况设定
    ssl_session_cache   shared:SSL:10m;
    #配置会话超时时间
    ssl_session_timeout 10m;

    include /etc/nginx/conf.d/*.conf;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128