Nginx 如何反代 wss 连接?

345 天前
 Llesue

我当前的设置时这样的,抄了很多教程,但是不懂原理感觉抄也是白抄,不管用。

server {
        listen 86;
        server_name box.example.com file.example.com files.example.com pan.example.com;
        if ($scheme = http){
                return 301 https://$server_name:88$request_uri;
        }
}

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}

server {
    listen 88 ssl http2;
    server_name box.example.com;
    error_page 497 301 =307 https://$host:$server_port$request_uri;

    ssl_certificate /etc/letsencrypt/live/box.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/box.example.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    location / {
        proxy_pass http://localhost:8687;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "";
        proxy_max_temp_file_size 0;
        proxy_read_timeout 500s;
        proxy_connect_timeout 600;
        proxy_send_timeout 240;
    }

    location /api/command/ {
        proxy_pass http://localhost:8687;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
2101 次点击
所在节点    NGINX
16 条回复
om2mo
345 天前
你连域名都没改怎么反代?看上去并没有看教程
keegan
345 天前
要么自己看看懂,要么直接 问 GPT 吧
as9567585
345 天前
Llesue
344 天前
@om2mo 域名是我替换的。
Llesue
344 天前
@keegan 我就是高强度 ChatGPT 用户。ChatGPT 回答不了这种问题……
Llesue
344 天前
@as9567585 你要是仔细看我贴出的配置,你会发现,我就是这么配置的。
eudemonwind
344 天前
同求,想反代谷狗,但是好像会被谷狗识别出来被 nginx 反代了,不给用
NSAgold
344 天前
你这配置应该是没问题的。
我以前的配法和#3 给的 bing 的是一样的(不过我的没有 set_header Host $host 那行,因为用不到,后端是个 tcp 转 ws 的程序),完全没问题。nginx 出去甚至还能再套层 cloudflare 。
Llesue
344 天前
其实我应该多补充一下:
1 、不是常规端口,是 http 86 ,https 88 。
2 、我用的是 filebrowser , 网页访问各项功能都正常,就是 filebrowser 有个命令行工具可以在网页上使用 shell 命令,这些 shell 命令是通过 websocket 传输的。这个功能无法使用,从浏览器调试看是请求不成功:
```
commands.js:12 WebSocket connection to 'wss://box.hideurl.com:88/api/command/?auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoyLCJsb2NhbGUiOiJ6aC1jbiIsInZpZXdNb2RlIjoibGlzdCIsInNpbmdsZUNsaWNrIjpmYWxzZSwicGVybSI6eyJhZG1pbiI6dHJ1ZSwiZXhlY3V0ZSI6dHJ1ZSwiY3JlYXRlIjp0cnVlLCJyZW5hbWUiOnRydWUsIm1vZGlmeSI6dHJ1ZSwiZGVsZXRlIjp0cnVlLCJzaGFyZSI6dHJ1ZSwiZG93bmxvYWQiOnRydWV9LCJjb21tYW5kcyI6WyJ3YyIsImRmIiwicHdkIiwibG4iLCJndnNoYXJlIiwiYm94LXVzZyIsImxzIiwidHJlZSIsIm12IiwiY3AiLCJybSIsImNhdCIsInRhaWwiLCJncmVwIiwiY2QiLCJ0bGRyIiwibWtkaXIiLCJkdSIsImZpbmQiLCJjaG93biIsInZuc3RhdCIsIm1hbiIsIndnZXQiLCJyY2xvbmUiLCJpZmNvbmZpZyIsImRhdGUiLCJ3aG9hbWkiLCJuZXRzdGF0Il0sImxvY2tQYXNzd29yZCI6ZmFsc2UsImhpZGVEb3RmaWxlcyI6dHJ1ZSwiZGF0ZUZvcm1hdCI6ZmFsc2V9LCJpc3MiOiJGaWxlIEJyb3dzZXIiLCJleHAiOjE2ODQ1NTYwMjMsImlhdCI6MTY4NDU0ODgyM30.gRQ53CHiiESvt7YZN387h5u6pXrvI6jDwoVBTSW_qxg' failed:
```
3 、我的配置里第一个 location 是 filebrowser 的。第二个 location 是 websocket 的。但是 wss 连接还是不通……
4 、ChatGPT 对此类问题束手无策,会重复无用信息车轱辘话。

### 题外话:
大家可以测试一下问 ChatGPT
```
http://damn.com:86 怎么用 nginx 跳转到 https://damn.com:88
```
ChatGPT 会给你一个错误答案。然后你继续问:
```
你仔细看看你的答案正确吗?
```
ChatGPT 会说:你说的对,我犯了一个错误。正确的配置应该是这样……(把上面错误的代码再发一遍),如果你继续说它的答案不正确,它还会道歉,然后原封不动在给你发一遍刚才的配置……哈哈
githmb
344 天前
proxy_set_header Connection "Upgrade";
cquan
344 天前
我设置过,怎么设都不行,然后发现好像不是 nginx 的问题,是 python 的 uwsgi 问题。
julyclyde
343 天前
关键在于那两个 header
zanelee
328 天前
command 的那个 location 加一下 proxy_set_header Connection "Upgrade";试试
zanelee
328 天前
@zanelee 不好意思,看下了前面配了 map
zanelee
328 天前
去试了一下,出现了这种情况,我的是因为是跨域。filebrowser 的日志也会报错 websocket: request origin not allowed by Upgrader.CheckOrigin 。看了 OP 后面给出的控制台报错的 url 和 nginx 配置的 url ,感觉就是因为这个 wss 请求的时候请求头的 Orgin 和代理过去的请求头的 Host 不一致造成的。
tyit
325 天前
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";



试试这两个

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/941433

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX