jony413

多媒体信息发布、排队叫号、医院分诊、电子班牌

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  Nginx是一款轻量级的web服务器/反向代理服务器,更详细的释义自己百度了。目前国内像新浪、网易等都在使用它。先说下我的服务器软件环境:

系统:windows_server_2008_standard_enterprise_and_datacenter_with_sp2_x64

当前运行的Tomcat:非安装版本Tomcat 6.0.36

就说这两个关键的吧,目前遇到的问题是访问量剧增单个tomcat已无力负载了,经常出现超时的情况。于是就计划用nginx布置负载均衡,网络上查到的资料多是介绍linux版本的nginx的布置及使用,但在windows中如果使用linux版本的nginx只能做个测试用,实际生产环境是无法使用的,会报如下的错误:

 

[html] view plain copy
 
  1. maximum number of descriptors supported by select() is 1024 while waiting for request  

这是因为文件访问句柄数被限制为1024了,当访问量大时就会无法响应。去网上有查过很多资料说是修改参数worker_connections可以解决此限制,还有其它很多说修改worker_rlimit_nofile 参数等,都尝试了但都以失败告终。就在准备换其它工具时在国外的一个论坛看到了一条回复,地址不记得了,说的是有专门的windows版本的nginx,已修改了文件句柄数据的限制。后来下载后果真配置成功运行ok了。只要下载到正确的版本配置还是so easy的。以下为下载配置过程哈~~

nginx for windows官网:http://nginx-win.ecsds.eu/

nginx for windows下载载地址: http://nginx-win.ecsds.eu/download/

我下载的是nginx 1.7.7.1 WhiteRabbit.zip这个版本,太新的版本我怕不稳定。下载完毕后解压安装包,里面有个简要的更新信息和安装指南Readme nginx-win version.txt。关键信息如下:

 

[html] view plain copy
 
  1. *** Default installation instructions;  
  2. * New: unzip this version with folder structure  
  3. * Old: overwrite with this version  
  4. * Check nginx.conf, nginx-org.conf and nginx-win.conf  
  5. * Windows optimization registry file: check your current values BEFORE setting the new ones  

找到conf文件夹中的nginx-win.conf,把它复制一份更名为nginx.conf,然后在此文件中做配置,我的配置文件如下:

 

 

