<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/ DTD/wml_1.1.xml">

<wml>
<head>
<meta http-equiv="cache-control" content="max-age=180,private" />
</head>
<card title="安装 Nginx 配置负载均衡">
<p>
作者:<a href="index.php?action=showuser&amp;userid=1&amp;hash=">admin</a><br />时间:2009-12-07 10:32<br />分类:<a href="index.php?action=list&amp;cid=3&amp;hash=">电脑技术</a><br />内容:
安装 Nginx 配置负载均衡


先安装：





    wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz   

    tar zxvf nginx-0.6.35.tar.gz   

    cd nginx-0.6.35   

    ./configure   

    make  

    make install  




wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
tar zxvf nginx-0.6.35.tar.gz
cd nginx-0.6.35
./configure
make
make install


安装时出现下面的错误：




Configuration summary
+ PCRE library is not found
+ OpenSSL library is not used
+ md5 library is not used
+ sha1 library is not used
+ using system zlib library


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=

option.




说没有 PCRE 库，那安装它：





    yum install pcre-devel  




yum install pcre-devel


安装完后，修改配置vi conf/nginx.conf，修改后看起来像：





    user  chenlb;   

    worker_processes  10;   

      

    #error_log  logs/error.log;   

    #error_log  logs/error.log  notice;   

    #error_log  logs/error.log  info;   

      

    #pid        logs/nginx.pid;   

      

    events {   

        use epoll;   

        worker_connections  1024;   

    }   

      

    http {   

        include       mime.types;   

        default_type  application/octet-stream;   

      

        #log_format  main  '$remote_addr - $remote_user [$time_local] $request '   

        #                  '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; '   

        #                  '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';   

      

        #access_log  logs/access.log  main;   

      

        sendfile        on;   

        #tcp_nopush     on;   

      

        #keepalive_timeout  0;   

        keepalive_timeout  65;   

      

        #gzip  on;   

      

        upstream demo {   

            server 172.0.0.1:8080 weight=1;   

            server 192.168.1.100:8080 weight=1;   

      

        }   

      

        server {   

            listen       80;   

            server_name  nobody.chenlb.com;   

      

            #charset koi8-r;   

      

            log_format  nobody_chenlb_com  '$remote_addr - $remote_user [$time_local] $request '   

                          '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; &quot;$http_x_forwarded_for&quot;';   

      

            access_log  logs/access.log  nobody_chenlb_com;   

      

            location / {   

                proxy_pass http://demo;   

                proxy_set_header   Host             $host;   

                proxy_set_header   X-Real-IP        $remote_addr;   

                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;   

            }   

        }   

      

    }  




