Nginx、haproxy反向代理設定

石曼迪發表於2017-04-01

Nginx反向代理配置:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;


upstream lingyu.com { 
      server  192.168.1.102; 
      server  192.168.1.104;
      #ip_hash; 
} 
    server {
        listen       800;
        server_name  localhost;
  
        location / {
             proxy_pass http://lingyu.com;
             proxy_set_header   Host             $host;  
             proxy_set_header   X-Real-IP        $remote_addr;  
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
             proxy_set_header   Cookie $http_cookie;
        }
    }
}

haproxy反向代理設定:

 

global
     daemon 
     maxconn 25600
 defaults
     mode http 
     timeout connect 5000ms 
     timeout client 50000ms 
     timeout server 50000ms
 frontend http-in
     bind *:800 
     default_backend servers
 backend servers
     balance     roundrobin #負載均衡模式輪詢
     server server1 192.168.1.102:80 cookie A check
     server server2 192.168.1.104:80 cookie B check

寫入Cookies時帶上Domain就可以互相識別。

補充:

由於伺服器資源缺乏,案例如下:

2套系統和若干個服務,要做負載均衡,只能將2套系統在2個伺服器上都部署一份,一個用80埠,一個用81埠,不對外開放

第三個伺服器做分流,同時再承擔一些其他的外網服務角色。

設計如:

網站,用80埠,baidu.com

後臺用81埠,home.baidu.com

將上述2個域名同時指到第三個公開的伺服器,用nginx根據埠分流。

配置如:

worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream baidu.com { 
      server  192.168.1.150; 
      server  192.168.1.151;
      #ip_hash; 
} 

upstream home.baidu.com { 
      server  192.168.1.150:81; 
      server  192.168.1.151:81;
      #ip_hash; 
} 


    server {
        listen       80;
        server_name  baidu.com;
          location / {
			 proxy_pass http://baidu.com;
             proxy_set_header   Host             $host;  
             proxy_set_header   X-Real-IP        $remote_addr;  
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
			 proxy_set_header   Cookie $http_cookie;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
		
	
	server {
        listen       80;
        server_name  home.baidu.com;
        location / {
			 proxy_pass http://home.baidu.com;
             proxy_set_header   Host             $host;  
             proxy_set_header    X-Real-IP        $remote_addr;  
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
			 proxy_set_header   Cookie $http_cookie;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

相關文章