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

idfiyjo8  于 2022-12-11  发布在  Nginx
关注(0)|答案(2)|浏览(331)

我已经成功地将Nginx配置为Web应用程序的反向代理。它可以正确地将Angular SPA发出的请求重定向到用Asp Core 2.1编写的Web API。但是,在我的Web API中,我想使用使用ws://mydomain.com/ws的WebSockets来建立连接。
这是我的可用站点现在的外观

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
    ##
}

有什么建议可以帮助我调试我的配置,看看为什么请求没有被正确重定向?

3yhwsihp

3yhwsihp1#

通过将上游添加到我的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";
        }
    } 
}

希望有人觉得它有用:)

o2gm4chl

o2gm4chl2#

我要使用使用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

相关问题