棘轮、wss 和 nginx 配置

ratchet, wss & nginx configuration

我当前的 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://) - 一个 运行ning 在 8443 端口上,另一个在 8444 上。 我尝试了很多配置建议,但其中 none 似乎有效(连接超时)。

更新:

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

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

可能吗?或者我应该更改连接 URI?

有什么建议吗?

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

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

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

因此,为了启用您的 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 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;
        }
}

希望对其他人有所帮助:)