每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream?bakend?{
server?192.168.0.14?weight=10;
server?192.168.0.15?weight=10;
}
2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream?bakend?{
ip_hash;
server?192.168.0.14:88;
server?192.168.0.15:80;
}
server
{
listen?80;
配置监听端口
server_name?image.***.com;
配置访问域名
location?~*?\.(mp3|exe)$?{
对以“mp3或exe”结尾的地址进行负载均衡
proxy_pass?http://img_relay$request_uri;
设置被代理服务器的端口或套接字,以及URL
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
}
location?/face?{
if?($http_user_agent?~*?"xnp")?{
rewrite?^(.*)$?http://211.151.188.190:8080/face.jpg?redirect;
}
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
error_page?404?502?=?@fetch;
}
location?@fetch?{
access_log?/data/logs/face.log?log404;
rewrite?^(.*)$?http://211.151.188.190:8080/face.jpg?redirect;
}
location?/image?{
if?($http_user_agent?~*?"xnp")?{
rewrite?^(.*)$?http://211.151.188.190:8080/face.jpg?redirect;
}
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
error_page?404?502?=?@fetch;
}
location?@fetch?{
access_log?/data/logs/image.log?log404;
rewrite?^(.*)$?http://211.151.188.190:8080/face.jpg?redirect;
}
}
##其他举例
server
{
listen?80;
server_name?*.***.com?*.***.cn;
location?~*?\.(mp3|exe)$?{
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
}
location?/?{
if?($http_user_agent?~*?"xnp")?{
rewrite?^(.*)$?http://i1.***img.com/help/noimg.gif?redirect;
}
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
#error_page?404?http://i1.***img.com/help/noimg.gif;
error_page?404?502?=?@fetch;
}
location?@fetch?{
access_log?/data/logs/baijiaqi.log?log404;
rewrite?^(.*)$?http://i1.***img.com/help/noimg.gif?redirect;
}
}
server
{
listen?80;
server_name?*.***img.com;
location?~*?\.(mp3|exe)$?{
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
}
location?/?{
if?($http_user_agent?~*?"xnp")?{
rewrite?^(.*)$?http://i1.***img.com/help/noimg.gif;
}
proxy_pass?http://img_relay$request_uri;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
#error_page?404?http://i1.***img.com/help/noimg.gif;
error_page?404?=?@fetch;
}
#access_log?off;
location?@fetch?{
access_log?/data/logs/baijiaqi.log?log404;
rewrite?^(.*)$?http://i1.***img.com/help/noimg.gif?redirect;
}
}
server
{
listen?8080;
server_name?ngx-ha.***img.com;
location?/?{
stub_status?on;
access_log?off;
}
原文:https://www.cnblogs.com/hanby/p/14362652.html