nginx和tomcat结合

Posted huagee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx和tomcat结合相关的知识,希望对你有一定的参考价值。

在日常的工作中,需要用到nginx和tomcat,在这里记录一下具体的配置。
假设域名为 www.demo.com

nginx

 server {
  listen 80;
  server_name www.demo.com demo.com;      #多个域名之间用空格分开
  access_log  /web/wwwlogs/www.demo.com.log; #定义访问日志
  error_log  /web/wwwlogs/www.demo.com.log; #定义错误日志
  index index.html index.htm index.jsp;        
  root /web/wwwroot/www.demo.com;     #定义网站根目录
  #error_page 404 /404.html;
  #error_page 502 /502.html;
     location ~ {
    proxy_pass "http://www.demo.com:8080";   #设置代理path,注意端口一定要对应
    proxy_set_header Host www.demo.com;    #设置请求头
    proxy_set_header X-Forwarded-For $remote_addr;     
  }
 #设置静态资源访问规则
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /.ht {
    deny all;
  }
}

tomcat7配置文件

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <!-- 定义tomcat http端口 -->
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />  
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
      </Realm>
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt" />
      </Host>
    <!-- tomcat 虚拟主机配置 -->
      <Host name="www.demo.com" appBase=""
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
           <Alias>demo.com</Alias>
           <Context path="" debug="0" docBase="/web/wwwroot/www.demo.com" reloadable="true" />
      </Host>
    </Engine>
  </Service>
</Server>

以上是关于nginx和tomcat结合的主要内容,如果未能解决你的问题,请参考以下文章

tomcat 和 nginx 结合使用

nginx和tomcat结合

电商平台 lnmp 架构之 nginx+tomcat

电商平台 lnmp 架构之 nginx+tomcat

Nginx SSL 结合Tomcat 重定向URL变成HTTP的问题

Nginx+Tomcat负载均衡集群