Nginx —— 负载均衡
Nginx 实现负载均衡,Nginx 是个反向代理网关,可以将请求分发到不同的负载服务器,支持大量的链接。
配置文件:
user www-data;
worker_processes 2; # 进程数
worker_cpu_affinity 00000001 00000010;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;
events {
use epoll;
worker_connections 1024; # 每个进程允许的最大链接数
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 500m;
resolver 114.114.114.114;
log_format main '$time_local $remote_addr $remote_user $status '
'$request_time $request [$body_bytes_sent/$bytes_sent] '
'"$http_user_agent" "$http_referer" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
# 负载 1:1 分发请求
upstream serverCluster{
server 172.31.5.3:8007 weight=1;
server 172.31.37.173:8006 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_pass http://serverCluster;
proxy_connect_timeout 1000;
proxy_send_timeout 1000;
proxy_read_timeout 1000;
}
}
}
问题:nginx访问出现504 Gateway Time-out
server {
listen 8888;
location / {
proxy_pass http://pdfs;
proxy_connect_timeout 18000;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
}
}
proxy_connect_timeout 1800s;#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 1800s;#后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 1800s;#连接成功后,后端服务器响应时间(代理接收超时)
这三个配置很重要,,,如果请求的服务器很长时间不返回,设置长一点,可以保证不是 nginx报超时。