zoukankan      html  css  js  c++  java
  • Nginx 配置负载均衡

    nginx负载均衡配置,主要是proxy_pass,upstream的使用。

    注意问题,多台机器间session的共享问题。

    不用session,用户cookie。或者用redis替代session。

    三台服务器,一台nginx转发(10.0.0.1),两台服务器(10.0.0.2,10.0.0.3)。

    nginx转发配置,

    upstream balance.xxx.com
    {
    	server 10.0.0.2:80;
    	server 10.0.0.3:80;
    }
    
    server
        {
            listen 80;
            server_name balance.xxx.com;
    
            location /
            {
    			proxy_pass        http://balance.xxx.com;
            	proxy_set_header   Host             $host;
            	proxy_set_header   X-Real-IP        $remote_addr;
            	proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            }
    
            access_log  /home/wwwlogs/access.log;
    }
    

    服务器1

    server
        {
            listen 80;
            #listen [::]:80 default_server ipv6only=on;
            server_name balance.xxx.com;
            index index.html index.htm index.php admin.php;
            root  /home/wwwroot/default/load;
    
            #error_page   404   /404.html;
            include enable-php-pathinfo.conf;
    
            location /nginx_status
            {
                stub_status on;
                access_log   off;
            }
    
            location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }
    
            location ~ .*.(js|css)?$
            {
                expires      12h;
            }
    
            location ~ /.
            {
                deny all;
            }
    
            location / {
                if (!-e $request_filename) {
                    rewrite  ^(.*)$  /index.php?s=/$1  last;
                }
            }
    
            access_log  /home/wwwlogs/access.log;
    }
    

    服务器2

    server
        {
            listen 80;
            #listen [::]:80 default_server ipv6only=on;
            server_name balance.xxx.com;
            index index.html index.htm index.php admin.php;
            root  /home/wwwroot/default/load;
    
            #error_page   404   /404.html;
            include enable-php-pathinfo.conf;
    
            location /nginx_status
            {
                stub_status on;
                access_log   off;
            }
    
            location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }
    
            location ~ .*.(js|css)?$
            {
                expires      12h;
            }
    
            location ~ /.
            {
                deny all;
            }
    
            location / {
                if (!-e $request_filename) {
                    rewrite  ^(.*)$  /index.php?s=/$1  last;
                }
            }
    
            access_log  /home/wwwlogs/access.log;
    }
    

    配好之后,记得重启nginx。

    增加健康筛选和权重。

    upstream balance.xxx.com
    {
    	server 101.132.185.131:80 weight=1;
    	server 47.91.40.220:80 weight=1; # 权重
    }
    
    server
        {
            listen 80;
            server_name balance.xxx.com;
    
            location /
            {
    		proxy_pass        http://balance.xxx.com;
            	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_next_upstream http_502 http_504 http_404 error timeout invalid_header; # 健康筛选
            }
    
            access_log  /home/wwwlogs/access.log;
    }
    
    

    实验发现,当服务器恢复正常时,负载均衡会自动恢复。

  • 相关阅读:
    github高效搜索使用总结
    使用redis防止商品超发
    yield对性能提升的一次小小测试
    实例直观解释sessionid的作用
    phper必知必会(二)
    phper必知必会(一)
    搭建laravel/homestead虚拟化开发环境
    【博客主题】自用主题备份 (SimpleMemory DIY)
    CentOS 7 配置清华大学EPEL镜像
    CentOS7网络配置-解决虚拟机更改网卡IP不生效问题
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10676085.html
Copyright © 2011-2022 走看看