本文内容参考自《 自动的 Nginx 反向代理配置 》。
个人觉得:名字翻译成《自动化 Nginx 反向代理配置》更为贴切。
【一句话总结】
介绍了如何构建一个能够自动化配置 nginx 反向代理的方式。即将后端服务的部署与前端 nginx 的配置更改进行解耦。
【知识点】
- 对于 API 的使用者来说所有操作都是在同一个 URL 空间里进行的,而实际上是根据 URL 中不同的顶级“段”来进行路由的。
- 自动化配置的流程:当增加处理新“段”的 server 后,将投递一条 rabbitmq 消息(包含 Claim、ipAddress 和 PortNumber 信息),然后通过一个定制的组件 ProxyAutomation 消费 rabbitmq 消息,之后通过 SSH 和 SCP 重新 nginx 的配置,并促使配置的重新加载。
- SSH.NET 上有相关库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
...
http {
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 main;
sendfile on;
keepalive_timeout 65;
include
/etc/nginx/conf
.d/*.conf;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
include
/etc/nginx/conf
.d
/api
.example.com.conf.d
/upstream
.*.conf;
server {
listen 80;
server_name api.example.com;
include
/etc/nginx/conf
.d
/api
.example.com.conf.d
/location
.*.conf;
location / {
root
/usr/share/nginx/api
.example.com;
index index.html index.htm;
}
}
|
例如需要增加 stock “段”,则要创建如下配置文件
1
2
3
|
location
/stock/
{
proxy_pass http:
//stock
;
}
|
1
2
3
4
|
upstream stock {
server 10.0.0.23:8001;
server 10.0.0.23:8002;
}
|