windows nginx + tomcat 负载整合

来源:互联网 发布:java构造函数重载定义 编辑:程序博客网 时间:2024/04/29 01:59

啰嗦话不多说了,直奔主题


完整实例下载


步骤1:

先到官方下载个windows版本的nginx1.5现在是最新版本,然后随便解压到一个目录下,我的目录是C:\Program Files (x86)\nginx-1.5.0

然后双击那个nginx.exe启动看下;默认配置是80端口,如果没有其他程序占用这个端口就应该没什么问题,然后直接访问http://localhost,看到欢迎界面就OK了


步骤2:

解压两个免安装的tomcat到随便一个目录吧,我的目录是E:\

用myeclipse或者eclipse新建一个web工程随便放点东西下去



index.jsp的主要内容


<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="pragma" content="no-cache"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="expires" content="0"/>  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/><meta http-equiv="description" content="This is my page"/><title>Insert title here</title><script src="http://localhost/plugin/jquery/core.js" type="text/javascript"></script><script>$(function(){$("#test").click(function(){alert("使用JQUERY方法成功!");});});</script></head><body>通过nginx进入tomcat访问静态资源成功! <input type="button" id="test" value="测试静态JS"/><% System.out.println("访问了这个页面!");%></body></html>


然后修改我们的两个tomcat的server.xml指向我们的项目,以及修改启动端口,毕竟一个电脑的端口都是唯一的,所以为了不冲突就只能修改各种不同的端口

这是一个示例,你可以直接搜索port,看到有端口数字就改变下,来点规律,前面加个1然后整个数字再加1(例如端口号是8080,你可以改成18080+1=18081)这样就不会冲突了

<?xml version='1.0' encoding='utf-8'?><Server port="18005" shutdown="SHUTDOWN">  <!--APR library loader. Documentation at /docs/apr.html -->  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->  <Listener className="org.apache.catalina.core.JasperListener" />  <!-- Prevent memory leaks due to use of particular java/javax APIs-->  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  <GlobalNamingResources>    <Resource name="UserDatabase" auth="Container"              type="org.apache.catalina.UserDatabase"              description="User database that can be updated and saved"              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"              pathname="conf/tomcat-users.xml" />  </GlobalNamingResources>  <Service name="Catalina">      <Connector port="18080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="18443" />    <!-- Define an AJP 1.3 Connector on port 8009 -->    <Connector port="18009" protocol="AJP/1.3" redirectPort="18443" />    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"             resourceName="UserDatabase"/>      <Host name="localhost"  appBase="webapps"            unpackWARs="true" autoDeploy="true"            xmlValidation="false" xmlNamespaceAware="false"><Context docBase="E:\eclipse\workspace\nginx\WebRoot" reloadable="false" path="/nginx"></Context>      </Host>    </Engine>  </Service></Server>

然后启动下两个tomcat看下有木有问题,如果木有问题就是OK了(通过bin下面的start启动就可以了)


步骤3: 来改我们的nginx配置文件

在conf下新增一个proxy.conf和gzip.conf文件,前者是使用代理需要的,后者是使用GZIP压缩需要的配置(可不用);具体的参数自己找找资料吧.这里不啰嗦了

proxy.conf

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 300;proxy_send_timeout 300;proxy_read_timeout 300;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;

gzip.conf

gzip              on;  gzip_min_length   1024;  gzip_buffers      4 8k;  gzip_comp_level   9;  gzip_proxied      any;  gzip_types        application/xml application/javascript application/x-javascript application/atom+xml application/rss+xml;  gzip_types        text/css text/html text/javascript text/js text/plain text/xml; 

最后是修改的重点nginx.conf

可以先copy下面的配置替换默认的配置,然后自己按照需求来调

#Nginx所用用户和组,window下不指定#user  niumd niumd;#工作的子进程数量(通常等于CPU数量或者2倍于CPU)worker_processes  2;#错误日志存放路径#error_log  logs/error.log;#error_log  logs/error.log  notice;error_log  logs/error.log  info;#指定pid存放文件pid        logs/nginx.pid;events {#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。#use epoll;#允许最大连接数worker_connections  2048;}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  off;access_log  logs/access.log;client_header_timeout  3m;client_body_timeout    3m;send_timeout           3m;client_header_buffer_size    1k;large_client_header_buffers  4 4k;sendfile        on;tcp_nopush      on;tcp_nodelay     on;#keepalive_timeout  75 20;include    gzip.conf;include   proxy.conf;upstream localhost {#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。#同一机器在多网情况下,路由切换,ip可能不同#ip_hash;#weigth参数表示权值,权值越高被分配到的几率越大server localhost:18080 weight=1;server localhost:28080 weight=6;}server {listen       80;server_name  localhost;location / {      root E:/eclipse/workspace/nginx/WebRoot/statict;    index index.html index.htm;}location ~ \.(html|js|css|png|gif)$ {      root E:/eclipse/workspace/nginx/WebRoot/statict;} location ~ \.(jsp|action)$ {    proxy_connect_timeout   3;    proxy_send_timeout      30;    proxy_read_timeout      30;    proxy_pass http://localhost;}}}

root参数是指向文件夹路径,例如我的默认首页是放在
E:/eclipse/workspace/nginx/WebRoot/statict

index是访问当前root下的各种静态文件,这里配置访问是index.html;我页面请求http://localhost的时候会请求root路径下的index.html

location ~ \.(html|js|css|png|gif)$
这里是正则过滤遇到括号里的类型就会找root路径下的文件,很明显这里就是动态跟静态的分离做法,我是把静态文件都放在static里面


location ~ \.(jsp|action)$
这里是过滤遇到jsp跟action请求就会转移到tomcat里请求资源


最后保存配置文件,关闭nginx,重新启动,访问htt://localhost/nginx/index.jsp

多请求几次就能看到两个tomcat的小黑窗口有显示输出的东西


nginx + tomcat负载就差不多可以了,具体的只是配置的细节,例如拒绝神马请求,请求神马需要转换路径,就写多几个location配置即可,这些内容就自行研究吧!


原创粉丝点击