使用 nginx 验证 websocket 请求
Authenticating websocket requests with nginx
有一个我不能修改的WebSocket服务器,我不想让每个人都向这个发送请求。我想向 WebSocket 添加类似 HTTP 基本身份验证的内容,以便它不允许每个请求都传递到实际服务器。我正在使用 Nginx 并想坚持使用它,但如果有任何其他程序可以实现这一点,我也可以接受。
感谢 Lawrence,我查看了子请求的文档,在使用 node.js
编写了一个身份验证服务器后,现在可以使用了
server {
location /ws {
auth_request /auth;
auth_request_set $auth_status $upstream_status;
proxy_pass http://localhost:1234;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /auth {
internal;
proxy_pass http://localhost:3003;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Remote-Addr $remote_addr;
proxy_set_header X-Original-Host $host;
}
}
有一个我不能修改的WebSocket服务器,我不想让每个人都向这个发送请求。我想向 WebSocket 添加类似 HTTP 基本身份验证的内容,以便它不允许每个请求都传递到实际服务器。我正在使用 Nginx 并想坚持使用它,但如果有任何其他程序可以实现这一点,我也可以接受。
感谢 Lawrence,我查看了子请求的文档,在使用 node.js
编写了一个身份验证服务器后,现在可以使用了server {
location /ws {
auth_request /auth;
auth_request_set $auth_status $upstream_status;
proxy_pass http://localhost:1234;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /auth {
internal;
proxy_pass http://localhost:3003;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Remote-Addr $remote_addr;
proxy_set_header X-Original-Host $host;
}
}