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 {
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章