ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Nginx —— 负载均衡

2022-05-06 17:31:28  阅读:41  来源: 互联网

标签:负载 http 均衡 nginx server Nginx proxy timeout log


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报超时。

参考链接:https://www.cnblogs.com/linjiqin/p/11858127.html

标签:负载,http,均衡,nginx,server,Nginx,proxy,timeout,log
来源: https://www.cnblogs.com/pythonwl/p/16229674.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有