user  chenlb;
worker_processes  10;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '
    #                  '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; '
    #                  '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream demo {
        server 172.0.0.1:8080 weight=1;
        server 192.168.1.100:8080 weight=1;

    }

    server {
        listen       80;
        server_name  nobody.chenlb.com;

        #charset koi8-r;

        log_format  nobody_chenlb_com  '$remote_addr - $remote_user [$time_local] $request '
                      '&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; &quot;$http_x_forwarded_for&quot;';

        access_log  logs/access.log  nobody_chenlb_com;

        location / {
            proxy_pass http://demo;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }

}


修改浏览器所在机子hosts，nobody.chenlb.com 指向到nginx所在机子。


jsp的测试页面，nginx-test.jsp：





    &lt;%@ page language=&quot;java&quot; pageEncoding=&quot;UTF-8&quot;%&gt;  

    &lt;%@ page import=&quot;java.util.LinkedHashMap&quot; %&gt;  

    &lt;%@ page import=&quot;java.util.Enumeration&quot; %&gt;  

    &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;  

    &lt;html&gt;  

    &lt;head&gt;  

    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;  

    &lt;title&gt;nginx demo&lt;/title&gt;  

    &lt;/head&gt;  

    &lt;body&gt;  

    &lt;%   

    LinkedHashMap map = new LinkedHashMap();   

    map.put(&quot;getServerName&quot;, request.getServerName());   

    map.put(&quot;getServerPort&quot;, request.getServerPort());   

    map.put(&quot;getRemoteAddr&quot;, request.getRemoteAddr());   

    map.put(&quot;getRemoteHost&quot;, request.getRemoteHost());   

    map.put(&quot;getRemotePort&quot;, request.getRemotePort());   

      

    map.put(&quot;getLocalName&quot;, request.getLocalName());   

    map.put(&quot;getLocalAddr&quot;, request.getLocalAddr());   

    map.put(&quot;getLocalPort&quot;, request.getLocalPort());   

    //HttpServletRequest req = (HttpServletRequest)request;   

    for (Enumeration e = request.getHeaderNames() ; e.hasMoreElements() ;) {   

        Object obj = e.nextElement();   

        map.put(obj.toString(), request.getHeader(obj.toString()));   

    }   

    %&gt;  

    &lt;table border=1&gt;  

    &lt;tr&gt;&lt;th&gt;name&lt;/th&gt;&lt;th&gt;value&lt;/th&gt;&lt;/tr&gt;  

    &lt;%   

      

    for(Object key : map.keySet()) {   

        out.println(&quot;&lt;tr&gt;&lt;th&gt;&quot;);   

        out.println(key+&quot;&lt;/th&gt;&lt;th&gt;&quot;+map.get(key)+&quot;&lt;/th&gt;&lt;/tr&gt;&quot;);   

    }   

      

    String str = request.getContextPath()+request.getServletPath()+(request.getPathInfo()==null?&quot;&quot;:request.getPathInfo())+(request.getQueryString() == null ? &quot;&quot; : &quot;?&quot;+request.getQueryString());   

    System.out.println(str);   

    %&gt;  

    &lt;/table&gt;  

    &lt;/body&gt;  

    &lt;/html&gt;  




&lt;%@ page language=&quot;java&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@ page import=&quot;java.util.LinkedHashMap&quot; %&gt;
&lt;%@ page import=&quot;java.util.Enumeration&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;nginx demo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
LinkedHashMap map = new LinkedHashMap();
map.put(&quot;getServerName&quot;, request.getServerName());
map.put(&quot;getServerPort&quot;, request.getServerPort());
map.put(&quot;getRemoteAddr&quot;, request.getRemoteAddr());
map.put(&quot;getRemoteHost&quot;, request.getRemoteHost());
map.put(&quot;getRemotePort&quot;, request.getRemotePort());

map.put(&quot;getLocalName&quot;, request.getLocalName());
map.put(&quot;getLocalAddr&quot;, request.getLocalAddr());
map.put(&quot;getLocalPort&quot;, request.getLocalPort());
//HttpServletRequest req = (HttpServletRequest)request;
for (Enumeration e = request.getHeaderNames() ; e.hasMoreElements() ;) {
	Object obj = e.nextElement();
	map.put(obj.toString(), request.getHeader(obj.toString()));
}
%&gt;
&lt;table border=1&gt;
&lt;tr&gt;&lt;th&gt;name&lt;/th&gt;&lt;th&gt;value&lt;/th&gt;&lt;/tr&gt;
&lt;%

for(Object key : map.keySet()) {
	out.println(&quot;&lt;tr&gt;&lt;th&gt;&quot;);
	out.println(key+&quot;&lt;/th&gt;&lt;th&gt;&quot;+map.get(key)+&quot;&lt;/th&gt;&lt;/tr&gt;&quot;);
}

String str = request.getContextPath()+request.getServletPath()+(request.getPathInfo()==null?&quot;&quot;:request.getPathInfo())+(request.getQueryString() == null ? &quot;&quot; : &quot;?&quot;+request.getQueryString());
System.out.println(str);
%&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;


分别在172.0.0.1:8080、192.168.1.100:8080的web实例下放入nginx-test.jsp。


启动nginx：





    sbin/nginx  




sbin/nginx


在浏览器中访问：http://nobody.chenlb.com/demo/nginx-test.jsp，出现了一个问题，一时快，一时很慢，看到nginx在域名查找，后来把upstream的demo在nginx那台机子的hosts配上IP就好。


参考：http://blog.s135.com/post/306/ ，http://wiki.nginx.org/NginxChsLoadBalanceExample
</p><p>
<a href="index.php?action=login&amp;hash=">立即登陆发表评论</a><br />
</p>
<p><a href="index.php?action=list&amp;hash=">返回日志列表</a><br /><a href="index.php?action=index&amp;hash=">返回主页</a></p>
</card>
</wml>
