您的当前位置:首页>全部文章>文章详情

nginx反向代理+负载均衡配置

发表于:2020-12-25 15:13:46浏览:86次TAG: #Nginx

反向代理多台机器,前端一台机代理到后端多台机器

配置前端机器

# 以 www.test.com 域名为例子
# nginx 中 location 配置
location / {
    proxy_set_header Host www.test.com
    include proxy_params;
    proxy_pass http://http-www-test-proxy
}

# upstream.conf 配置
$ vim /etc/nginx/conf.d/upstream.conf
upstream http-www-test-proxy {
    server 10.10.10.1:80 max_fails=3 fail_timeout=5s weight=5;
    server 10.10.10.2:80 max_fails=3 fail_timeout=5s weight=5 down;
}
# max_fails=3 : 最大失败次数=3
# fail_timeout=5s : max_fails次失败后暂停时间
# weight : 权重,默认1
# down|backup : down不参与负载均衡|backup其他所有非backup机器down或者忙时请求

配置后端机器

# 10.10.10.1 or 10.10.10.2
server {
    listen 80;
    server_name  www.test.com;
    root /home/www/code/www.test.com/htdocs;
    index  index.php index.html index.htm;

    location ~ \.php$ {
        client_max_body_size 200m;
        fastcgi_pass    php7-fpm-service;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    error_log  /home/logs/nginx/www.test.com/error.log;
}

后端机器 php7-fpm-service php负载均衡配置

$ vim /etc/nginx/conf.d/upstream.conf
// 添加如下
upstream php7-fpm-service {
    server 127.0.0.1:9901 max_fails=3 fail_timeout=5s weight=9;
    server 127.0.0.1:9902 max_fails=3 fail_timeout=5s weight=6;
    server 127.0.0.1:9903 max_fails=3 fail_timeout=5s weight=3;
    server 127.0.0.1:9904 max_fails=3 fail_timeout=5s weight=1;
}
栏目分类全部>
腾讯云采购季云服务器一折促销