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環境。有知道的大牛可以評論裏告知一下

 

 

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