Nginx 各种代理姿势

接口代理

1
2
3
4
5
6
7
8
9
10
11
location /apis {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
rewrite ^/apis/(.*)$ /$1 break;
proxy_pass http://test.amazonaws.com:8082;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
}

Websocket 代理

1
2
3
4
5
6
7
8
9
10
11
12
location /ws/ {
proxy_pass http://test.amazonaws.com:8082/ws;
proxy_http_version 1.1;
proxy_read_timeout 360s;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade"; #配置连接为升级连接
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Cloudflare Image反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
location /image/ {
rewrite ^/image/(.*)$ /xxxxx/$1/public break;
proxy_pass https://imagedelivery.net;
proxy_ssl_name imagedelivery.net;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_set_header Host imagedelivery.net;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_hide_header Upgrade;
proxy_hide_header Alt-Svc;
proxy_hide_header Expect-CT;
proxy_http_version 1.1;
proxy_set_header Connection "";
}

静态图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /static {
alias /xx/xxx;
proxy_ssl_name imagedelivery.net;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_set_header Host imagedelivery.net;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_hide_header Upgrade;
proxy_hide_header Alt-Svc;
proxy_hide_header Expect-CT;
proxy_http_version 1.1;
proxy_set_header Connection "";
}