FreeBSD 8.1下用nginx配置简单web负载均衡
- 摘要:本文将介绍如何利用nginx配置简单的web负载均衡。文中所用的平台是64bit的FreeBSD 8.1。另外,由于Nginx负载均衡器采用ip_hash来代替默认的rr方式,虽然解决了session问题,却无法保证后端服务器的负载均衡。本文对session问题的解决方案也进行了介绍。
- 标签:FreeBSD 8.1 nginx 负载均衡
cd /usr/ports/www/nginx make install clean
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webserver { server 192.168.21.45 weight=1; server 192.168.4.45 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webserver; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4 32k; proxy_temp_file_write_size 64k; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/www/nginx-dist; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
worker_processes 4; #这个与服务器的核数等同即可 upstream #这个里面的weight值越大,服务器所要承担的压力也越大;如果采用ip_hash模块时,就不能分配weight了,不然Nginx启动时会报错 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #以上配置是让后端的web服务器可以通过X-Forwarded-For获取客户端的真实IP #允许客户端请求的最大的单个文件字节数 client_max_body_size 10m; #缓冲区代理缓冲用户端请求的最大字节数 可以理解为先保存到本地再传给用户 client_body_buffer_size 128k; #跟后端服务器连接的超时时间_发起握手等候响应超时时间 proxy_connect_timeout 90; #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理 proxy_read_timeout 90; #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据 proxy_send_timeout 90; #代理请求缓存区_这个缓存区间会保存用户的头信息以供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;
下一篇:沙溢胡可“奉子成婚”