Nginx简单负载均衡配置

分类:CentOS教程 阅读:82987 次

Nginx("engine x")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,它已经在该站点运行超过两年半了。Igor将源代码以类BSD许可证的形式发布。 Nginx的中文维基:[url]http://wiki.codemongers.com/NginxChs[/url] 1、安装pcre库
tar jxvf pcre-7.9.tar.bz2
cd pcre-7.9/
./configure
make
make install 2、安装nginx
tar zxvf nginx-0.6.36.tar.gz
cd nginx-0.6.36/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install 3、运行nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 4、配置nginx反向代理
loading.abc.com和loading.xyz.com域名均指向nginx所在的服务器IP,用户访问[url]http://loading.abc.com[/url],将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。用户访问[url]http://loading.xyz.com[/url],将其负 载均衡到192.168.1.7服务器的8080、8081、8082端口。 以下为配置文件nginx.conf: user www www; worker_processes 10; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; #最大文件描述符
worker_rlimit_nofile 51200; events {
use epoll;
worker_connections 51200;
} http {
include conf/mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on; upstream abc {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
} upstream xyz {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
} server {
listen 80;
server_name loading.abc.com;
location / {
proxy_pass[url]http://abc[/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;
} log_format abc '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /www/logs/abc.log abc;
} server {
listen 80;
server_name loading.xyz.com;
location / {
proxy_pass[url]http://xyz[/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;
} log_format xyz '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /www/logs/xyz.log xyz;
}
}