我使用 Nginx 作为基于 PHP 的 WebSocket 应用程序的反向代理,并尝试使用 Nginx 反向代理对 WebSocket 服务器进行负载测试。当并发用户达到 1,000 左右时,我收到 503 错误。
但是,当我直接测试 PHP 应用程序而不使用 Nginx 反向代理时,该应用程序可以处理超过 5,000 个并发用户。
我的 Nginx 版本nginx version: nginx/1.18.0
以下是我的 Nginx 配置。
user nginx;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 30000;
events {
worker_connections 10000;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 128;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # 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_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
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
以下是我的 Nginx 站点配置。
server {
listen 80;
listen [::]:80;
server_name socket.mydomain.com;
location / {
proxy_pass http://127.0.0.1:6001;
proxy_set_header Host $host;
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;
proxy_set_header X-VerifiedViaNginx yes;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_redirect off;
# Specific for websockets: force the use of HTTP/1.1 and set the Upgrade header
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
我的配置有问题吗?还是我忘记更新什么了?
答案1
我发现了一个问题。这是因为 Cloudflare。我需要启用仅 DNS 模式。
可以在此处阅读有关 Cloudflare WebSocket 的信息。
https://support.cloudflare.com/hc/en-us/articles/200169466-Using-Cloudflare-with-WebSockets