Nginx+keepalived+3 a site

Source: Internet
Author: User

A deployment environment:

master:192.168.127.129

slave:192.168.127.130

VIP:192.168.127.100 (Requires public IP in the real world)

There are three sites on each of the two servers, Web,h5,app


Nginx and keepalived installation procedure omitted


Two master configuration file settings

1 Master Load Balancer Configuration

[[email protected] vhosts]# vi ld.conf upstream www{      server 192.168.127.129:81;     server 192.168.127.130:81 ;ip_ hash;} server {     listen 81;     server_name   www.ihnhlife.com;     location / {        proxy_pass http://www/;       proxy_set_header host   $host;   }}## s.ihnhlife.com  app End upstream s{      Server 192.168.127.129:83 ;     server 192.168.127.130:83 ;ip_ hash;} server {     listen 83;     server_name   s.ihnhlife.com;     location / {        proxy_pass http://s/;       proxy_set_header host  $host;   }}upstream  h5{     server 192.168.127.129:82;     server  192.168.127.130:82;     ip_hash;} server {     listen 82;     server_name   m.ihnhlife.com;     location / {        proxy_pass http://h5/;       proxy_set_header host $ host;  }}


2 Master 3-site configuration files:

[[Email protected] vhosts]# cat m.ihnhlife.com.confserver{    listen  192.168.127.129:82;    server_name localhost;    index  index.html index.htm;    root /data/wwwroot/m.ihnhlife.com/;} [[Email protected] vhosts]# cat s.ihnhlife.com.confserver{    listen  192.168.127.129:83;    server_name localhost;    index  index.html index.htm;    root /data/wwwroot/www.ihnhlife.com/;  location / {            proxy_pass  Http://192.168.127.129:8080;            proxy_set_ header host   $host;             Proxy_set_header x-forwarded-for   $proxy _add_x_forwarded_for;            proxy_set _header x-real-ip   $remote _addr;}} server{    listen 192.168.127.129:81;    server_name  Localhost;    index index.html index.htm;    root /data /wwwroot/www.ihnhlife.com/;}


3 Master Three site root configuration (/data/wwwroot)


[Email protected] wwwroot]# lsm.ihnhlife.com s.ihnhlife.com www.ihnhlife.com[[email protected] m.ihnhlife.com]# cat in dex.html 129m.inhnlife.com[[email protected] s.ihnhlife.com]# cat index.html129 s.inhnlife.com[[email protected] www.ihnhlife.com]# Cat index.html 129 www.ihnhlife.com

4 Master keepalived Configuration


global_defs {   notification_email {     [email  protected]   }   notification_email_from [email protected]    smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id  LTT}vrrp_script chk_nginx {   #检测nginx服务是否在运行有很多方式, such as process, script detection, etc.     script  "Killall -0 nginx"    #用shell命令检查nginx服务是否存在    interval 1    #时间间隔为1秒检测一次    weight -2    #当nginx的服务不存在了, -2   the current weight  fall 2       #测试失败的次数    rise 1        #测试成功的次数}vrrp_instance in_1 {    state master     interface eth0    virtual_router_id 22     Priority 100    advert_int 1    authentication {         auth_type PASS        auth_pass aaaa     }    virtual_ipaddress {         192.168.127.100    }   track_script {    chk_nginx     #引用上面的vrrp_script定义的脚本名称}}


5 Master Nginx main configuration file:

User nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log  crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;        #并发数设置events {    use epoll;    worker_connections  6000;    #单个后台worker The maximum number of concurrent links for  process processes}http{    include  Mime.types;    default_type application/octet-stream;    server_ names_hash_bucket_size 3526;    server_names_hash_max_size 4096;     log_format combined_realip  ' $remote _addr$http_x_forwarded_for[$time _local] '      (Log_format is the name of the log can be customized)      ' $host   ' $request _uri '   $status '       "$http _referer"   "$http _user_agent" ';    sendfile on;     tcp_nopush on;    keepalive_timeout 30;    client_header_timeout 3m;     client_body_timeout 3m;    send_timeout 3m;     connection_pool_size 256;    client_header_buffer_size 1k;     large_client_header_buffers 8 4k;    request_pool_size 4k;     output_buffers 4 32k;    postpone_output 1460;     client_max_body_size 10m;    client_body_buffer_size 256k;     client_body_temp_path /usr/local/nginx/client_body_temp;     proxy_temp_path /usr/local/nginx/proxy_temp;    fastcgi_temp_path /usr/local/ Nginx/fastcgi_temp;    fastcgi_intercept_errors on;    tcp_ Nodelay on;    gzip on;    gzip_min_length 1k;    gzip_buffers 4  8k;    gzip_comp_level 5;    gzip_http_version 1.1;     gzip_types text/plain application/x-javascript text/css text/htm  application/xml;    include vhosts/*.conf;

Three slave configuration files:

Slave In addition to the configuration of keepalvied, the rest are the same, copy one can (3 site content change, in order to facilitate identification)


Configuration of the keepalived:

Vim keepalived.conf #此配置文件是从Master服务器上copy过来, just a small change to the state BACKUP #把这里原先的MASTER改成BACKUPpriority #把这里原先的100改成99


Four-start service and test


Finally, the Nginx and keepalived services are launched on both hosts respectively.

1 Stop the Nginx service above the master to see if 3 sites can be accessed normally

2 Stop the Keepalvied service above the master to see if 3 sites can be accessed normally



This article is from the "Drifting Away" blog, make sure to keep this source http://825536458.blog.51cto.com/4417836/1846404

Nginx+keepalived+3 a site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.