【问题标题】:Nginx as a forward proxy for websockets and post requestsNginx 作为 websockets 和 post 请求的转发代理
【发布时间】:2018-07-17 16:37:32
【问题描述】:

我目前正在使用 nginx 作为 websockets 的转发代理,到目前为止它一直运行良好。客户端连接:

var ws = new WebSocket('ws://10.30.0.142:8020/');

我也想转发一个帖子请求。在这种情况下,客户端将 /post 添加到 ws 地址,以便地址扩展为 'ws://10.30.0.142:8020/post'。但是对该地址的请求返回:

http://10.30.0.142/post 404 (Not Found)

我正在使用以下配置文件(nginx.conf),这对于发布请求(位置/post/)很可能是错误的:

upstream websocket {
        server 127.0.0.1:8010;
}

server {
        listen       8020;
        server_name  server;
        root /var/www/html;
        expires off;

        keepalive_timeout 0;

        access_log /dev/null;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

        location /post/ {
            proxy_pass http://127.0.0.1:8010;
       }

        location ~* \.(?:css|js|map|jpe?g|gif|png)$ { }

    }

 }

我应该如何正确配置这个文件来解决这个问题?

【问题讨论】:

    标签: nginx post websocket


    【解决方案1】:

    幸运的是,解决方法非常简单:只需将行 location /post/ { 更改为 location /post {,多余的斜杠仅匹配对 /post/<something else> 的请求,根据您的描述,这不是您想要的。

    事实上,如果您只想匹配对/post 的请求,而不是对/post<some other string>/post/<some other string> 的请求,您甚至可能想将该行更改为location =/post {

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 2018-02-14
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      相关资源
      最近更新 更多