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

标签 nginx websocket reverse-proxy

我已成功将 Nginx 配置为我的 Web 应用程序的反向代理。它正确地将从我的 Angular SPA 发出的请求重定向到用 Asp Core 2.1 编写的 Web API。但是,在我的 Web API 中,我想使用使用的 WebSocketsws://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.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";
        }
    } 
}

希望有人觉得它有用:)

关于nginx - 如何配置nginx代理ws(websocket)协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53745789/

相关文章:

除根目录外的任何其他页面上的 django、nginx、gunicorn 404

java - 使用websocket制作android聊天模块

python-3.x - 是否有服务器 Python 3 websocket 模块?

nginx - 反向代理后面的HTTP/2

Apache ProxyPass - 与远程服务器进行 SSL 握手期间出错

reverse-proxy - 使用 nginx 正则表达式位置匹配将 URI 动态映射到多个反向代理的不同端口

android - nginx 从 Web 重定向到移动应用程序

docker - Nginx与docker,找不到与斜杠不同的位置

linux - 如何在标题中显示 nginx 缓存文件的年龄

ruby-on-rails - Rails 5 actioncable 卡住服务器