windows 下nginx和tomcat集成

来源:互联网 发布:网络科科是什么意思 编辑:程序博客网 时间:2024/06/06 00:51

一、为什么需要对Tomcat服务器做负载均衡:

Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果有超过500的并发数便会出现Tomcat不能响应新的请求的情况,严重影响网站的运行。另外,在访问量大的情况下,Tomcat的线程数会不断增加。由于Tomcat自身对内存的占用有控制,当对内存的占用达到最大值时便会出现内存溢出,对网站的访问严重超时等现象,这时便需要重新启动Tomcat以释放占用的内存,这样做便会阻断网站运行。 

所以对Tomcat做负载均衡便很有必要。目前可以和Tomcat做负载均衡的主流服务器是Apache,但是Nginx由于功能多、配置简单等优点逐渐成为很多负载均衡服务器的首选。Nginx的并发数可达到50000,所以理论上可以和Tomcat以1:100的比例来配置,这样便可以很好的解决网站并发瓶颈问题。而且Nginx、apache是基于http反向代理方式,位于ISO模型的第七层应用层。直白些就是TCP UDP 和http协议的区别,Nginx不能为基于TCP协议的应用提供负载均衡。

二. 配置方法:

1.下载相应的服务器,Tomcat的版本采用6.0的.Nginx采用1.4.4版本

     目录结构

     Nginx-

              |_  conf   配置目录

              |_  contrib

              |_  docs 文档目录

              |_  logs  日志目录

              |_  temp 临时文件目录

              |_  html 静态页面目录

              |_  nginx.exe 主程序

注:

若果想停止nginx,dos环境运行命令:nginx -s stop.

开启nginx 双击nginx.exe 或者dos 下运行start nginx;

下面是nginx作为前端反向代理服务器的配置:nginx.conf代码

#Nginx所用用户和组,window下不指定#user  nobody;#工作的子进程(通常等于CPU数量或者1倍于CPU)worker_processes  1;#错误日志存放路径#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#指定pid存放文件#pid        logs/nginx.pid;events {    #允许最大连接数    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;      #定义日志格式    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #客户端上传文件大小控制client_max_body_size 8m;    #gzip  on;    upstream localhost {          server localhost:8899;        #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。          #同一机器在多网情况下,路由切换,ip可能不同                ip_hash;  }        server {        listen       9999;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root html;index index.html index.htm;#此处的 http://localhost与upstream localhost对应proxy_pass  http://localhost;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size   10m; client_body_buffer_size  128k;proxy_connect_timeout  100;proxy_send_timeout   100;proxy_read_timeout 100;proxy_buffer_size 4k;proxy_buffers  4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size  64k;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

3.修改Nginx配置文件nginx.conf

(1)在#gzip  on;后面加入下面配置: 

 upstream localhost {               server localhost:8080;               server localhost:8088;              ip_hash;}

(2)修改第一个server{}配置中的listen  80;改为新的端口号,因为我的本机80端口被其他服务器占用,因此将此处改为listen 9999;

location / {          root   html;            index  index.html index.htm; }  改为: location / {                        root html;index index.html index.htm;#此处的 http://localhost与upstream localhost对应proxy_pass  http://localhost;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size   10m; client_body_buffer_size  128k;proxy_connect_timeout  100;proxy_send_timeout   100;proxy_read_timeout 100;proxy_buffer_size 4k;proxy_buffers  4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size  64k;        }

(3)3.tomcat的配置

<!-- port="8899" tomcat监听端口,随意设置,别太小 -->  <Connector port="8899" protocol="HTTP/1.1"                  connectionTimeout="20000"                  redirectPort="8443" />  
验证配置( 首先测试nginx配置是否正确,测试命令:nginx -t  (默认验证:conf\nginx.conf),也可以指定配置文件路径


启动nginx双机nginx.exe文件或者用start nginx启动 打开浏览器输入地址http://localhost:9999看到下面画面表示成功:



0 0
原创粉丝点击