nginx反向代理及负载均衡方式和配置
做proxy的配置文件(访问本地和远程服务器,本地后端用apache运行在8080)
user www www;
worker_processes 8;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
charset gbk;
server_names_hash_bucket_size 128;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /dev/shm;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/html application/xml;
access_log off;
client_max_body_size 12m;
#max_execution_time = 800;
#post_max_size = 20M;
#upload_max_filesize = 20M;
send_timeout 60;
upstream bbs_xxx_com {
server 127.0.0.1:8080 weight=3;
server 60.18.xxx.xxx:80;
}
server
{
listen 80;
server_name bbs.xxx.com;
proxy_redirect off;
charset gbk;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://bbs_xxx_com;
}
#定义日志格式
log_format access ‘$remote_addr - $remote_user [$time_local] $request ‘
‘”$status” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
#打日志
access_log /usr/local/nginx/logs/access.log access;
#允许客户端请求的最大的单个文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数 可以理解为先保存到本地再传给用户
client_body_buffer_size 128k;
#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 600;
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
proxy_read_timeout 600;
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_send_timeout 600;
#代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
proxy_buffer_size 8k;
#同上 告诉Nginx保存单个用的几个Buffer 最大用多大空间
proxy_buffers 4 32k;
#如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
proxy_busy_buffers_size 64k;
#proxy缓存临时文件的大小
proxy_temp_file_write_size 64k;
}
}
nginx的upstream目前支持负载均衡方式的分配:
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream tomcats {
server 10.1.1.107:88 weight=10;
server 10.1.1.132:80 weight=10;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream tomcats {
ip_hash;
server 10.1.1.107:88;
server 10.1.1.132:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
nginx本身是不支持url_hash的,需要打第三方的patch,下载地址http://anotherbug.blog.chinajavaworld.com/servlet/AttachmentServlet/download/123297-4298-2148/nginx_upstream_hash-0[1].2.tar.gz
See http://wiki.codemongers.com/NginxHttpUpstreamRequestHashModule for more details.
首先进到nginx的目录
cd nginx-0.5.21
patch -p0 < /path/to/this/directory/nginx-0.5.21.patch
./configure --add-module=/path/to/this/directory
make && make install
我的测试环境vmvare 模拟3个linux
ip分别:nginx 192.168.1.49
WEB1:192.168.1.50
WEB2:192.168.1.51
测试网站我反向代理192.168.1.49
这样nginx上编译完成需要配置,内容如下
http {
upstream 192.168.1.49{
server 192.168.1.50;
server 192.168.1.51;
hash $request_uri;
}
server {
listen 80;
server_name 192.168.1.49;
location / {
proxy_pass http://192.168.1.49;
}
}
然后启动nginx

浙公网安备 33010602011771号