Windows下配置Nginx+Tomcat配置集群负载均衡

来源:互联网 发布:手机版极简付款软件 编辑:程序博客网 时间:2024/06/05 18:33
Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。


   首先是window环境结构如下图:

Window xp环境:Nginx1.0.15+Tomcat7.0.8

1、下载地址

       http://nginx.org/en/download.html

2、目录结构


      Nginx-

               |_  conf   配置目录

               |_  contrib

               |_  docs 文档目录

               |_  logs  日志目录

               |_  temp 临时文件目录

               |_  html 静态页面目录

               |_  nginx.exe 主程序

解压缩到一个无空格的英文目录即可

双击nginx.exe即可启动Nginx服务器

DOS环境启动

运行CMD 到DOS界面

 

下面是一些常用的使用命令:
 Nginx -s stop         快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
 Nginx -s quit         平稳关闭Nginx,保存相关信息,有安排的结束web服务。
 Nginx -s reload       因改变了Nginx相关配置,需要重新加载配置而重载。
 Nginx -s reopen       重新打开日志文件。

 Nginx -v       查看版本
 Nginx -V       查看nginx的版本,编译器版本和配置参数

3、nginx.conf配置

  Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:\nginx-1.0.15、默认主配置文件为D:\nginx-1.0.15\nginx.conf。下面是nginx作为前端反向代理服务器的配置。

Nginx.conf代码

 

[plain] view plaincopy
  1. #Nginx所用用户和组,window下不指定  
  2. #user  Administrator;  
  3. #工作的子进程数量(通常等于CPU数量或者2倍于CPU)   
  4. worker_processes  2;  
  5. #错误日志存放路径  
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9. #指定pid存放文件  
  10. #pid        logs/nginx.pid;  
  11.   
  12.   
  13. events {  
  14. #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
  15. #use epoll;  
  16.     worker_connections  2048;  
  17. }  
  18.   
  19.   
  20. http {  
  21.     include       mime.types;  
  22.     default_type  application/octet-stream;  
  23. #定义日志格式  
  24.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  25.     #                  '$status $body_bytes_sent "$http_referer" '  
  26.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  27.   
  28.     #access_log  logs/access.log  main;  
  29.   
  30.     sendfile        on;  
  31.     #tcp_nopush     on;  
  32.   
  33.     #keepalive_timeout  0;  
  34.     keepalive_timeout  65;  
  35.       
  36.     include    gzip.conf;  
  37. upstream localhost {     
  38.       #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。     
  39.       #同一机器在多网情况下,路由切换,ip可能不同     
  40.       #ip_hash;      
  41.       server localhost:18081;     
  42.       server localhost:8080;     
  43.      }   
  44.   
  45.     server {  
  46.         listen       80;  
  47.         server_name  localhost;  
  48.   
  49.         #charset koi8-r;  
  50.   
  51.         #access_log  logs/host.access.log  main;  
  52.   
  53.         location / {  
  54.             root   html;  
  55.             index  index.html index.htm;  
  56. proxy_pass http://localhost;  
  57.         }  
  58.   
  59.         #error_page  404              /404.html;  
  60.   
  61.         # redirect server error pages to the static page /50x.html  
  62.         #  
  63.         error_page   500 502 503 504  /50x.html;  
  64.         location = /50x.html {  
  65.             root   html;  
  66.         }  
  67.   
  68.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  69.         #  
  70.         #location ~ \.php$ {  
  71.         #    proxy_pass   http://127.0.0.1;  
  72.         #}  
  73.   
  74.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  75.         #  
  76.         #location ~ \.php$ {  
  77.         #    root           html;  
  78.         #    fastcgi_pass   127.0.0.1:9000;  
  79.         #    fastcgi_index  index.php;  
  80.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  81.         #    include        fastcgi_params;  
  82.         #}  
  83.   
  84.         # deny access to .htaccess files, if Apache's document root  
  85.         # concurs with nginx's one  
  86.         #  
  87.         #location ~ /\.ht {  
  88.         #    deny  all;  
  89.         #}  
  90.     }  
  91.   
  92.   
  93.     # another virtual host using mix of IP-, name-, and port-based configuration  
  94.     #  
  95.     #server {  
  96.     #    listen       8000;  
  97.     #    listen       somename:8080;  
  98.     #    server_name  somename  alias  another.alias;  
  99.   
  100.     #    location / {  
  101.     #        root   html;  
  102.     #        index  index.html index.htm;  
  103.     #    }  
  104.     #}  
  105.   
  106.   
  107.     # HTTPS server  
  108.     #  
  109.     #server {  
  110.     #    listen       443;  
  111.     #    server_name  localhost;  
  112.   
  113.     #    ssl                  on;  
  114.     #    ssl_certificate      cert.pem;  
  115.     #    ssl_certificate_key  cert.key;  
  116.   
  117.     #    ssl_session_timeout  5m;  
  118.   
  119.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  120.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  121.     #    ssl_prefer_server_ciphers   on;  
  122.   
  123.     #    location / {  
  124.     #        root   html;  
  125.     #        index  index.html index.htm;  
  126.   
  127.     #    }  
  128.     #}  
  129.   
  130. }  

代理设置如下:

[plain] view plaincopy
  1. proxy_redirect          off;     
  2. proxy_set_header        Host $host;     
  3. proxy_set_header        X-Real-IP $remote_addr;     
  4. proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;     
  5. client_max_body_size    10m;     
  6. client_body_buffer_size 128k;     
  7. proxy_connect_timeout   300;     
  8. proxy_send_timeout      300;     
  9. proxy_read_timeout      300;     
  10. proxy_buffer_size       4k;     
  11. proxy_buffers           4 32k;     
  12. proxy_busy_buffers_size 64k;     
  13. proxy_temp_file_write_size 64k;   


zip压缩相关配置如下:

[plain] view plaincopy
  1. gzip              on;     
  2. gzip_min_length      1000;     
  3. gzip_types         text/plain text/css application/x-javascript;  


4、Tomcat配置

第一处端口修改:

[html] view plaincopy
  1. <!--  修改port端口:8006 俩个tomcat不能重复,端口随意,别太小-->    
  2. <Server port="8006" shutdown="SHUTDOWN">   
第二处端口修改:
[html] view plaincopy
  1. <!-- port="18081" tomcat监听端口,随意设置,别太小 -->    
  2. <Connector port="18081" protocol="HTTP/1.1"      
  3.                connectionTimeout="20000"      
  4.                redirectPort="8443" />    
第三处端口修改:
[html] view plaincopy
  1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />   
Engine元素增加jvmRoute属性:
[html] view plaincopy
  1. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">    

启动Nginx和俩个Tomcat验证置负载均衡设置,http://localhost/ 或http://localhost/index.jsp 。
至此window下nginx+tomcat负载均衡配置结束,关于tomcat Session的问题通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。

参考资料:

http://www.oschina.net/bbs/thread/9301

oschina.NET 生产配置,此网站采用Java语言,nginx,tomcat服务器。

http://nginx.org/

张宴:<<实战Nginx>>