# 下载 nginx

最新版nginx下载地址 (opens new window)
最新版ngx_cache_purge下载地址 (opens new window)
最新版ngx_slowfs_cache下载地址 (opens new window)
最新版nginx_upstream_check_module下载地址 (opens new window)
最新版incubator-pagespeed-ngx下载地址 (opens new window)
如果需要流媒体则需下载nginx-rtmp-module:
最新版nginx-rtmp-module下载地址 (opens new window)

本例版本打包下载

# 解压 nginx

本例假设将安装文件下载到 /usr/app/soft 目录。

export soft_dir="/usr/app/soft"
cd $soft_dir
tar --no-same-owner -zxvf $soft_dir/ngx_cache_purge-2.3.tar.gz
tar --no-same-owner -zxvf $soft_dir/ngx_slowfs_cache-1.10.tar.gz
tar --no-same-owner -zxvf $soft_dir/nginx-1.20.1.tar.gz
tar --no-same-owner -zxvf $soft_dir/nginx_upstream_check_module-0.3.0.tar.gz
tar --no-same-owner -zxvf $soft_dir/incubator-pagespeed-ngx-1.13.35.2-stable.tar.gz 

cd $soft_dir/incubator-pagespeed-ngx-1.13.35.2-stable
#wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
tar -xzvf $soft_dir/1.13.35.2-x64.tar.gz 

#更改google-perftools的搜索安装路径:
cd $soft_dir/nginx-1.20.1
vi auto/lib/google-perftools/conf
#查找将:/usr/local/ 替换为:/usr/local/gperftools/
g/\/usr\/local\//s//\/usr\/local\/gperftools\//g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 安装 nginx

本例假设将安装文件下载到 /usr/app/soft 目录。
本例安装到 /usr/app/nginx 目录。

groupadd nginx 
useradd -g nginx nginx 

cd $soft_dir/nginx-1.20.1

./configure --prefix=/usr/app/nginx \
--with-threads \
--with-http_realip_module \
--with-http_addition_module \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--user=nginx --group=nginx \
--add-module=../ngx_cache_purge-2.3 \
--add-module=../ngx_slowfs_cache-1.10 \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-pcre \
--with-file-aio \
--add-module=../nginx_upstream_check_module-0.3.0 \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-google_perftools_module   \
--with-ipv6   \
--with-http_ssl_module   \
--with-stream

make -j4 && make install
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

# 更改 nginx 配置文件

mv /usr/app/nginx/conf/nginx.conf /usr/app/nginx/conf/nginx_old.conf
vi /usr/app/nginx/conf/nginx.conf
1
2
点击查看 nginx.conf 样例
google_perftools_profiles /tmp/tcmalloc;
user  nginx nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #按照cpu数目来指定
worker_rlimit_nofile 51200; 
error_log /usr/app/nginx/logs/error.log error;
pid /usr/app/nginx/logs/nginx.pid; 

