【转】Nginx + Tomcat 负载均衡

一、什么是Nginx?

        nginx [engine x]是Igor Sysoev编写的一个HTTP和反向代理服务器,另外它也可以作为邮件代理服务器。 它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括Yandex、Mail.Ru、VKontakte,以及Rambler。据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器(详见:http://nginx.org/cn/ 或者 google)。

 

二、在Windows 下Nginx+Tomcat 配置

        Nginx :http://nginx.org/

        Tomcat:http://tomcat.apache.org/

    我本机环境是 Windows XP+Nginx 1.0.15+ apache-tomcat-7.0.34

        1、Nginx安装:Nginx下载解压到本机一无空格、汉字的任意目录即可。

            

        2、conf/nginx.conf 配置文件修改

            a:修改Nginx默认端口:

                

Conf代码    收藏代码
  1. server {  
  2.         listen       80;  
  3.         server_name  localhost;  
  4.         ......  
  5. }  

              此处的listen 即为监听,默认80 端口,若有需要可修改为其它端口。

            b:在keepalive_timeout 后面增加

 

Conf代码    收藏代码
  1. upstream location {  
  2.     #weigth 参数表示权值,权值越高被分配到的几率越大  
  3.     server 127.0.0.1:8048 weight=3;  
  4.     server 127.0.0.1:8049 weight=2;  
  5.     ip_hash;  
  6. }  

 

 

            c:修改

 

Conf代码    收藏代码
  1. location / {  
  2.         root    html;  
  3.     index   index.html index.htm;  
  4. }  

 

 

               为

             

Conf代码    收藏代码
  1. location / {  
  2.             root    html;  
  3.         index   index.html index.htm;  
  4.         proxy_pass  http://location;  
  5.         proxy_redirect  off;  
  6.         proxy_set_header    Host $host;  
  7.         proxy_set_header    X-Real-IP $remote_addr;  
  8.         proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  
  9.         client_max_body_size    10m;  
  10.         client_body_buffer_size 128k;  
  11.         proxy_connect_timeout   90;  
  12.         proxy_send_timeout  90;  
  13.         proxy_read_timeout  90;  
  14.         proxy_buffer_size   4k;  
  15.         proxy_buffers   4 32k;  
  16.         proxy_busy_buffers_size 64k;  
  17.         proxy_temp_file_write_size  64k;  
  18. }  

 

 

         3、修改Tomcat server.xml中端口配置,让本机能运行多个Tomcat,注意Tomcat发布端口要和2.a 中映射端口一致。

 

到此配置完成,输入 http://127.0.0.1:80(或自定义端口)即可访问

 

转自:http://guomingjun.iteye.com/blog/1939158

猜你喜欢

转载自gdfdfg-tech.iteye.com/blog/2055447