我如何修复这个Nginx配置正确代理WebSocket请求,而不是返回301



Nginx新手。试图配置Nginx作为另一个web服务器运行在http://localhost:8082前面的SSL代理服务器。也就是说,我希望所有对http://localhost的请求都重定向到https://localhost。那部分工作得很好。

问题是,端口8082上的应用程序也使用WebSocket连接在ws://localhost:8082/public-api/repossession-requests-socket。我试图将任何连接重定向到ws://localhost/public-api/repossession-requests-socketwss://localhost/public-api/repossession-requests-socket,并让Nginx代理这些WebSocket请求到ws://localhost:8082/public-api/repossession-requests-socket

相反,WebSocket连接失败,因为Nginx为返回301 ws://localhost/public-api/repossession-requests-socket &wss://localhost/public-api/repossession-requests-socket。我的配置如下;我在测试中使用Docker映像nginx:alpine ($PWD映射到/app)。

我需要如何改变这一点,使我不再看到301 ?

events {
  worker_connections 1024;
}
http {
    server {
        listen 80;
        return 301 https://$host$request_uri;
    }
    server {
        listen 443;
        server_name localhost;
        ssl_certificate /app/docker/public.pem;
        ssl_certificate_key /app/docker/private.pem;
        ssl on;
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;
        access_log /app/access-443.log;
        location / {
          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 $scheme;
          proxy_pass http://localhost:8082;
          proxy_read_timeout 90;
          proxy_redirect http://localhost:8082 https://localhost;
        }
        location /public-api/repossession-requests-socket/ {
            proxy_pass http://localhost:8082;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

找到问题。location节末尾的斜杠。

location /public-api/repossession-reqeuests-socket/应该是location /public-api/repossession-reqeuests-socket

相关内容

  • 没有找到相关文章