第一次使用nginx.
我有一个nodejs WebSocket服务器监听ws:// service_name:3600.
我正在使用docker-compose:
version: "2"
services:
# stuff
service_name:
image: imagename
ports:
- 3600:3600
links:
# stuff
- proxy
proxy:
image: image-from-nginx-with-custom-config
ports:
- 80:80
- 443:443
- 8443:8443
我的配置:
// stuff
server {
listen 8443;
server_name localhost;
ssl on;
ssl_certificate /etc/nginx/certs/crt.pem;
ssl_certificate_key /etc/nginx/certs/key.pem;
keepalive_timeout 60;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
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-Forwarded-Proto https;
location / {
proxy_pass ws://service_name:3600;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
我在启动时获得了/etc/nginx/conf.d/default.conf中的nginx:[emerg]无效的URL前缀.
所以nginx不承认ws,我该怎么办?
解决方法:
在nginx中,你仍然需要在你的网址中使用http作为协议而不是ws.
proxy_pass http://service_name:3600;
浏览器需要ws和wss协议,在下面添加的服务器端通过http处理websockets
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";