我在 Ubuntu 服务器 14.04 上安装了 Nginx 作为反向代理,将来自单个 IP 地址的流量重定向到在不同端口上运行的多个 NodeJ。它还将 HTTP 重定向到 HTTPS。
以下是nginx.conf的内容:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 4096;
multi_accept on;
}
http {
##
# 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;
##
# Logging Settings
##
# access_log /var/log/nginx/access.log;
access_log off;
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_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
以下是 /sites-enabled/default 的内容:
server {
listen 80;
server_name *.domain.com;
#redirect HTTP to HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name app.domain.com;
ssl on;
ssl_certificate /etc/ssl/certs/app.domain.com_bundle.crt;
ssl_certificate_key /etc/ssl/server.key;
location / {
proxy_pass http://localhost:5000;
}
}
server {
listen 443;
server_name app2.domain.com;
ssl on;
ssl_certificate /etc/ssl/certs/app2.domain.com.crt;
ssl_certificate_key /etc/ssl/server.key;
location / {
proxy_pass http://localhost:5002;
}
}
server {
listen 443;
server_name app3.domain.com;
ssl on;
ssl_certificate /etc/ssl/certs/app3.domain.com.crt;
ssl_certificate_key /etc/ssl/server.key;
location / {
proxy_pass http://localhost:5001;
}
}
server {
listen 443;
server_name app4.domain.com;
ssl on;
ssl_certificate /etc/ssl/certs/app4.domain.com_bundle.crt;
ssl_certificate_key /etc/ssl/server.key;
location / {
proxy_pass http://localhost:4999;
}
}
我在 /var/log/nginx/error.log 中收到很多错误,如下所示,但不针对特定的服务器或文件(在 GET 中)
2016/10/05 23:59:08 [error] 11439#0: *26933 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 123.123.123.123, server: app4.domain.com, request: "GET /includes/app/language.js HTTP/1.1", upstream: "http://127.0.0.1:4999/includes/app4/language.js", host: "app4.domain.com", referrer: "https://app4.domain.com/"
如果我运行相同的 nodejs 实例并直接访问它,它会运行得非常快,所以我很确定 NGINX 就是问题所在。
编辑:经过多次测试后,我得到了更多详细信息:我现在非常确定,是我的应用程序位于反向代理之后,导致了这个问题。我重新安装了所有内容,只有一个带有 socket.io 的 nodejs 应用程序。使用以下方式访问时,它运行得非常快http://app.domain.com:5000。然后我尝试使用 nginx 和 apache2 作为反向代理,将端口 80 重定向到 443,将端口 443 重定向到应用程序的端口 5000,它开始超时并且变得非常慢,而如果直接使用端口 5000 访问,它会保持非常快。我想我并不是唯一一个想要在反向代理后面运行带有 socket.io 的 nodejs 服务器的人。
任何帮助都欢迎。非常感谢。