[svc]tomcat目录结构/虚拟主机/nginx反向代理cache配置

tomcat目录文件


/usr/local/tomcat/bin/catalina.sh stop
sleep 3
/usr/local/tomcat/bin/catalina.sh start

tomcat目录结构

[root@web03 tomcat]# tree -L 1
.
├── bin              # **存放tomcat服务管理脚本
├── conf             # **tomcat 配置文件存放目录
├── lib              # web应用调用的jar包存放路径
├── LICENSE
├── logs             # tomcat 日志存放目录,catalina.out 为主要输出日志
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp             # 存放临时文件(里面文件可干)
├── webapps          # web程序存放目录
└── work             # 存放jsp编译产生的.java 与 .class文件(里面文件可干)

配置文件

[root@web03 conf]# tree -L 1
.
├── Catalina
├── catalina.policy
├── catalina.properties
├── context.xml
├── logging.properties
├── logs
├── server.xml           # tomcat 主配置文件
├── server.xml.bak
├── tomcat-users.xml     # tomcat管理用户配置文件,webui那个管理
├── tomcat-users.xsd
└── web.xml

内置webapp

[root@n1 webapps]# tree -Ld 1
# tomcat内置的一些app
.
├── docs# tomcat    # 帮助文档
├── examples        # web应用实例
├── host-manager    # 主机管理页
├── manager         # 管理
├── ROOT            # 默认站点根目录
└── jforum          # 我自己部署的一个app

日志文件

[root@n1 tomcat]# tree -L 1 logs/
logs/
├── catalina.2018-03-14.log
├── host-manager.2018-03-14.log
├── catalina.out                        # Tomcat启动或关闭日志文件
├── localhost.2018-03-14.log            # 错误和其它日志
├── localhost_access_log.2018-03-14.txt # 访问日志
└── manager.2018-03-14.log              # 管理日志

tomcat配置文件

下载下来干掉注释的样子

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <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="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"/>
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine name="Catalina" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
            </Realm>

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>
        </Engine>
    </Service>
</Server>

server.xml虚拟主机配置

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log." suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>
appBase:    此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件的目录路径;
            可以使用基于$CATALINA_HOME的相对路径;

unpackWars: 在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;

autoDeploy: 在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true;

新增一个虚拟主机: www.test.com

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>

            <Host name="www.test.com" appBase="/web/webapp"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/" docBase="/web/webapp" reloadable="true"/>
                <Alias>web.test.com</Alias>
            </Host>
  • 准备网页文件
$ mkdir -pv /web/webapp
$ cd /web/webapp
$ cat index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
 <head>
  <title>JSP test page.</title>
 </head>
 <body>
  <% out.println("Welcome to test. Site, http://www.test.com"); %>
 </body>
</html>

访问

为www.test.com起一个别名web.test.com

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>

            <Host name="www.test.com" appBase="/web/webapp"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/" docBase="/web/webapp" reloadable="true"/>
                <Alias>web.test.com</Alias>
            </Host>

再定义一个Context并测试

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log." suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>

            <Host name="www.test.com" appBase="/web/webapp"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/" docBase="/web/webapp" reloadable="true"/>
                <Context path="/test" docBase="/web/test" reloadable="true"/>
                <Alias>web.test.com</Alias>
            </Host>
[root@tomcat webapp]# mkdir /web/test
[root@tomcat webapp]# cd /web/test
[root@tomcat test]# vim index.jsp
<%@ page language="java" %>
<html>
 <head><title>TomcatA</title></head>
 <body>
  <h1><font color="red">TomcatA </h1>
  <table align="centre" border="1">
   <tr>
    <td>Session ID</td>
  <% session.setAttribute("abc","abc"); %>
    <td><%= session.getId() %></td>
   </tr>
   <tr>
    <td>Created on</td>
    <td><%= session.getCreationTime() %></td>
   </tr>
  </table>
 </body>
</html>

  • 至此完整的server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <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="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"/>
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
        <Engine name="Catalina" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
            </Realm>

            <Host name="localhost" appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
            </Host>

            <Host name="www.test.com" appBase="/web/webapp"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/" docBase="/web/webapp" reloadable="true"/>
                <Context path="/test" docBase="/web/test" reloadable="true"/>
                <Alias>web.test.com</Alias>
            </Host>
        </Engine>
    </Service>
</Server>

Tomcat图形管理接口(很少用到)

  • Server Status 主要用来查看服务器的状态
  • Manager App 主要用来管理应用程序的部署及监控
  • Host Manager 主要用来管理虚拟主机

tomcat-users.xml

高版本如果不生效可以参考

  • Manager的四个管理角色:

manager-gui - allows access to the HTML GUI and the status pages
manager-script - allows access to the text interface and the status pages
manager-jmx - allows access to the JMX proxy and the status pages
manager-status - allows access to the status pages only

  • tomcat-users.xml里配置个ui管理员账号
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
    <role rolename="manager-gui"/>
    <role rolename="admin-gui"/>
    <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
</tomcat-users>


部署jforum,用nginx做反代

nginx简单配置


worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include    mime.types;
    default_type application/octet-stream;
    sendfile    on;

    keepalive_timeout 65;
    server {
        listen    80;
        server_name localhost;
        location / {
            #root  html;
            #index index.html index.htm;
            proxy_pass http://192.168.18.201/; #注释默认两行,新增一行。
        }
        #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;
        }
    }
}

启动后可以正常访问了.
但是每次索取图片都得从tomcat上去下载,nginx没缓存.

nginx cache配置

#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#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;
    #gzip on;
    proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G; #新建缓存路径与相关属性
    upstream backend {
         #建立后端tomcat服务器
        server 192.168.14.11:8080 weight=1;
    }
    server {
        listen    80;
        server_name localhost;
        #charset koi8-r;
        #access_log logs/host.access.log main;
        location / {
            #root  html;
            #index index.html index.htm;
            #proxy_pass http://192.168.14.11:8080/; #注释原来的代理设置
            proxy_pass http://backend/; #启动后端服务器
        }
        location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {
             #缓存图片与静态内容
            proxy_pass http://backend;
            proxy_cache first;
            proxy_cache_valid 200 24h; #200状态缓存24小时
            proxy_cache_valid 302 10m; #302状态缓存10分钟
            add_header X-Cache-Status $upstream_cache_status; #在http头部增加一个字段显示是否命令缓存
        }
        #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;
        }
    }
}
mkdir -pv /nginx/cache

访问可以看到 response header里有cache hit了.

可以看到nginx缓存的一些东西了

可以看到我们的静态内容来自缓存,动态内容全部代理到后端的tomcat服务器上.配置完毕

omcat系列之服务器的基本配置及Nginx反向代理tomcat服务

posted @ 2018-03-14 13:21  _毛台  阅读(381)  评论(0编辑  收藏  举报