Nginx 反向代理 + URL 重写 + websockets

Nginx 反向代理 + URL 重写 + websockets

我看见问题,但它似乎对我来说不起作用。

目前(工作)情况是:

server {
    listen 443 ssl;

    server_name updates.example.com;
    ssl_certificate     fullchain.pem;
    ssl_certificate_key privkey.pem;

    location /update {
        proxy_pass              "http://localhost:5000";

        proxy_connect_timeout   60;
        proxy_read_timeout      60;
        proxy_send_timeout      60;
        proxy_intercept_errors  off;
        proxy_http_version      1.1;
        proxy_set_header        Host               $http_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  $scheme;
        proxy_set_header        X-Client-Subject   $ssl_client_s_dn;
        proxy_set_header        X-Client-Cert      $ssl_client_cert;
    }

    location / {
        proxy_pass              "http://localhost:5001";

        proxy_connect_timeout   60;
        proxy_read_timeout      60;
        proxy_send_timeout      60;
        proxy_intercept_errors  off;
        proxy_http_version      1.1;
        proxy_set_header        Host               $http_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  $scheme;
        proxy_set_header        Upgrade            $http_upgrade;
        proxy_set_header        Connection         "upgrade";
    }

}

现在我想向 nginx 重定向添加一个前缀位置,同时保持后端不变。

根据上述问题的答案,我尝试了一些类似的事情:

    location /manage {
        proxy_pass              "http://localhost:5001/";

        proxy_connect_timeout   60;
        proxy_read_timeout      60;
        proxy_send_timeout      60;
        proxy_intercept_errors  off;
        proxy_http_version      1.1;
        proxy_set_header        Host               $http_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  $scheme;
        proxy_set_header        Upgrade            $http_upgrade;
        proxy_set_header        Connection         "upgrade";
    }

或者:

    location /manage {
        rewrite                 /manage/(.*) /$1  break;
        proxy_pass              "http://localhost:5001";
        proxy_redirect          off;

        proxy_connect_timeout   60;
        proxy_read_timeout      60;
        proxy_send_timeout      60;
        proxy_intercept_errors  off;
        proxy_http_version      1.1;
        proxy_set_header        Host               $http_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  $scheme;
        proxy_set_header        Upgrade            $http_upgrade;
        proxy_set_header        Connection         "upgrade";
    }

实际上,这两种方法都“有点”有效,因为显示的页面正确,但都无法通过 websocket 更新页面。我在应用程序中没有看到任何错误,但无论我在页面上做什么都不会触发后端应用程序(python RemI App)中的任何内容。显然,websocket 连接没有正确传输。

我错过了什么?

答案1

此表单适用于我使用 websockets(不带引号):

location /alternate/primus/ {
    proxy_pass                  http://servername/primus/;
    proxy_http_version          1.1;
    proxy_set_header Upgrade    $http_upgrade;
    proxy_set_header Connection "upgrade";
}

相关内容