棘轮、wss 和 nginx 配置

Posted

技术标签:

【中文标题】棘轮、wss 和 nginx 配置【英文标题】:ratchet, wss & nginx configuration 【发布时间】:2018-02-05 12:37:56 【问题描述】:

我当前的 nginx 配置文件:

server 
    listen 443 ssl default_server;
    listen [::]:80 ipv6only=on;

    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;

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

    root /var/www/domain/public;
    index index.php index.html index.htm;

    server_name domain;

    location / 
            try_files $uri $uri/ /index.php?$query_string;
    

# PHP-FPM Configuration Nginx
    location ~ \.php$ 
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    

我希望能够运行 2 个安全的 WebSocket 服务器 (wss://) - 一个在 8443 端口上运行,另一个在 8444 上运行。 我尝试了许多配置建议,但似乎都不起作用(连接超时)。

更新:

我希望能够像这样连接到 WebSocket 服务器:

conn = new ab.Session('wss://domain:8443',....)

有可能吗?还是我应该更改连接 URI?

有什么建议吗?

【问题讨论】:

【参考方案1】:

经过大量挖掘,我设法解决了我的问题:

我已经从一开始就尝试了以下设置,但就我而言,我的所有问题都是防火墙设置.. 是的,这很愚蠢

首先 - 超时问题的原因是防火墙

所以,为了启用您的 tcp 端口,请使用 (Centos 7):

firewall-cmd --zone=public --add-port=80/tcp --permanent

那么,

firewall-cmd --reload

伟大的指南:http://ask.xmodulo.com/open-port-firewall-centos-rhel.html

我的设置:

upstream websocket
    server 127.0.0.1:8443;


map $http_upgrade $connection_upgrade 
    default Upgrade;
    '' close;


server 
        listen 443 ssl default_server;
        listen [::]:443 default_server ssl http2 ipv6only=on;
        ssl on;
        ssl_prefer_server_ciphers on;
        ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;

        if ($request_uri ~ "^[^?]*//") 
            rewrite "(.*)" $scheme://$host$1 permanent;
        

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

        root /var/www/domain/public;
        index index.php index.html index.htm;

        server_name domain

        location / 
            try_files $uri $uri/ /index.php?$query_string;
        

        # PHP-FPM Configuration Nginx
        location ~ \.php$ 
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        
        location /ws/ 
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_redirect off;
            proxy_read_timeout 86400s;
            proxy_send_timeout 86400s;
            keepalive_timeout 86400s;
            # prevents 502 bad gateway error
            proxy_buffers 8 32k;
            proxy_buffer_size 64k;
            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 https;
            reset_timedout_connection on;
        

希望它能帮助其他人:)

【讨论】:

以上是关于棘轮、wss 和 nginx 配置的主要内容,如果未能解决你的问题,请参考以下文章

nginx 如何同时配置https和wss

Nginx配置https和wss

用于 SSL PHP 站点和 Websockets WSS 的 Nginx 配置?

nginx配置支持https和wss(websocket)协议

Nginx配置之WSS

Nginx配置wss访问实现微信小程序的websocket通信