CentOS/Redhat:安装Nginx作为反向代理负载均衡器

时间:2020-01-09 10:43:26  来源:igfitidea点击:

如何在CentOS/RHEL 5.x下的两个Apache Web服务器之前将nginx配置为故障转移反向代理负载平衡器?

nginx是Web和反向代理服务器。

Nginx用于Apache Web服务器。
来自Internet的,指向其中一台Web服务器的所有连接都通过Nginx代理服务器路由,该服务器可以处理请求本身,也可以将请求全部或者部分传递给主Web服务器。

我们的样品设置

Internet-
         |
    =============                               |---- apache1 (192.168.1.15)
    | ISP Router|                               |   
    =============                               |---- apache2 (192.168.1.16)
         |                                      | 
         |                                      |---- db1 (192.168.1.17)
         |      |eth0 -> 192.168.1.11 ----------/
         |-lb0==|                        /
         |      |eth1 -> 192.54.1.1 ----/
         |
         |      |eth0 -> 192.168.1.10 ----------\
         |-lb1==|                        /      |---- apache1 (192.168.1.15)
                |eth1 -> 192.54.1.1 ----/       |
                                                |---- apache2 (192.168.1.16)
                                                |
                                                |---- db1 (192.168.1.17)

其中:

  • lb0 Linux盒通过eth1直接连接到Internet。这是主负载均衡器。
  • lb1 Linux盒通过eth1直接连接到Internet。这是备用负载平衡器。如果主控网络失败,它将变为活动状态。
  • 192.54.1.1该IP在lb0和lb1服务器之间移动。它称为虚拟IP地址,由keepalived管理。
  • eth0连接到LAN,所有其他后端软件服务器都通过eth0连接。
  • Nginx同时安装在lb0和lb1上。它将收听192.54.1.1。您需要将nginx配置为反向代理服务器。它将连接到Apache1和Apache2。
  • 在Apache#1和Apache#2服务器上安装httpd服务器。将它们配置为侦听192.168.1.15:80和192.168.1.16:80。不要将公共IP分配给此框。仅通过局域网激活eth0。
  • 在Db#1上安装MySQL/Oracle/PgSQL服务器。配置数据库服务器以侦听192.168.1.17:$db_server_port。不要将公共IP分配给此框。仅通过局域网激活eth0。

简而言之,您需要以下硬件:

  • 2个负载平衡器反向代理服务器(250GB SATA,2GB RAM,具有RHEL 64位+ keepalived + nginx的单个Intel P-D930或者AMD 170s)
  • 2个Apache Web服务器(软件RAID-1,SCSI-73GBx2 15k磁盘,6GB RAM,双Intel Xeon或者AMD RHEL 64位+ Apache 2 64位CPU)
  • 1个备用Apache Web服务器(软件RAID-1,SCSI-73GBx2 15k磁盘,6GB RAM,双Intel Xeon或者带有RHEL 64位+ Apache 2的AMD 64位CPU)
  • 1个数据库服务器(RAID-10,SCSI-73GBx4 15k磁盘,16GB RAM,双Intel Xeon或者AMD 64位CPU和RHEL 64位+ MySQL 5)
  • 1个缓存服务器(RAID-1,SCSI-73GBx2 15k磁盘,8GB RAM,双Intel Xeon或者带有RHEL 64位的AMD 64位CPU)
  • 1个异地备份服务器(RAID-6、1TB SATAx4、4GB RAM,具有RHEL 64位的单个Intel/AMD CPU)
  • 根据要求从数据库,存储,pop3和SMTP服务器。
  • Internet上行链路100Mbps +或者根据要求。

从lb0和lb1删除不需要的软件

执行以下命令:

# yum -y groupremove "X Window System"
# x=$(yum list installed | egrep -i 'php|httpd|mysql|bind|dhclient|tftp|inetd|xinetd|ypserv|telnet-server|rsh-server|vsftpd|tcsh' | awk '{ print }')
# yum -y remove $x
# yum -y install bind-utils sysstat openssl-devel.x86_64 pcre-devel.x86_64 openssl097a.x86_64
# /usr/sbin/authconfig --passalgo=sha512 --update
# passwd root

上面的内容将从lb0和lb1中删除X窗口和其他不需要的软件。

在lb0和lb1上都安装Nginx

输入以下命令以下载nginx,执行:

# cd /opt
# wget http://sysoev.ru/nginx/nginx-0.8.33.tar.gz

解压缩nginx,执行:

# tar -zxvf nginx-0.8.33.tar.gz
# cd nginx-0.8.33

为64位RHEL/CentOS Linux配置nginx:

