tomcat作为servlet容器,本身就是一个完整独立的服务器,nginx既可以作为单独的服务器也可以做为代理服务器来处理请求,nginx擅于处理静态请求。本例中,使用nginx+tomcat组合来进行部署,工作原理:
请求从客户端发送,首先由nginx接收,如果为静态请求(即请求静态文件比如图片、html页面或多媒体等),nginx直接处理,返回请求的文件;如果是动态请求(对应controller里面的RequestMapping),则nginx为代理将请求转发至tomcat,tomcat将处理结果返回nginx,再由nginx反馈给客户端。
通常一台服务器会部署多个应用,也就是拥有多个虚拟主机,在这种情况下如何判断请求是指向哪个应用的呢?判断依据就是请求头的‘Host’参数,如何实现,下面会作详细的介绍。。。
1.配置tomcat
tomcat/conf/server.xml格式:
<Server port="8005" shutdown="SHUTDOWN">
<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>
<Service name="Catalina">
<Connector URIEncoding="UTF-8" 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 "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
</Host>
</Engine>
</Service>
</Server>
请求头中的Host参数用来匹配<Host>标签中的name属性,匹配成功则由当前host处理请求
存在默认的host--localhost,要在同一个tomcat上同时运行多个应用,则要每个应用对应一个<Host>标签,并进行设置。假定$Tom_Home为tomcat根目录,tomcat上添加虚拟主机步骤:
在server.xml文件中添加<Host appBase=”$Tom_Home/app1” name=”www.xxx.com”>
还有其它的属性,可以根据要求设值,都有默认值,结果大体为:
<Host appBase="/home/dev/ProgramFiles/apache-tomcat-7.0.29/l-app" autoDeploy="true" name="www.lxcf.com" unpackWARs="true" deployOnStartup="false" deployIgnore="^(?!(manager)|(tomee)$).*">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt" />
</Host>
创建相关目录:$Tom_Home/app1、$Tom_Home/conf/Catalina/www.xxx.com(tomcat启动时也会根据host自动创建)
在$Tom_Home/conf/Catalina/www.lxcf.com文件中添加文件ROOT.xml用来定义应用context path,文件格式:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" docBase="/home/dev/work/projects/lxcf/WebContent" />
docBase 的值为应用的根的绝对路径,path的值由tomcat自动设置
另一种配置方式是将context属性放在Host标签内,无需创建文件,也不用添加ROOT.xml文件:
<Host name="zbb.gtn.com" appBase="webapps/zbb">
<Context path="/" docBase="/home/dev/app/zbb/zbb_war_exploded" allowLinking="true" reloadable="false"/>
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="zbb_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>
</Host>
2.配置Nginx
声明一下nginx的作用,当客户端发来请求,首先有nginx接收,如果是动态请求则转交给tomcat处理;如果是请求的静态资源(ie 图片,css,js,多媒体等),则直接返回请求的资源
nginx/conf/nginx.conf格式:
#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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
请求到达nginx时与server中的server_name进行匹配,匹配成功则由当前server处理该请求
存在一个默认的server名为localhost,并有示例说明
添加<server>并进行设置,如:
server {
listen 80;
root /home/dev/work/projects/xxx/WebContent;
server_name www.xxx.com;
client_max_body_size 100M;
location / {
rewrite ^/$ /home.do;
}
location ~ \.do$ {
proxy_pass http://www.xxx.com:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
root 指定到应用的根目录
3.启动tomcat和nginx
命令行启动tomcat $Tom_Home/bin 目录下有个startup.sh 文件,命令:sh startup.sh 即可启动
如果需要传参数到tomcat的话,则用:p=v sh startup.sh;终止tomcat:sh shutdown.sh
如果配置没问题的话,就可以实现同一台机器、一个tomcat、一个nginx下部署多个应用,即设置多个虚拟主机