如何配置nginx代理ws(websocket)协议

问题描述 投票:0回答:2

我已成功将 Nginx 配置为我的 Web 应用程序的反向代理。它正确地将我的 Angular SPA 发出的请求重定向到用 Asp Core 2.1 编写的 Web API。但是,在我的 Web API 中,我想使用 WebSockets

ws://mydomain.com/ws
建立连接。

这就是我的网站现在的样子

server {
    listen 80;
    listen 443;
    ssl on;
    ssl_certificate /etc/***/***.crt;
    ssl_certificate_key /etc/***/***.key;

    root /home/***/***/***/wwwroot;

    # Add index.php to the list if you are using PHP
    index index.html;

    server_name my.domain.com;
    location / {
            try_files $uri $uri/ /index.html;
    }
    location /api/ {
            proxy_pass http://localhost:5000;
    }
    location /api/signalr {

            proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $http_connection;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
    location /ws{
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_pass http://localhost:5000;
    }
}

和 nginx.conf

http {
    map $http_upgrade $connection_upgrade{
            default upgrade;
            '' close;
    }
    upstream websocket{
            server my.domain.com;
    }
    #
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##
}

有什么建议可以调试我的配置以查看请求未正确重定向的原因吗?

nginx websocket reverse-proxy
2个回答
15
投票

通过将上游添加到我的 nginx.conf 文件中找到了解决方案。

http {
    map $http_upgrade $connection_upgrade{
            default upgrade;
            `` close;
    }
    upstream websocket{
        server 127.0.0.1:58550; 
        #SERVER endpoint that handle ws:// connections
    }

    server{
        #Now you can connect via ws:// protocol on ws://yourdomainaddress:8020/ws
        listen 8020;
        location /ws {
                proxy_pass http://websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }
    } 
}

希望有人觉得它有用:)


1
投票

我想使用使用

ws://mydomain.com/ws

的 WebSocket

这可以通过

upstream {…}
指令来实现,如您的答案中所示,但您也可以修改原始的
location /ws {…}
指令,如下所示:

location /ws {
    # … 3 unchanged lines omitted …
    proxy_pass http://localhost:5000/ws;
}

唯一的变化是将

/ws
附加到
proxy_pass
URI。这是必要的,因为当 Nginx 构造代理 URI 时,
location /ws {…}
匹配的路径前缀会自动删除。因此,当想要保留它时,我们必须将其重新添加到代理 URL 的开头,如
proxy_pass
指令所提供。

Nginx

proxy_pass
文档的话来说:

如果使用 URI 指定 proxy_pass 指令,则当请求传递到服务器时,规范化请求 URI 中与位置匹配的部分将被 [proxy_pass] 指令中指定的 URI 替换

© www.soinside.com 2019 - 2024. All rights reserved.