nginx反向代理和负载均衡
- nginx反向代理和负载均衡
vim /usr/local/nginx/conf/vhosts/proxy.conf upstream XXXXX{ server 61.135.169.125:80; server 61.135.169.121:80; } server{ listen 80; server_name www.baidu.com; location / { proxy_pass http://XXXXX/; proxy_set_header Host $host; #多个ip加上这行,这里可以固定写死为服务端的域名 proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /bbs/ { proxy_pass http://xxxxxxx/; #访问到后端根 } location /bbs/ { proxy_pass http://xxxxxxx/bbs/; #访问到后端bbs } location ~* ^.*\.(jpg|gif) { proxy_pass http://xxxxxxx; #这种模糊匹配 代理上不能跟url,不然会提示语法错误 } }
代理tcp
stream { upstream cloudsocket { hash $remote_addr consistent; # $binary_remote_addr; server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s; } server { listen 3306;#数据库服务器监听端口 proxy_connect_timeout 10s; proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。 proxy_pass cloudsocket; } }