1. 程式人生 > >nginx和tomcat結合

nginx和tomcat結合

protocol shutdown engine factor pro forward mes text upd

在日常的工作中,需要用到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結合