Nginx、haproxy反向代理设置

时间:2022-06-15 20:27:28

Nginx反向代理配置:

#user  nobody;
worker_processes ; events {
worker_connections ;
} 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 ;
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
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:
default_backend servers
backend servers
balance roundrobin #负载均衡模式轮询
server server1 192.168.1.102: cookie A check
server server2 192.168.1.104: 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;
}
}
}