要配置Nginx以支持WSS(WebSocket Secure),你需要在Nginx配置文件中设置SSL,并将WebSocket的流量代理到Workerman服务。以下是一个基本的配置示例:
- 生成SSL证书(如果你还没有):
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
- 配置Nginx:
server {
listen 443 ssl;
server_name your_domain.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://your_workerman_server_ip:workerman_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
确保替换your_domain.com
、证书路径和your_workerman_server_ip:workerman_port
为你的实际域名、SSL证书路径和Workerman服务器的IP地址和端口号。
- 确保Workerman服务监听相应的端口(例如,如果Workerman监听1234端口,确保Nginx配置中的
proxy_pass
指向这个端口)。 - 重启Nginx服务以应用新配置:
sudo systemctl restart nginx
确保Workerman服务也在运行中,并且正确配置以处理WebSocket连接。
以上配置假设你已经在服务器上安装了Nginx和Workerman,并且Workerman已经设置好监听WebSocket连接。如果你还没有设置Workerman,你需要确保Workerman的worker正确处理WebSocket连接,例如:
use Workerman\Worker;
$worker = new Worker('websocket://your_domain.com:workerman_port');
$worker->onMessage = function($connection, $data) {
// 处理消息
};
Worker::runAll();
替换your_domain.com:workerman_port
为你的域名和端口号。