天天看点

Nginx实现反向代理,负载均衡,url重写以及健康检测

一,先对Nginx打补丁,以实现对后台服务器的健康状况进行检测,nginx源码,补丁,nginx的服务控制脚本已经上传到附件。

<a href="http://blog.51cto.com/attachment/201208/185843607.jpg" target="_blank"></a>

注意在实验中前端服务器并没有使用两块网卡,但是在实际应用肯定需要两块网卡,一个内网,一个用于接收外网请求。两台web服务器都需要安装http软件。

#yum  -y  install pcre-devel(nginx的rewrite需要依赖)

#useradd -r nginx -s /sbin/noglogin

# unzip healthcheck_nginx_upstreams.zip 

#tar xf nginx-1.0.14.tar.gz

# cd nginx-1.0.14

#patch  -p1 &lt;/root/cep21-healthcheck_nginx_upstreams-16d6ae7/nginx.patch 

#./configure \

  --prefix=/usr \

  --sbin-path=/usr/sbin/nginx \

  --conf-path=/etc/nginx/nginx.conf \

  --error-log-path=/var/log/nginx/error.log \

  --http-log-path=/var/log/nginx/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/var/tmp/nginx/client/ \

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

  --with-pcre \

  --add-module=/root/cep21-healthcheck_nginx_upstreams-16d6ae7/nginx.patch 

#make &amp;&amp; make install

将服务控制脚本复制到/etc/rc.d/init.d/目录下,并给执行权限chmod +x /etc/rc.d/init.d/nginx。

启动服务并测试nginx能否正常。下面开始实现nginx的各种功能:

二,实现简单反代,但是没有缓存。没有缓存的话只做反代还会降低客户端的响应速度。

Vim  /etc/nginx/nginx.conf

location / {/表示无论客户端请求什么内容都代理到http://17216.150.22

           # root   html;

           # index  index.html index.htm;

           proxy_pass http://172.16.150.22;

   }

三,为反代提供缓存,在配置文件中添加下面

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;

     proxy_cache_path /tmp/nginx/cache       levels=1:2  keys_zone=STATIC:10m

       定义缓存的目录  inactive=24h   max_size=1g;

levels=1:2表示记录的缓存级别的1级目录一个字符,2级目录2个字符)

proxy_temp_path /tmp/nginx/tmp keys_zone表示在内存中将缓存建立索引存储在STATIC这个变量,且最大为10

缓存的临时目录 inactive=24h非活动连接保存24h小时,max_size=1g缓存大保存1g。

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / { 

            proxy_pass http://172.16.150.22;

           proxy_set_header Host  172.16.150.22;

           proxy_cache          STATIC;

           proxy_cache_valid    200 302 1d;为返回码为200,302的请求缓存1天

           proxy_cache_valid    404 1m; 为返回码为404的请求缓存1分钟,404为错误页面

}

每次修改完配置文件的内容需要重启nginx服务或重新载入。不然新的配置不会生效。

查看刚才的levels的效果。

# ll /tmp/nginx/cache/8/a8/bc72520e6a590d2bea21b7264dcafa88 

-rw------- 1 nginx nginx 321 Aug  9 09:15 

  8/a8表示1:2

# cat /tmp/nginx/cache/8/a8/bc72520e6a590d2bea21b7264dcafa88 

四,如果只代理动态页面,则静态页面由nginx自己响应用户。下面的配置当用户

访问http://172.16.150.1/images这个路径则使用后端服务器来响应

location / {

            root   html;

            index  index.html index.htm;

          }

        location /images/ {

           proxy_pass http://172.16.150.22/images;在后台服务器的网页根目录需要创建这个目录

           proxy_cache_valid    200 302 1d;

           proxy_cache_valid    404 1m;

           proxy_cache_use_stale  error timeout invalid_header updating

                                  http_500 http_502 http_503 http_504;

        }

在后台服务器上创建目录与提供页面

# cd /var/www/html/images

# vim index.html

五,如果后端有多台服务器,nginx还可以实现负载均衡

    upstream myhttpd { 对后台服务器设置一个组,这组中添加添加服务器

        server 172.16.150.22 weight=5; weight=5表示设置服务器的权重

        server 172.16.150.30 weight=5;

#charset koi8-r;

#access_log  logs/host.access.log  main;

#   location / {

        #    root   html;

        #    index  index.html index.htm;

        #  }

        location / {

           proxy_pass http://myhttpd;

        #   proxy_cache         STATIC;如果不注释下,看不到效果

        #   proxy_cache_valid   200 302 1d;

        #   proxy_cache_valid    404 1m;

           #proxy_cache_use_stale  error timeout invalid_header updating

        #                         http_500 http_502 http_503 http_504;

六,实现URL重写

# upstream myhttpd {

        #server 172.16.150.22 weight=5;

        #server 172.16.150.30 weight=5;

        #}

            if (!-f $request_filename) {

                rewrite /.* /err.html permanent; (permanent表示永久的重定向到这个页面)

*表示任意页面,如果访问不存在的任意网页都重定向到http://172.16.150.21/err.html

err.html这个页面需要存在。

        #location / {

        #   proxy_pass http://myhttpd;

        #   proxy_cache         STATIC;

        #  proxy_cache_use_stale  error timeout invalid_header updating

        #   http_500 http_502 http_503 http_504;

为nginx提供错误页面。

vim /usr/html/err.html

为某个目录定义别名,用户访问的路径其实并不存在,而是将其转发到另外一个页面。

     location / {

            index  index.html ;

            rewrite ^/forum/(.*) /bbs/$1 last ;

        #    http_500 http_502 http_503 http_504;

 如果用户访问http://172.16.150.21/forum/则被http://172.16.150.21/bbs

# mkdir /usr/html/bbs 创建bbs路径,并提供测试面

# vim /usr/html/bbs/index.html

七:实现域名跳转

server {

        server_name  test.magedu.com; 

如果访问test.magedu.com则跳转到tomcat.magedu.com

        rewrite ^/.* http://tomcat.magedu.com/$1 last;

 #charset koi8-r;

 location / {

八,检查后端服务器健康状态

upstream myhttpd {

        server 172.16.150.22 weight=5;

        healthcheck_enabled; 开启健康检查功能

        healthcheck_delay 1000;   

对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为1000。                healthcheck_timeout 1000;

进行一次健康检测的超时时间,单位为毫秒,默认值2000。

        healthcheck_failcount 3;

对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器

        healthcheck_send "GET/.health HTTP/1.0";

对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器,只支持http 1.0的协议

        #server_name  test.magedu.com;

        #rewrite ^/.* http://tomcat.magedu.com/$1 last;

# location / {

        #     root   html;

        #     index  index.html ;

location /stat {

        healthcheck_status;

访问172.16.150.21/stat可以看到后台服务器的运行是否正常 

<a href="http://down.51cto.com/data/2361226" target="_blank">附件:http://down.51cto.com/data/2361226</a>

本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/960545,如需转载请自行联系原作者

继续阅读