[html] view plain copy
 
  1. #user  nobody;  
  2. # multiple workers works !  
  3. # 工作进程数:这个数值要根据服务器CPU核心数来配置,如6核12线程的cpu可以配置为6或12。  
  4. worker_processes  6;  
  5.   
  6. #错误日志存放路径  
  7. #error_log  logs/error.log;  
  8. #error_log  logs/error.log  notice;  
  9. #error_log  logs/error.log  info;  
  10.   
  11. #pid        logs/nginx.pid;  
  12.   
  13.   
  14. events {  
  15. #设置单个进程同时打开的最大连接数,这个值设置大些能接受较多的连接,当然这需要cpu和内存支持哦~~  
  16.     worker_connections  32768;  
  17.     # max value 32768, nginx recycling connections+registry optimization =   
  18.     #   this.value * 20 = max concurrent connections currently tested with one worker  
  19.     #   C1000K should be possible depending there is enough ram/cpu power  
  20.     # multi_accept on;  
  21. }  
  22.   
  23.   
  24. http {  
  25.     #include      /nginx/conf/naxsi_core.rules;  
  26.     include       mime.types;  
  27.     default_type  application/octet-stream;  
  28.   
  29.     #log_format  main  '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '  
  30.     #                  '$status $body_bytes_sent "$http_referer" '  
  31.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  32.   
  33.     #access_log  logs/access.log  main;  
  34.   
  35. #     # loadbalancing PHP  
  36. #     upstream myLoadBalancer {  
  37. #         server 127.0.0.1:9001 weight=fail_timeout=5;  
  38. #         server 127.0.0.1:9002 weight=fail_timeout=5;  
  39. #         server 127.0.0.1:9003 weight=fail_timeout=5;  
  40. #         server 127.0.0.1:9004 weight=fail_timeout=5;  
  41. #         server 127.0.0.1:9005 weight=fail_timeout=5;  
  42. #         server 127.0.0.1:9006 weight=fail_timeout=5;  
  43. #         server 127.0.0.1:9007 weight=fail_timeout=5;  
  44. #         server 127.0.0.1:9008 weight=fail_timeout=5;  
  45. #         server 127.0.0.1:9009 weight=fail_timeout=5;  
  46. #         server 127.0.0.1:9010 weight=fail_timeout=5;  
  47. #         least_conn;  
  48. #     }  
  49. # 在此处设置tomcat服务器信息,同样tomcat也可以不在同一主机中。这里设置了两个tomcat服务,比重是1:1了。 localhost更换为服务器的IP  
  50.          upstream localhost{  
  51.        #ip_hash;  
  52.            server  127.0.0.1:9010 weight=1;  
  53.            server  127.0.0.1:9020 weight=1;  
  54.          }  
  55.   
  56.     sendfile        off;  
  57.     #tcp_nopush     on;  
  58.   
  59.     server_names_hash_bucket_size 128;  
  60.   
  61. ## Start: Timeouts ##  
  62.     client_body_timeout   10;  
  63.     client_header_timeout 10;  
  64.     keepalive_timeout     80;  
  65.     send_timeout          10;  
  66.     keepalive_requests    10;  
  67. ## End: Timeouts ##  
  68.   
  69.     #gzip  on;  
  70.   
  71.     server {  
  72. #这个很关键~~它是nginx监听的端口哦~~  
  73.         listen       8080;  
  74.         server_name  localhost;  
  75.   
  76.         #charset koi8-r;  
  77.   
  78.         #access_log  logs/host.access.log  main;  
  79.   
  80. # For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode  
  81.   
  82.         location / {  
  83.             #include    /nginx/conf/mysite.rules; # see also http block naxsi include line  
  84.             ##SecRulesEnabled;  
  85.               ##DeniedUrl "/RequestDenied";  
  86.               ##CheckRule "$SQL >= 8" BLOCK;  
  87.               ##CheckRule "$RFI >= 8" BLOCK;  
  88.               ##CheckRule "$TRAVERSAL >= 4" BLOCK;  
  89.               ##CheckRule "$XSS >= 8" BLOCK;  
  90.                 
  91.         proxy_pass         http://localhost;  
  92.             root   html;  
  93.             index  index.html index.htm;  
  94.         }  
  95.   
  96. # For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi  
  97.         ##location /RequestDenied {  
  98.         ##    return 412;  
  99.         ##}  
  100.   
  101. ## Lua examples !  
  102. #         location /robots.txt {  
  103. #           rewrite_by_lua '  
  104. #             if ngx.var.http_host ~= "localhost" then  
  105. #               return ngx.exec("/robots_disallow.txt");  
  106. #             end  
  107. #           ';  
  108. #         }  
  109.   
  110.         #error_page  404              /404.html;  
  111.   
  112.         # redirect server error pages to the static page /50x.html  
  113.         #  
  114.         error_page   500 502 503 504  /50x.html;  
  115.         location = /50x.html {  
  116.             root   html;  
  117.         }  
  118.   
  119.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  120.         #  
  121.         #location ~ \.php$ {  
  122.         #    proxy_pass   http://127.0.0.1;  
  123.         #}  
  124.   
  125.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  126.         #  
  127.         #location ~ \.php$ {  
  128.         #    root           html;  
  129.         #    fastcgi_pass   127.0.0.1:9000; # single backend process  
  130.         #    fastcgi_pass   myLoadBalancer; # or multiple, see example above  
  131.         #    fastcgi_index  index.php;  
  132.         #    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
  133.         #    include        fastcgi_params;  
  134.         #}  
  135.   
  136.         # deny access to .htaccess files, if Apache's document root  
  137.         # concurs with nginx's one  
  138.         #  
  139.         #location ~ /\.ht {  
  140.         #    deny  all;  
  141.         #}  
  142.     }  
  143.   
  144.   
  145.     # another virtual host using mix of IP-, name-, and port-based configuration  
  146.     #  
  147.     #server {  
  148.     #    listen       8000;  
  149.     #    listen       somename:8080;  
  150.     #    server_name  somename  alias  another.alias;  
  151.   
  152.     #    location / {  
  153.     #        root   html;  
  154.     #        index  index.html index.htm;  
  155.     #    }  
  156.     #}  
  157.   
  158.   
  159.     # HTTPS server  
  160.     #  
  161.     #server {  
  162.     #    listen       443 ssl spdy;  
  163.     #    server_name  localhost;  
  164.   
  165.     #    ssl                  on;  
  166.     #    ssl_certificate      cert.pem;  
  167.     #    ssl_certificate_key  cert.key;  
  168.   
  169.     #    ssl_session_timeout  5m;  
  170.   
  171.     #    ssl_prefer_server_ciphers On;  
  172.     #    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;  
  173.     #    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS;  
  174.   
  175.     #    location / {  
  176.     #        root   html;  
  177.     #        index  index.html index.htm;  
  178.     #    }  
  179.     #}  
  180.   
  181. }  



 

Tomcat的配置就比较简单了就是再复制一个改三处端口就可以了。具体自己百度了。

配置完毕运行nginx.exe和tomcat就可以验证配置情况了。

nginx启动控制我采用了网上一哥们的脚本,地址如下:

http://feitianbenyue.iteye.com/blog/1989868

posted on 2018-01-22 09:47  jony413  阅读(185)  评论(0)    收藏  举报