我有一个websocket的应用程序,它位于:
http://localhost:8888/something/ws
我想使用nginx作为代理,但是连接的转发不起作用。这是我的配置文件:
upstream backend {
server 127.0.0.1:8888;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://backend;
proxy_next_upstream error;
}
location /something/ws {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
故障是什么?我需要重写路径吗?
Ubuntu 12.04。随nginx 1.1一起发布。在升级nginx到1.6之前,我不得不删除nginx-common和nginx-full,否则nginx 1.1将被重新安装…
您可以在下面尝试:
location /something/ws/ {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Path rewriting
rewrite /something/ws/(.*) /$1 break;
proxy_redirect off;
}
关键是路径重写