【问题标题】:How to configure nginx to proxy ws (websocket) protocol如何将 nginx 配置为代理 ws (websocket) 协议
【发布时间】:2019-05-13 17:18:52
【问题描述】:

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


    【解决方案1】:

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

    希望有人觉得它有用:)

    【讨论】:

    • 为什么是地图?您没有在任何地方使用 $connection_upgrade 的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2017-05-05
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    相关资源
    最近更新 更多