# ./configure --without-http_autoindex_module --without-http_ssi_module --without-http_userid_module --without-http_auth_basic_module --without-http_geo_module --without-http_fastcgi_module --without-http_empty_gif_module --with-openssl=/lib64

输出示例:

....
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
...

安装相同:

# make
# make install

创建nginx用户帐户

执行以下命令来创建用户帐户:

# useradd -s /sbin/nologin -d /usr/local/nginx/html -M nginx
# passwd -l nginx

在lb0和lb1上将nginx配置为反向代理负载均衡器

编辑/usr/local/nginx/conf/nginx.conf,执行:

# vi /usr/local/nginx/conf/nginx.conf

如下更新:

pid               logs/nginx.pid;
user              nginx nginx;
worker_processes  10;
 
events {
    worker_connections  1024;
}
 
http {
  default_type       application/octet-stream;
 
## Common options ##
include options.conf;
 
## Proxy settings ##
include proxy.conf;
 
## lb domains ##
include theitroad.com.conf;
}

编辑/usr/local/nginx/conf/options.conf,执行:

# vi /usr/local/nginx/conf/options.conf

如下更新:

## Size Limits
  client_body_buffer_size     128K;
  client_header_buffer_size   1M;
  client_max_body_size          1M;
  large_client_header_buffers 8 8k;
 
## Timeouts
  client_body_timeout   60;
  client_header_timeout 60;
  expires               24h;
  keepalive_timeout     60 60;
  send_timeout          60;
 
## General Options
  ignore_invalid_headers   on;
  keepalive_requests      100;
  limit_zone gulag $binary_remote_addr 5m;
  recursive_error_pages    on;
  sendfile                 on;
  server_name_in_redirect off;
  server_tokens           off;
 
## TCP options
  tcp_nodelay on;
  tcp_nopush  on;
 
## Compression
  gzip              on;
  gzip_buffers      16 8k;
  gzip_comp_level   6;
  gzip_http_version 1.0;
  gzip_min_length   0;
  gzip_types        text/plain text/css image/x-icon application/x-perl application/x-httpd-cgi;
  gzip_vary         on;
 
## Log Format
  log_format  main  '$remote_addr $host $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
                    '"$gzip_ratio"';

编辑/usr/local/nginx/conf/proxy.conf,执行:

## Proxy caching options
  proxy_buffering           on;
  proxy_cache_min_uses       3;
  proxy_cache_path          /usr/local/nginx/proxy_temp/ levels=1:2 keys_zone=cache:10m inactive=10m max_size=1000M;
  proxy_cache_valid         any 10m;
  proxy_ignore_client_abort off;
  proxy_intercept_errors    on;
  proxy_next_upstream       error timeout invalid_header;
  proxy_redirect            off;
  proxy_set_header          X-Forwarded-For $remote_addr;
  proxy_connect_timeout     60;
  proxy_send_timeout        60;
  proxy_read_timeout        60;

编辑/usr/local/nginx/conf/theitroad.com.conf,执行:

## Connect to backend servers via LAN ##
## Reverse Proxy Load Balancer Logic ##
upstream theitroad  {
      server 192.168.1.15 weight=10 max_fails=3 fail_timeout=30s;
      server 192.168.1.16 weight=10 max_fails=3 fail_timeout=30s;
      # only comes alive when above two fails      
      server 192.168.1.23 weight=1 backup;
}
 
server {
      access_log  logs/access.log main;
      error_log   logs/error.log;
      index       index.html;
      root        /usr/local/nginx/html;
      server_name theitroad.com www.theitroad.com subdomain.theitroad.com;
 
     ## Only requests to our Host are allowed
      if ($host !~ ^(theitroad.com|www.theitroad.com|subdomain.theitroad.com)$ ) {
         return 444;
      }
 
     ## redirect www to nowww
     # if ($host = 'www.theitroad.com' ) {
     #    rewrite  ^/(.*)$  http://theitroad.com/  permanent;
     # }
 
     ## Only allow these request methods
     if ($request_method !~ ^(GET|HEAD|POST)$ ) {
         return 444;
     }
 
     ## PROXY - Web
      location / {
        proxy_pass  http://theitroad;
        proxy_cache            cache;
        proxy_cache_valid      200 24h;
        proxy_cache_use_stale  error timeout invalid_header updating http_500 http_502 http_503 http_504;
        proxy_ignore_headers   Expires Cache-Control;
 
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      }
 
     # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

启动Nginx Web服务器:

# /usr/local/nginx/sbin/nginx
# netstat -tulpn | grep :80
# echo ' /usr/local/nginx/sbin/nginx' >> /etc/rc.local

打开浏览器并输入域名,例如theitroad.com:

http://theitroad.com