nginx 相关备忘
安装 openresty nginx
1 | yum install pcre-devel openssl-devel gcc curl |
配置service
1 | vim /usr/lib/systemd/system/openresty.service |
1 | [Unit] |
- 之后的操作命令:
1
systemctl start|stop|restart|reload openresty
- 报错
systemd[1]: PID file /data/soft/openresty/nginx.pid not readable (yet?) after start.
可能是 service 配置的 PIDFile 和 nginx.conf 的不一样,或者是 nginx.conf 用的用户没有示例 /data/soft/openresty 的读写权限
配置
nginx.conf
- 需要注意 user 配置,需要确认这个用户的权限
- 需要注意 pid 配置,需要和 service 配置一致
1
2
3
4
5user root;
worker_processes auto;
worker_rlimit_nofile 102400;
error_log logs/error.log ;
pid /data/soft/openresty/nginx.pid;
示例:pay 路由
- pay.route.conf
示例展示用 pay.route.lua 里的规则重置 ngx.proxy,然后代理到对应处理里面
需要用 rewrite_by_lua/rewrite_by_lua_file 重写 ngx.proxy
需要注意这里的相对路径是从 /data/soft/openresty/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
47server {
listen 8080;
location / {
limit_except POST { deny all; }
set $proxy "";
rewrite_by_lua_file conf/vhosts/pay.route.lua;
proxy_pass http://$proxy$uri;
}
}
server {
listen 3000;
location = /pay {
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_block {
ngx.say("3000....")
ngx.exit(200)
}
}
}
server {
listen 3001;
location = /pay {
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_block {
ngx.say("3001....")
ngx.exit(200)
}
}
}
server {
listen 3002;
location = / {
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_block {
ngx.say("error")
ngx.exit(400)
}
}
} - pay.route.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-- pay route
local default_proxy = "127.0.0.1:3002"
local route_proxy_config = {
["1012"] = "127.0.0.1:3000",
["1016"] = "127.0.0.1:3001",
["1018"] = "127.0.0.1:3002",
};
local area_args_key = "ext"
local area_index = 2
-- route_proxy
local route_proxy = default_proxy
ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if args[area_args_key] then
local index = 1
for area in string.gmatch(args[area_args_key], "[^|]+") do
if index == area_index then
if route_proxy_config[area] ~= nil then
route_proxy = route_proxy_config[area]
break
end
end
index = index + 1
end
end
ngx.var.proxy = route_proxy