详解nginx

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
1、检查nginx基本依赖包是否安装pcre-devel openssl-devel
rpm -qa pcre-devel pcre
rpm -qa openssl-devel openssl
若没有执行以下命令安装
yum -y install pcre-devel pcre
yum -y install openssl-devel openssl
yum -y install gcc-c++
2、安装nginx
[ ! -d /home/ropon/tools ] && mkdir -p /home/ropon/tools
cd /home/ropon/tools
id -u www >/dev/null 2>&1
[ $? -ne 0 ] && useradd www -s /sbin/nologin -M -g www #创建普通用户www及www组不指定家目录
wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
tar xvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --user=www --group=www --prefix=/usr/local/nginx-1.10.3/ \
--with-http_stub_status_module --with-http_ssl_module
make
make install
ln -s /usr/local/nginx-1.10.3 /usr/local/nginx 便于后期升级nginx版本,及快速获取nginx版本号
--prefix=设置安装路径
--user=进程用户权限
--group=进程用户组权限
--with-http_stub_status_module 激活状态信息
--with-http_ssl_module 激活ssl功能
3、nginx.conf配置文件说明
worker_processes 4; #worker进程数量
events {
worker_connections 10240; #每个worker进程支持的最大连接数
}
http {
include mime.types; #nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #链接超时时间
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /ropon_ngx_status {
stub_status on; #开启nginx status
access_log off;
allow 1.1.1.1; #仅允许某个IP查看
deny all;
}
}
}
Active connections: 4 #正在处理的活动连接数
server accepts handled requests
209 209 34 启动共处理多少连接;启动共创建多少次握手(请求丢失数(握手数-连接数)); 共处理多少次请求
Reading: 0读取客户端header信息数 Writing: 1返回客户端header信息数 Waiting: 3等待下次请求驻留连接
error_log logs/error.log warnerrorcrit; #错误日志配置
可放置标签段:main,http,server,location
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr #记录访问网站的客户端IP
$http_x_forwarded_for #使用CDN记录真实客户端IP
$remote_user #
$time_local #访问时间
$request #请求页面
$status #访问状态码
$body_bytes_sent #响应body的大小,单位字节
$http_referer #
$http_user_agent #客户端UA信息
access_log logs/access.log main(默认日志记录格式); #配置访问日志
可放置标签段:http,server,location,if in location,limit_except
1.1.1.1 - - [23/Nov/2017:10:37:45 +0800] "GET /ropon.html HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-"
remote_addr 1.1.1.1
remote_user -
time_local 23/Nov/2017:10:37:45 +0800
request GET /ropon.html HTTP/1.1
status 404
body_bytes_sent 57字节
http_referer 直接访问故记录-
http_user_agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
http_x_forwarded_for 没有使用CDN故记录-
深入了解日志参数[高并发避免日志频繁写入影响IO性能]
access_log path format gzip [=level] [buffer=size] [flush=time] [if=condition];
access_log logs/access.gz main gzip buffer=32k flush=5s;
4、nginx日志切割
mkdir -p /usr/local/script
vi cut_del_logs.sh
#!/bin/bash
LOGS_PATH=/home/wwwlogs/default.gz
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOGS_PATH}/access.gz ${LOGS_PATH}/access_${YESTERDAY}.gz
kill -USR1 `ps axu grep "nginx: master process" grep -v grep awk '{print $2}'`
#kill USR1 指告诉应用程序重载配置文件相当于reload
cd ${LOGS_PATH}
find . -mtime +7 -name "*20[1-9][3-9]*" xargs rm -f
exit 0
crontab -e
1 0 * * * /usr/local/script/cut_del_logs.sh
5、配置nginx虚拟主机
mkdir /usr/local/nginx/conf/vhost
mkdir -p /home/wwwroot/default
include vhost/*.conf;
cd /usr/local/nginx/conf/vhost && vi 000default.conf
server {
listen 80;
server_name 1.1.1.1
location / {
root /home/wwwroot/default;
index index.html;
}
}
6、location的使用
location [ = ~ ~* ^~ ] uri {
}
~ 区分大小写
~* 不区分大小写
!~ !~* 取反
^~ 做常规字符串检查,不做正则匹配。
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /files/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gifjpg)$ {
[ configuration E ]
}
eg:
location = / {
return 401;
}
location /files/ {
return 402;
}
location ^~ /images/ {
return 403;
}
location ~* \.(jpg)$ {
return 405;
}
访问http://1.1.1.1 或http://1.1.1.1/ 出现401状态码
访问http://1.1.1.1/files/ 出现402状态码
访问http://1.1.1.1/images/ssfasdfsda或http://1.1.1.1/images/ 都会出现403状态码
访问http://1.1.1.1/dfasfsdfsd/sfdasdfsd.jpg或http://1.1.1.1/324123423.jpg 都会出现405状态码
=/是精确匹配优先级最高与放置顺序无关
/是默认匹配,指没有匹配上其他location,最后匹配默认
/files/是路径匹配
^~ /images/也是路径匹配,加了特殊字符^~,/files/11.jpg 优先匹配路径
~* \.(jpg)$是扩展名匹配
7、Nginx rewrite
语法:rewrite regex(正则表达式) replacement [flag];
可放置标签段:server,location,if
rewrite ^/(.*) http://1.1.1.1/$1 permanent;
^/(.*)匹配所有 匹配成功后跳转到http://1.1.1.1/$1 $1代表前面^/(.*)具体内容 permanent是永久重定向标记
\ 转义字符 \\ \$
^匹配字符串起始位置
$匹配字符串结束位置
*匹配前面字符零次或多次,比如ro* 可匹配r、roo,*相当于{0,}
+匹配前面字符一次或多次,比如ro+可匹配ro、roo,但不能匹配r,+相当于{1,}
?匹配前面字符零次或一次,比如lp(ro)?可匹配lp、lpro中lp,?相当于{0,1}
.匹配除“\n”之外的任何单字符,若要匹配\n,可使用[.\n]
(pattern)匹配括号内的pattern,并且后面可获取对应匹配,用$0,$1..$9
若要匹配括号字符,使用\( \)
last 本条规则匹配完,继续匹配下条location uri规则
break 本条规则匹配完,就终止不再继续匹配
redirect 返回302临时重定向
permanent 返回301永久重定向
例子
if ( $http_host ~* "^(.*)\.idiyrom\.com$") {
set $temp $1;
rewrite ^(.*) http://www.idiyrom.com/$temp/ropon.html;
break;
}
实现的是xxx.idiyrom.com 跳转到 www.idiyrom.com/xxx/ropon.html
8、try_files最核心的功能是可以替代rewrite
语法: try_files file ... uri 或 try_files file ... = code
try_files $uri $uri/ /?$args;
按顺序检查文件或文件夹是否存在,返回第一个找到的文件或文件夹。
结尾的斜线表示为文件夹 比如$uri/
如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数/?$args
eg:
location / {
try_files $uri $uri/ /?$args;
}
比如访问http://west.idiyrom.com/imgaes/
$uri 指的是文件,比如images文件,$uri=/imgaes/
$uri/ 指的是文件夹,比如images文件夹
如果文件或文件夹都不存在,就会内部重定向到/?$args,其中$args是get方法获取的参数,比如images/test?testcanshu
日志格式:'[$cookie_customerID_cookie_flag] [$args]' '($uri)'
[-] [testcanshu](/index.html)
$args=testcanshu
9、Nginx访问认证
auth_basic
语法:auth_basic stringoff;
可放置标签段:http,server,location,limit_except
auth_basic_user_file file;
可放置标签段:http,server,location,limit_except
htpasswd -bc /usr/local/nginx/conf/htpasswd ropon 123456
chmod 400 /usr/local/nginx/conf/htpasswd
chown www /usr/local/nginx/conf/htpasswd
location = /ropon/ {
auth_basic ropon_test;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
10、添加启动项
cat /etc/init.d/nginx
……
nginx_install_dir=/usr/local/nginx
[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=$nginx_install_dir/sbin:\$PATH" >> /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep $nginx_install_dir /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=$nginx_install_dir/sbin:\1@" /etc/profile
. /etc/profile
wget -O /etc/init.d/nginx http://mirrors.ropon.top/shell/lnmp/init.d/Nginx-init-CentOS.txt
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
自动安装shell脚本:http://mirrors.ropon.top/shell/lnmp/nginx.sh
附Rewriterule参数详解
1) R 强制外部重定向,后面可以代301或302跳转。
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联。
8) T=MIME-type(force MIME type) 强制MIME类型。
9) NS 只用于不是内部子请求。
10) NC 不区分大小写。
11) QSA 追加请求字符串。
12) NE 不在输出转义特殊字符。

更新nginx1.22.0 并安装支持lua

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
#安装所需
yum -y install pcre-devel pcre
yum -y install openssl-devel openssl
yum -y install gcc-c++

#创建普通用户及组
groupadd www && useradd www -s /sbin/nologin -M -g www

#下载所需文件
echo-nginx-module-0.62.tar.gz
luajit2-2.1-20220915.tar.gz
lua-nginx-module-0.10.22.tar.gz
lua-resty-core-0.1.24.tar.gz
lua-resty-lrucache-0.13.tar.gz
nginx-1.22.0.tar.gz
ngx_devel_kit-0.3.1.tar.gz

wget https://nginx.org/download/nginx-1.22.0.tar.gz
wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20220915.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.22.tar.gz

#批量解压
ls *.tar.gzxargs -n 1 tar -xzvf

#编译所需库
cd luajit2-2.1-20220915
make install PREFIX=/usr/local/luajit2-2.1-20220915
设置环境变量到/etc/profile
export LUAJIT_LIB=/usr/local/luajit2-2.1-20220915/lib/
export LUAJIT_INC=/usr/local/luajit2-2.1-20220915/include/luajit-2.1
source /etc/profile
ln -sf /usr/local/luajit2-2.1-20220915/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

cd ../lua-resty-core-0.1.24
make install PREFIX=/usr/local/lua_core
cd ../lua-resty-lrucache-0.13
make install PREFIX=/usr/local/lua_core

#安装nginx
cd ../nginx-1.22.0
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_ssl_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.22 --add-module=../echo-nginx-module-0.62
make -j 4
make install

nginx.conf http节加
lua_package_path "/usr/local/lua_core/lib/lua/?.lua;;";
nginx -t

nginx 安装https正向代理

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
#下载所需库
https://github.com/chobits/ngx_http_proxy_connect_module

#打补丁 看介绍不同版本补丁不一样
#这里是nginx-1.22.0
patch -p1 < ../ngx_http_proxy_connect_module-0.0.3/patch/proxy_connect_rewrite_102101.patch
#如果之前已安装nginx,查看之前编译参数nginx -V
--add-module=../ngx_http_proxy_connect_module-0.0.3
make && make install

#新建proxy.conf配置文件
server {
listen 1443;
charset utf-8;
#DNS解析(核心配置)
resolver 10.2.1.10;
client_max_body_size 50m;
access_log logs/access_proxy.log;

#need ngx_proxy module
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;

location / {
proxy_pass $scheme://$http_host$request_uri;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
proxy_next_upstream error timeout invalid_header http_502;
proxy_max_temp_file_size 0k;
proxy_ssl_server_name on;
}
}

之前是通过yum安装升级

1
2
3
4
5
6
yum -y install gcc-c++ pcre-devel pcre openssl-devel openssl
yum -y install libxslt-devel libxml2 libxml2-dev gd-devel perl-devel perl-ExtUtils-Embed gperftools

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4' --add-module=/usr/local/ngx_devel_kit-0.3.1 --add-module=/usr/local/lua-nginx-module-0.10.22 --add-module=/usr/local/echo-nginx-module-0.62
make
make install