centos7.2 nginx负载均衡配置

多年没有搞负载均衡了,之前学习的时候在虚拟机搞过,后面小公司没有搞过,大公司有专业运维,所以一直没有机会真正的做一下。最近闲来无事,而且还有一台香港的云服务器闲来无用,想着有个项目部署在成都的服务器上,那就给他们搞个负载均衡吧。

 

一、开始配置

  • 在自己服务器增加一个nginx配置文件

cd /usr/local/nginx/conf/vhost
touch xxxx.conf
  • 在配置文件增加负载均衡代码

vi xxxx.conf
upstream test{
        server 132.232.xx.xx:10110;#xx改成自己的ip
        server 150.109.xx.xx:10110 backup; #备份
}
server {
    listen 80;
    server_name admin.lllllddddd.com;#改成自己的域名
    location / {
            proxy_pass http://test;#此名称是upstream的名称
            proxy_set_header Host   $proxy_host;
            proxy_connect_timeout 2s;
    }
}
#注意,下面不要再增加其他nginx的location配置,不然是不会走负载均衡的
  • 在上面配置的服务器132.232.xx.xx增加nginx配置文件

vi 132.232.xx.xx.conf
server {
    listen 10110;
    server_name localhost; #此处需要用location
    root /www/ld/public/admin;

    charset utf-8;
    index index.html index.htm index.php default.html default.htm default.php;

    location / {
            if (-f $request_filename/index.php){
               rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                #rewrite (.*) /index.php;
                rewrite ^(.+)$ /index.php?s=$1;
            }
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    error_log  /www/log/nginx/api.ld.com-error.log error;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;#取决于php-fpm的设置
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;#指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息,off表示关闭
        fastcgi_param MY_ENV release;
    }

    sendfile off;#指定是否使用sendfile系统调用来传输文件
    client_max_body_size 20m;#允许上传文件的大小

    access_log off;

    #禁用htaccess
    location ~ /\.ht {
        deny all;
    }
}

 

  • 在服务器150.109.xx.xx 增加如下nginx配置文件

vi 150.109.xx.xx.conf
server {
    listen 10110;
    server_name localhost;#此处需要用location
    root /www/ld/public/admin;

    charset utf-8;
    index index.html index.htm index.php default.html default.htm default.php;

    location / {
            if (-f $request_filename/index.php){
               rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                #rewrite (.*) /index.php;
                rewrite ^(.+)$ /index.php?s=$1;
            }
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    error_log  /www/log/nginx/admin.ld.com-error.log error;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;#取决于php-fpm的设置
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;#指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息,off表示关闭
        fastcgi_param MY_ENV release;
    }

    sendfile off;#指定是否使用sendfile系统调用来传输文件
    client_max_body_size 20m;#允许上传文件的大小

    access_log off;

    #禁用htaccess
    location ~ /\.ht {
        deny all;
    }
}

 

  • 重启两台服务器的nginx,然后就可以测试了

 

二、upstream配置参数介绍

 

参考:https://blog.csdn.net/caijunsen/article/details/83002219

三、采坑记录

  • 在负载均衡配置文件上有其他location,导致负载均衡一直没有生效。

解决方法:去掉其他的location

  • 负载到的服务器,采用的是docker配置的,想到于   负载到B服务器-》B服务器上的nginx Proxy代理容器-》代理容器也是采用nginx代理,代理的业务容器中。

解决方法:此时的坑不懂是为啥,但是就是没有用,可能是自己哪里配置错误,改成不用docker容器,直接在服务器部署php环境。有知道的大牛可以评论里告知一下

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章