NGINX:如何将HTTPstream量代理到一台服务器,并向另一台服务器提供stream量?

我是nginx新手,以及它如何处理locatoins,并希望得到我遇到的问题的帮助:

在同一台服务器上,我有一个Apache Web服务器和一个WebSocket服务器。 Apache工作在端口8080上,websocket服务器工作在9090上。我想在前面放一个nginx代理,这样http / https通信代理到apache,ws / wss通信代理到websocket服务器。

我有以下configuration:

location /ws* { proxy_pass ws://127.0.0.1:9090; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } 

这工作正常,但我面临的问题是,所有ws *请求也redirect到Apache服务器,而我只希望他们被redirect到WS服务器。 我读过nginx只需要匹配的第一个位置,但这似乎是错误的在我的情况。

所以问题是我如何代理http(s)stream量只能到apache和ws(s)通信到web套接字服务器?

你有的位置语法看起来不像你正在做的事情。

如果您尝试使用位置URL执行regex,则正确的语法是:

 location ~ /ws.* { 

如果你想在ws目录下做所有事情,你应该有:

 location /ws/ { 

您可能需要查看文档以获取更多关于位置的信息。 http://nginx.org/en/docs/http/ngx_http_core_module.html#location