Nginx反向代理笔记
名词规范
- 源站:实际部署有网站或应用的服务器,也就是Nginx要请求的服务器
- 代理站:装有Nginx的服务器,也就是暴露给用户访问的服务器
- 代理域名:用户用来访问代理站的域名
TCP层代理
特性
- 不是HTTP层的代理,Nginx服务器无法对SSL进行解密,显然也就无法支持缓存,以及需要修改HTTP报文的功能
- 不是我们通常说的反向代理,更像是根据SNI分流进行的端口转发
- 可以根据SNI选取upstream
- 若源站支持SSL,则代理站域名与源站域名必须相同,代理站无需管理证书
步骤
运行
sudo apt install libnginx-mod-stream编辑
/etc/nginx/nginx.conf在
stream{...}中添加上游(使用域名或IP均可)并指定端口在
stream{...}中添加映射(可使用正则表达式)- 支持使用正则表达式,如需匹配
*.dreamchaser-luzeyu.cn可写~^(?<subdomain>\w+)\.dreamchaser-luzeyu\.cn$ - 这一步完成后,重启nginx,将域名通过hosts或修改DNS解析来解析到nginx服务器,此时应该已经可以访问到源站
- 但是80端口还是nginx的默认页面,还需让http请求重定向到https
1
2
3
4
5
6
7
8stream {
map $ssl_preread_server_name $backend {
~^(?<subdomain>\w+)\.dreamchaser-luzeyu\.cn$ dreamchaser-luzeyu-cn;
}
upstream dreamchaser-luzeyu-cn {
server 162.159.153.2:443;
}
}- 支持使用正则表达式,如需匹配
添加
/etc/nginx/conf.d/force_https.conf,修改如下1
2
3
4
5server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}此时对所有域名的http请求都会重定向到https上
若只希望针对某个域名进行重定向,可去除
default_server选项,并在server_name后指定域名如果对所有域名的请求都重定向,此时重启nginx可能报错,因为还有一个默认服务器也监听了80端口
编辑
/etc/nginx/sites-available/default,删除掉这个网站的80端口监听即可如果服务器还有其他需求,希望仅仅对几个
host_name进行重定向,可以修改如下:1
2
3
4
5
6
7
8
9
10server {
listen 80;
# 填写需要跳转的域名
server_name storage.020124.xyz storage.dreamchaser-luzeyu.cn cloud.020124.xyz cloud.dreamchaser-luzeyu.cn www.020124.xyz www.dreamchaser-luzeyu.cn static.020124.xyz static.dreamchaser-luzeyu.cn;
# 使用正则表达式匹配子域名的访问
# server_name ~^(?<subdomain>\w+)\.dreamchaser-luzeyu\.cn$;
# 使用星号匹配
# server_name *.dreamchaser-luzeyu.cn *.020124.xyz;
return 301 https://$host$request_uri;
}
重启Nginx
应用层反向代理
特性
- 我们常说的反向代理一般指这种,CDN也是类似的原理(GCore CDN甚至直接用的Nginx)
- 可以修改HTTP报文,支持缓存功能
- 源站域名可以与代理站不同
- 因此可以用来代理运行在同一服务器上的Tomcat、Flask、Docker应用
- 需要在代理站上配置代理域名的SSL证书
步骤
在
/etc/nginx/conf.d创建site.conf编辑内容如下
- 可手动修改SSL端口,以及HTTP端口
- 填入域名,配置证书的路径
- 根据应用的需要修改其他配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25server {
listen 80;
listen 443 ssl; # 监听443 SSL
server_name tc.hk.020124.xyz; # 域名
ssl_certificate /etc/letsencrypt/live/tc.hk.020124.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/tc.hk.020124.xyz/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_redirect off;
# 配置代理 指向tomcat/flask/docker/...服务
proxy_pass http://127.0.0.1:8899;
client_max_body_size 40000m;
}
}重启Nginx
acme.sh证书申请
将域名解析到服务器IP
安装acme.sh,确保服务器上有Nginx
创建server_name为该域名的server,示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22server {
listen 80;
listen [::]:80;
# listen 4443 ssl http2;
# listen [::]:4443 ssl http2;
server_name example.dreamchaser-luzeyu.cn;
root /usr/share/nginx/html;
# ssl_certificate "/root/.acme.sh/example.dreamchaserluzeyu.cn_ecc/fullchain.cer";
# ssl_certificate_key "/root/.acme.sh/example.dreamchaserluzeyu.cn_ecc/example.dreamchaser-luzeyu.cn.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}使用acme.sh申请并安装ssl证书
1
2
3
4
5
6
7# --- 申请证书
acme.sh --issue --domain example.dreamchaser-luzeyu.cn --nginx
# --- 安装证书
acme.sh --install-cert --domain example.dreamchaser-luzeyu.cn \
--key-file /etc/nginx/ssl/example.dreamchaser-luzeyu.cn/example.dreamchaser-luzeyu.cn.key \
--fullchain-file /etc/nginx/ssl/example.dreamchaser-luzeyu.cn/fullchain.cer \
--reloadcmd "service nginx force-reload"自动更新证书
1
acme.sh --install-cronjob
典型配置
规范
- 清除
/etc/nginx/sites-available/default中的网站 - REALITY协议监听8443端口
- VMess WS协议监听8080端口,路径为
/data - VMess WS域名规定为
<vps_info>data.020124.xyz以及<vps_info>data.dreamchaser-luzeyu.cn,代理路径为/rpc_warpper - 对于其他的
http://*.020124.xyz以及http://*.dreamchaser-luzeyu.cn的请求,统一重定向到https
代理WebSocket
nano /etc/nginx/conf.d/ws_proxy.conf
1 | server { |
强制https
/etc/nginx/conf.d/force_https.conf
1 | server { |
代理Cloudflare
nano /etc/nginx/nginx.conf在最外层添加:
1 | stream { |
如果报错[emerg] unknown directive "stream" ,则使用命令sudo apt install libnginx-mod-stream安装stream模块。
如果还需要代理cloudflare的80端口,则需要配置应用层代理,因为Nginx的TCP代理只支持具有SNI的SSL流量。
touch /etc/nginx/conf.d/cf_proxy.conf && nano /etc/nginx/conf.d/cf_proxy.conf
1 | # Create variable `upstream_server` based on passed $host |
注:
server_name似乎不支持域名的正则匹配,因此使用map进行匹配nginx将优先匹配有
server_name的server,如果无一匹配,才会匹配端口相同、无server_name的server,因此这个配置不会影响其他服务map命令用于创建一个新的变量,举例如下:1
2
3
4
5
6
7
8# 创建一个名为$upstream_server的变量
map $host $upstream_server {
default default_server;
# 如果传入的请求的host为example.com,那么$upstream_server就为server1
example.com server1;
# 如果传入的请求的host为another-example.com,那么$upstream_server就为server2
another-example.com server2;
}
还应注释/etc/nginx/nginx.conf中的这一行:
1 | include /etc/nginx/sites-enabled/*; |
nginx上游节点
对于国内服务器
1 | upstream my-cf-http { |
注意端口要根据实际情况进行修改。
常用文本
常用命令
1 | sudo apt update && sudo apt install nginx libnginx-mod-stream lsb-release gpg coreutils tor nano -y |
Nginx配置文件
/etc/nginx/nginx.conf
1 | user www-data; |
xray配置模板
海外VPS
1 | { |
中转VPS
1 | { |
服务模板
1 | [Unit] |