nginx反向代理

Nginx代理与负载均衡配置与优化

 Nginx代理

 Nginx0.7.48版本开始,支持了类似Squid的缓存功能。NginxWeb缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

 Nginx 0.8.32版本,proxy_cachefastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid

 在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为负载均衡服务器“Web缓存服务器来使用。

 下面的文档说明了nginx如何做代理服务器,将请求转发到其他服务器,本身不做缓存。使用版本为nginx-0.8.15,配置如下:

 http

{

……..

     client_max_body_size           300m          ;                  // 允许客户端请求的最大单个文件字节数

         client_body_buffer_size       128k;         

         // 缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户

         proxy_connect_timeout          600;

    // 跟后端服务器连接的超时时间_发起握手等候响应超时时间

    // 连接成功后_等候后端服务器响应时间_其实已经进入后端排队之中等候处理

  1. 告诉nginx保存单个用的几个buffer最大用多大空间
 

// proxy缓存临时文件的大小

        server 192.168.0.110:80 weight=5;

        server 192.168.0.121:80 weight=5;

    }

    upstream mysrv {

        server 192.168.0.32:80 weight=2;

        server 127.0.0.1:8000 weight=8;

    }

    server {

        listen       80;

        server_name  club.xywy.com;

        charset gbk;

        root  /www;

        access_log logs/aaa.log combined;

//下面是第一个域名,使用clubsrv的代理

        location / {

            proxy_next_upstream http_502 http_504 error timeout invalid_header;

// 如果后端服务器返回502504或执行超时等错误,自动将请求转发到upstream另一台服务器

            proxy_pass  http://clubsrv;>

// 与上面upstream自己命名的名字填写一致

            proxy_redirect     off;

            proxy_set_header   Host            club.xywy.com;

            proxy_set_header   X-Real-IP        $remote_addr;

            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

// nginx在前端做代理,后端的日志会显示127.0.0.1,上面配置可以显示用户真实IP(还需装第三方软件,见下面的详细说明)

            index  index.htm index.html index.php;

        }

//下面是第二个域名,使用mysrv的代理,访问www.sum.com/message目录下的

    server {

        listen       80;

        server_name  www.sum.com;

        location /message {

           proxy_pass  http://mysrv;>

           proxy_set_header   Host            $host;

// 访问这个域名的,只有mysrv 本机可以访问

          }

//访问除了/message之外的www.sum.com/ 地址,

        location / {

           proxy_pass  http://mysrv;>

           proxy_set_header   Host            $host;

                     proxy_set_header   X-Real-IP       $remote_addr;

下面的配置,与上面错误返回的效果相同,这里可以不写。

 

error_page   500 502 503 504  /50x.html;  

location = /50x.html

{

   root   html;

}

 

2Nginx负载均衡指令 

Nginx属于软件的七层负载均衡(lvs是软件的四层负载均衡的代表),七层负载均衡软件还有L7SWLayer7 switching)、HAProxy等。支持负载均衡的模块是Http Upstream。下面介绍此模块及他下面的几个指令 

HTTP Upstream模块

 1ip_hash指令 

当对后端的多台动态应用服务器做负载均衡时,ip_hash指令将某个客户端IP的请求通过哈希算法定位到同一台后端服务器上。这样,当来自某ip用户在Sever A上登录后,再访问该站点的其他URL时,能保证访问仍在Server A上。如果不加ip_hash,加入用户在Server A上登录,再访问该站点其他URL,就有可能跳转到后端的Sever BC…..,而session记录在A上,BC上没有,就会提示用户未登录。

注意:但这种访问不能保证后端服务器的负载均衡,可能后端有些server接受到的请求多,有些server接受的少,设置的权重值不起作用。

建议如果后端的动态应用程序服务器能做到session共享,而不用nginx上配置ip_hash的方式。

 

upstream mysrv {

        ip_hash;

        server 192.168.0.110:80 weight=2;

        server 127.0.0.1:8000 down;

        server 192.168.0.212:80 weight=8;

    }

2server指令

该指令用语指定后端服务器的名称和参数。服务器的名称可以是一个域名,一个ip,端口号或UNIX Socket

参数介绍:

weight=number 设置服务器权重,权重值越高,被分配到客户端请求数越多。默认为1

max_fails=numbser fail_timeout指定的时间内对后端服务器请求失败的次数,如果检测到后端服务器无法连接及发生错误(404除外),则标记为失败。如果没有设置,默认为1。设置为0则关闭这项检查。

fail_timeout=time 在经历参数max_fails设置的失败次数后,暂停的时间。

配置如下:

upstream mysrv {

        ip_hash;

        server  www.xywy.com  weight=2;

        server  127.0.0.1:8000   down;

        server  192.168.0.212:80  max_fails=3  fail_timeout=30s;

        server  unix:/tmp/bakend3;

    }


( ! ) Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 169
Call Stack
#TimeMemoryFunctionLocation
10.0010412440{main}( ).../index.php:0
20.08524270704Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08524270704Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.302511311704Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.303111333208Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.304211388536Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.304511415504require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.318311811992K2ControllerItem->execute( ).../k2.php:64
90.318311811992K2ControllerItem->display( ).../BaseController.php:710
100.340712456400K2ControllerItem->display( ).../item.php:78
110.340712456400K2ControllerItem->display( ).../controller.php:19
120.346612823456Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.348512843824K2ViewItem->display( ).../ViewController.php:102
140.437215998296K2ViewItem->display( ).../view.html.php:742
150.437215998296K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.439116170600include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
  • Rate this item
    (0 votes)
  • Published in Nginx
  • Read 3499 times
More in this category: Reverse Proxy & Log Format »

( ! ) Notice: Only variables should be assigned by reference in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 478
Call Stack
#TimeMemoryFunctionLocation
10.0010412440{main}( ).../index.php:0
20.08524270704Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08524270704Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.302511311704Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.303111333208Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.304211388536Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.304511415504require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.318311811992K2ControllerItem->execute( ).../k2.php:64
90.318311811992K2ControllerItem->display( ).../BaseController.php:710
100.340712456400K2ControllerItem->display( ).../item.php:78
110.340712456400K2ControllerItem->display( ).../controller.php:19
120.346612823456Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.348512843824K2ViewItem->display( ).../ViewController.php:102
140.437215998296K2ViewItem->display( ).../view.html.php:742
150.437215998296K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.439116170600include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
back to top