# 下载 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
本例假设将安装文件下载到 /data/soft 目录。
export soft_dir="/data/soft"
cd $soft_dir
rm -f ./nginx-1.24.0
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_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
tar --no-same-owner -zxvf $soft_dir/nginx-1.24.0.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.24.0
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
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装 nginx
本例假设将安装文件下载到 /data/soft 目录。
本例安装到 /data/nginx 目录。
groupadd nginx
useradd -g nginx nginx
cd $soft_dir/nginx-1.24.0
./configure --prefix=/data/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
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 /data/nginx/conf/nginx.conf /data/nginx/conf/nginx_old.conf
vi /data/nginx/conf/nginx.conf
1
2
2
点击查看 nginx.conf 样例
google_perftools_profiles /tmp/tcmalloc;
worker_processes 8;
user nginx root;
worker_rlimit_nofile 51200;
error_log /data/nginx/logs/error.log error;
pid /data/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 65532;
multi_accept on;
}
http {
include /data/nginx/conf/mime.types;
default_type application/octet-stream;
charset utf-8;
access_log off;
access_log /dev/null;
#error_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;
client_max_body_size 3000m;
server_names_hash_bucket_size 128;
client_header_buffer_size 128k;
large_client_header_buffers 4 64k;
client_body_buffer_size 256k;
open_file_cache max=65532 inactive=120s;
open_file_cache_valid 120s;
open_file_cache_min_uses 5;
limit_conn_zone $binary_remote_addr zone=one:100m;
# limit_conn one 8;
limit_req_zone $binary_remote_addr zone=rone:100m rate=20r/s;
#proxy_temp_path /dev/shm/proxy_temp_path;
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;
upstream tomcats {
ip_hash;
# sticky;
server 127.0.0.1:8081;
# server 127.0.0.1:8082;
# server 127.0.0.1:8083;
keepalive 16;
check interval=5000 rise=1 fall=3 timeout=3000;
}
server {
listen 80;
root /data/cqupt_news/www;
charset utf-8;
default_type text/html;
index index.html;
location /api/ {
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_pass http://tomcats;
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_redirect off;
proxy_set_header Accept-Encoding '';
# limit_req zone=rone burst=20;
}
location ~ .*\.(js|css)?$ {
expires 1d;
}
}
}
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
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
# 设置自启动服务(方式一)
注:本方式适合Centos6、7、8版本
# 第一步:编写启动脚本
vi /etc/init.d/nginx
1
点击查看启动文件样例
#!/bin/bash
# chkconfig: 2345 58 74
# description: nginx web server
# processname: nginx
# config: /data/nginx/conf/nginx.conf
# pidfile: /data/nginx/log/nginx.pid
case $1 in
start)
rm -rf /dev/shm/proxy_cache_path
/data/nginx/sbin/nginx
;;
stop)
/data/nginx/sbin/nginx -s stop
;;
restart)
rm -rf /dev/shm/proxy_cache_path
/data/nginx/sbin/nginx -s stop
/data/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
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
2
3
4
# 设置自启动服务(方式2)
注:本方式适合Centos8、9版本
# 第一步:创建启动服务脚本
在nginx目录(例如:/data/nginx/) 创建启动服务的脚本start.sh
vi /data/nginx/start.sh
1
点击查看start.sh内容
#!/bin/bash
/data/nginx/sbin/nginx
exit 0
# 第二步:创建停止服务脚本
在nginx目录(例如:/data/nginx/) 创建停止服务的脚本stop.sh
vi /data/nginx/stop.sh
1
点击查看stop.sh内容
#!/bin/bash
/data/nginx/sbin/nginx -s stop
# 第三步:赋予权限
分别赋予start.sh 和 stop.sh 执行权限
chmod +x /data/nginx/start.sh
chmod +x /data/nginx/stop.sh
1
2
2
# 第四步:创建service脚本
vi /lib/systemd/system/nginx.service
1
点击查看nginx.service内容
[Unit]
Description=nginx
After=network.target
[Service]
Type = forking
ExecStart = /data/nginx/start.sh
ExecStop = /data/nginx/stop.sh
[Install]
WantedBy = multi-user.target
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 设置权限
chmod 754 /lib/systemd/system/nginx.service
### 第五步:启动或停止服务
```shell script
# 启动服务
systemctl start nginx.service
# 关闭服务
systemctl stop nginx.service
1
2
3
4
5
6
7
2
3
4
5
6
7
如果发现启动错误,修改启动脚本后需重新加载
systemctl daemon-reload
1
# 第六步:设置为自启动服务
systemctl enable nginx.service
1