ngnix基於二級域名的負載均衡(兩臺server)

有一個域名 32.com ,然後自己解析時可能會有,www.32.com,lxp32.com
兩臺server :server1(192.168.2.32) server2(192.168.2.40), 在server1上配置負載均衡,
思路,我們要配置負載均衡時,對外的時80端口,那麼配置裏邊的server,就不能 listen 80端口了,就得改端口,兩個server上同一個服務監聽的端口要一樣,比如www.32.com 都要建通82端口,那麼在server1 上和server2上都要listen 82 端口,配置如下

 server {
        listen       192.168.2.32:82;
        server_name  www.32.com;
        root         /usr/share/nginx/html/www;
        include /etc/nginx/default.d/*.conf;
    server {
        listen       192.168.2.40:82;
        server_name  www.32.com;
        root         /usr/share/nginx/html/www;

   server {
        listen       192.168.2.32:83;
        server_name  lxp.32.com;
        root         /usr/share/nginx/html;
  server {
        listen       192.168.2.40:83;

        server_name  lxp.32.com;
        root         /usr/share/nginx/html;

第二步:配置upstream 在server1(192.168.2.32)中。

www 的佔用82端口,
upstream www{
server 192.168.2.32:82;    #最好寫ip地址,不寫域名
server 192.168.2.40:82;
  }

lxp佔用83端口。
upstream lxp {
server 192.168.2.32:83;
server 192.168.2.40:83;
}

第三步:配置server

server {
    listen 192.168.2.32:80;  #對外的是80端口
    server_name  ~^(.+)?.32.com;    #是匹配所有的32.com的域名
    index index.html index.php;

     location  /  {
     if ($host ~* www) {         #以www開頭的主機proxy_pass到www這個upstream。
     proxy_pass http://www;
     }
     if ($host ~* lxp) {         #以lxp開頭的主機proxy_pass到lxp這個upstream。
     proxy_pass http://lxp;
     }
     root /usr/share/nginx/html/$1;  #定義發佈目錄,$1取得是主機名,這是域名在同一目錄下,要是不在統一目錄下的話,就用多了location匹配,每個location指定一個root就行了,比較麻煩。
}
}

第四步:
就是將server2的一些配置要與server1的相同了,比如
server_name,
端口號(一個主機名要一致),
還有root,同一個主機名,比如都是lxp.32.com root都需要一樣,其次目錄裏邊的東西要一樣。

下邊是server1和server2的所有配置:
server1:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

upstream www{
server 192.168.2.32:82;
server 192.168.2.40:82;
  }

upstream lxp {
server 192.168.2.32:83;
server 192.168.2.40:83;
}

 server {
    listen 192.168.2.32:80; 
    server_name  ~^(.+)?.32.com;
    index index.html index.php; 

     location  /  {
     if ($host ~* www) {
     proxy_pass http://www;
     }
     if ($host ~* lxp) {
     proxy_pass http://lxp;
     }
    root /usr/share/nginx/html/$1;
}
}

 server {
        listen       192.168.2.32:82;
        server_name  www.32.com;
        root         /usr/share/nginx/html/www;
        include /etc/nginx/default.d/*.conf;

    location / {
        root         /usr/share/nginx/html/www;
        index         index.php index.html;
        }
    location ~ \.php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
   }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

lxp.conf 的配置文件

[root@www ~]# cat /etc/nginx/conf.d/lxp.conf 
    server {
        listen       192.168.2.32:83;
        server_name  lxp.32.com;
        root         /usr/share/nginx/html/lxp;

      #  include /etc/nginx/default.d/*.conf;

        location /  {
        root  /usr/share/nginx/html/lxp;
        index index.html index.htm index.php;

          }

    location ~ \.php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
   }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

server2的配置

www主機的配置:
    server {
        listen       192.168.2.40:82;
        server_name  www.32.com;
        root         /usr/share/nginx/html/www;

        include /etc/nginx/default.d/*.conf;

        location / {
        root         /usr/share/nginx/html/www;
        index         index.php index.html;
        }

             location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
            }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

lxp主機的配置
[root@www ~]# cat /etc/nginx/conf.d/lxp.conf 
    server {
        listen       192.168.2.40:83;

        server_name  lxp.32.com;
        root         /usr/share/nginx/html;

      #  include /etc/nginx/default.d/*.conf;

        location /  {
        root  /usr/share/nginx/html/lxp;
        index index.html index.htm index.php;

          }

    location ~ \.php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
   }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章