Nginx簡單負載均衡配置

Nginx("engine x")是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Nginx是由Igor Sysoev爲俄羅斯訪問量第二的Rambler.ru站點開發的,它已經在該站點運行超過兩年半了。Igor將源代碼以類BSD許可證的形式發佈。
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;
      }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章