events {
    use epoll;
    worker_connections  65532;
    multi_accept on;
}
http {
    include /usr/app/nginx/conf/mime.types; 
    default_type application/octet-stream; 
    charset utf-8;
    access_log off;
    access_log   /dev/null;

    ssi on;
    ssi_silent_errors on;
    ssi_types text/shtml;

    keepalive_timeout 65;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    #隐藏版本号
    server_tokens off;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 4k; 
    large_client_header_buffers 4 64k;
    #client_max_body_size 3000m;#上传文件大小限制
    client_body_buffer_size 256k;
    open_file_cache max=65532 inactive=120s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 2;

    limit_conn_zone   $binary_remote_addr zone=one:100m;
    #指定一个会话最大的并发连接数。当超过指定的最发并发连接数时,服务器将返回 "Service unavailable" (503)。
    limit_conn one 8;
    #limit_rate 300k;
    limit_req_zone  $binary_remote_addr  zone=rone:100m  rate=20r/s;
    proxy_cache_path /dev/shm/proxy_cache_path levels=1:2 keys_zone=xiong:1200m inactive=3h max_size=20g;     
    
    gzip on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types   text/plain application/x-javascript text/css application/xml text/javascript;
    gzip_vary on;
    
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffering    on;
    proxy_buffer_size        32k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;

    # 集群中的所有后台服务器的配置信息
    #down 表示单前的server暂时不参与负载 
    #weight 默认为1.weight越大,负载的权重就越大。 权值越高被分配到的几率越大
    #max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 
    #fail_timeout:max_fails次失败后,暂停的时间。 
    #backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
 
    upstream tomcats_proxy {
        ip_hash;
        #sticky;
        server 127.0.0.1:8081;
        server 127.0.0.1:8082;
        keepalive 16;
        check interval=5000 rise=1 fall=3 timeout=3000;   #interval检测间隔5000毫秒,rise请求1次正常标记为正常up,fall失败3次标记为失败down,timeout超时为3000毫秒
    }
    
    server {
        listen   80;
        listen [::]:80 ipv6only=on;
        root /usr/app/proxy/www;
        index login.shtml;
        location ~ ^/(WEB-INF)/ {deny all;}     #禁止访问目录
        # if (-d \$request_filename){rewrite ^/(.*)([^/])\$ http://\$host/\$1\$2/ permanent;}  # 使/abc这样的目录自动转到/abc/
        
        #客户端curl某个页面,返回404
        #if ($http_user_agent ~*"curl"){
        #    return 404;
        #}

        location ~ \.flv {flv;} 
        location ~ \.mp4$ {mp4;} 
        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/hls;
            expires -1;
            #root /tmp/hls;
            add_header Cache-Control no-cache;
        }

        # 清除缓存设置 例如:http://127.0.0.1/purge/test.gif可以清楚该url的缓存
        location ~ /purge(/.*)
        {
            allow 127.0.0.1;
            deny all;
            proxy_cache_purge xiong $host$1$is_args$args;
        }

#        location ~ ^/(ImageFile)/ {
#            root /usr/app/proxy/proxyserver;
#        }
#        location /status {
#            check_status;
#            access_log   off;
#            allow 127.0.0.1;
#            deny all;
#        }
        #缩放图片 http://127.0.0.1/1.jpg?r100x20
        location ~* \.(gif|jpg|jpeg|png)/r(\d+)x(\d+)$ {
            rewrite ^(.+)/r(\d+)x(\d+)$ $1 break;
            image_filter resize $2 $3;
            #image_filter crop $2 $3;
            image_filter_jpeg_quality 65;
            image_filter_buffer         20M;
            image_filter_interlace      on;
            image_filter_transparency on;

            expires      2d;
            #如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
             proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
            proxy_cache xiong;
            proxy_cache_key $uri$is_args$args;
            proxy_cache_valid  200 304 302 5m;

            proxy_pass http://tomcats_proxy;
            proxy_redirect off;
            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 Accept-Encoding '';
#           proxy_ignore_headers "Cache-Control" "Expires";
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
            #限制一个客户端的并发连接数,超过了这个数就要排队等待了,nodelay指超过了这个数不排队直接拒绝。
            limit_req  zone=rone burst=20 nodelay;
        }

        location ~* \.(gif|jpg|jpeg|png)$ {
            if ( $args ~* ^(.*)r\d+x\d+$ ){
              # rewrite ^(.+)$ $1/$args? last;
                 proxy_pass $scheme://$host:$server_port/$uri/$args;
            }
            expires      2d;

            #如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
            proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
            proxy_cache xiong;
            proxy_cache_key $uri$is_args$args;
            proxy_cache_valid  200 304 302 5m;

            proxy_pass http://tomcats_proxy;
            proxy_redirect off;
            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 Accept-Encoding '';
#           proxy_ignore_headers "Cache-Control" "Expires";
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
            #限制一个客户端的并发连接数,超过了这个数就要排队等待了,nodelay指超过了这个数不排队直接拒绝。
            limit_req  zone=rone burst=20 nodelay;
       }
        #动态文件访问交后端tomcat服务器组处理
        location ~.*\.(jsp|do)?$ {
            #如果后端的服务器返回500、502、503、504执行超时等错误、自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
            proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
            proxy_cache xiong;
            proxy_cache_key $uri$is_args$args;
            #缓存10分钟
            proxy_cache_valid  200 304 302 10m;

            #配置支持websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            #配置websocket经常断的情况:
            proxy_connect_timeout 4s;
            proxy_read_timeout 60s; #可以考虑这个时间配置长一点
            proxy_send_timeout 12s; #可以考虑这个时间配置长一点


            proxy_pass http://tomcats_proxy;
            proxy_redirect off;
            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 Accept-Encoding '';
            #限制一个客户端的并发连接数,超过了这个数就要排队等待了,nodelay指超过了这个数不排队直接拒绝。
            limit_req  zone=rone burst=20 nodelay;
        }
  
        location / {
            expires 2d;
            proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
            proxy_cache xiong;
            proxy_cache_key $uri$is_args$args;
#            proxy_cache_valid  200 304 302 5m;
            proxy_cache_valid any 30m;
            proxy_pass http://tomcats_proxy;
            proxy_redirect off;
            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 Accept-Encoding '';
#           proxy_ignore_headers "Cache-Control" "Expires";
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;            
            #限制一个客户端的并发连接数,超过了这个数就要排队等待了,nodelay指超过了这个数不排队直接拒绝。
            limit_req  zone=rone burst=20 nodelay;
        }

        #一定是不走tomcat的,走外部目录file路径的,file目录是存放附件的目录,tomcat不能直接访问的
        #缩放图片 http://127.0.0.1/file/201803/1.jpg?r100x20
        location ~* /file/.*\.(gif|jpg|jpeg|png)$ {
            if ( $args ~* ^(.*)r\d+x\d+$ ){
                 proxy_pass $scheme://$host:$server_port/$uri/$args;
            }
            root /usr/app/hhj/attefile/;
            
            expires      2d;

            proxy_cache xiong;
            proxy_cache_key $uri$is_args$args;
            proxy_cache_valid  200 304 302 5m;
            proxy_redirect off;
            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 Accept-Encoding '';
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
       }
       #一定是不走tomcat的,接受file路径跳转过来的url
       location ~ ^/file/.*\.(gif|jpg|jpeg|png)/r(\d+)x(\d+)$ {
                rewrite ^(.+)/r(\d+)x(\d+)$ $1 break;
                root /usr/app/hhj/attefile;
                image_filter resize $2 $3;
                image_filter_jpeg_quality 65;
                image_filter_buffer         20M;
                image_filter_interlace      on;
                image_filter_transparency on;
        }
    }

    # 配置php
    server {
        listen   68;
        root /usr/app/php/www;
        index index.php;

        # 清除缓存设置 例如:http://127.0.0.1/purge/test.gif可以清楚该url的缓存
        location ~ /purge(/.*)
        {
            allow 127.0.0.1;
            deny all;
            proxy_cache_purge xiong $host$1$is_args$args;
        }

        #以扩展名方式匹配静态文件,缓存静态文件
        location ~* \.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|jar|txt|flv|swf|txt|wma|doc|docx|xls|xlsx|pdf)$ {
            expires 30m;
            proxy_cache xiong;
            proxy_cache_valid 200 304 1d;  #针对 200 304 页面缓存1小时
            proxy_cache_valid any 30m;  #其他页面缓存30分钟
            #proxy_cache_key 指定了包含在缓存中的缓存关键字(默认情况下服务器的主机名并没有包含到缓存关键字中)
            proxy_cache_key $host$uri$is_args$args;
            proxy_redirect off;
            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 Accept-Encoding "";
            proxy_ignore_headers "Cache-Control" "Expires";
        }
        #动态文件访问交后端php服务器组处理
        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;
        }
    }

}
#配置流媒体服务器
rtmp {
    access_log off;
    access_log  /dev/null;
    server {
        listen 1935;
        application live {
            live on;
            hls on;
            hls_path /tmp/hls;
        }
    }
}
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

# 编写 nginx 自启动服务文件

vi /etc/init.d/nginx
1
点击查看启动文件样例
#!/bin/bash
# chkconfig: 2345 58 74
# description: nginx web server
# processname: nginx
# config: /usr/app/nginx/conf/nginx.conf
# pidfile: /usr/app/nginx/log/nginx.pid
case $1 in
        start)
                rm -rf /dev/shm/proxy_cache_path
                /usr/app/nginx/sbin/nginx
                ;;
        stop)
                /usr/app/nginx/sbin/nginx -s stop
                ;;
        restart)
                rm -rf /dev/shm/proxy_cache_path
                /usr/app/nginx/sbin/nginx -s stop
                /usr/app/nginx/sbin/nginx
                ;;
        esac
exit 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 设置 nginx 为自启动服务

chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
systemctl enable nginx
1
2
3
4