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;
}

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

posted @ 2019-04-09 12:02  TBHacker  阅读(358)  评论(0编辑  收藏  举报