由于技术研发部在开发三期业务时没有考虑到与二期业务的兼容性,为了保证三期业务上线时不影响二期用户客户端的使用,需要在同一tomcat下部署两个工程。但由于这两个工程之间需要相互调用,据博主测试这两个工程如果再同一端口下运行时总会有一个工程无法正常请求另一个工程的业务(研发部门未经测试直接交付实施上线【太可怕了】),所以在部署业务时,笔者为了保证业务按时上线采用了在一个Tomcat配置多个端口号以对应不同服务完成业务的正常交付。
一、在Tomcat下配置多个服务,用不同的端口号。下面以增加1个端口号为例 。具体实施步骤如下所示:
第一步、修改 vi /webapp/tomcat6/conf/server.xml文件
具体修改内容如下:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005"shutdown="SHUTDOWN">
<Resourcename="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>
<Servicename="Catalina">
<Connectorport="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Hostname="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/webapp/tomcat6/webapps/sdmp/"reloadable="true" ></Context>
</Host>
</Engine>
</Service>
<!—此次向下开始添加新的端口服务-->
<!-- add byfengzhanhai on 2013 -->
<Servicename="Catalinai">
<!—service name 必须要修改成与原servicename不一致-->
<Connectorport="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!—端口号 必须修改-->
<Connectorport="8010" protocol="AJP/1.3"redirectPort="8443" />
<RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Hostname="localhost" appBase="webapp"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!—appBase name 必须要修改-->
<Context path="" docBase="/webapp/tomcat6/webapp/mbp/"reloadable="true" ></Context>
<!—docBase 调整至相应appBase属性 保证前方代理访问端口号自动调整到该服务-->
</Host>
</Engine>
</Service>
</Server>
<!--注意Service name、Engine name、appBase,端口号别忘了修改,以免重复无法启动 -->
第二步、在/webapp/tomcat6创建相应的部署目录
[root@localhosttomcat6]# mkdir webapp
同时将工程发布到的新建的部署目录 中
第三步、创建配置文件目录和配置文件 1)进入相应目录
[root@localhost tomcat6]# cd /webapp/tomcat6/conf
2)创建目录
[root@localhost conf]# mkdir Catalinai
3)拷贝配置文件
[root@localhostconf]# cp -r ./Catalina/localhost/ ./Catalinai
4)验证以上操作的正确性
[root@localhostconf]# cd Catalinai/
[root@localhost Catalinai]# cd localhost/
[root@localhostlocalhost]# pwd
/webapp/tomcat6/conf/Catalinai/localhost
[root@localhostlocalhost]# ls
host-manager.xml manager.xml
第四步、重启tomcat服务并验证相应端口服务已经正常运行
1) lsof–i:8080
2) lsof–i:8090
如果返回相应java进程则证明配置无误。
二、附tomcat前端Nginx代理相关的调整和配置
##以下为测试环境下Nginx配置文件的详细内容,关键配置点参见蓝色标注的文字亲
user www www;
worker_processes 2;
error_log /usr/local/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
#include/usr/local/nginx/conf/proxy.conf;
#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;
server_names_hash_bucket_size128;
client_header_buffer_size 32k;
large_client_header_buffers 832k;
# client_max_body_size 8m;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
##cache##
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size128k;
proxy_temp_path/usr/local/nginx/temp;
proxy_cache_path/usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1dmax_size=30g;
##end##
# upstream 124.128.153.56 {
# server http://10.0.211.4/sdws/;
# serverhttp://10.0.211.5/sdws/;
# serverhttp://10.0.211.6/sdws/;
# }
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
index index.html index.htm index.jsplogin.jsp;
#access_log logs/host.access.log main;
root /webapp/tomcat6/webapps/sdmp;
location ~.*\.(jsp|do|action)$ #设置动态数据的请求url
{
index index.jsp;
proxy_pass http://localhost:8080; #动态请求转向处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
}
location~.*\.(gif|jpg|jpeg|png|bmp|swf|html)$ #静态数据保存时效
{
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host$host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}
location ~.*\.(js|css)?$ #js和css保存时效
{
expires 1h;
}
location ~(favicon.ico)
{
log_not_found off;
expires 30d;
break;
}
location /status #统计Nginx运行状态
{
stub_status on;
access_log/usr/local/nginx/logs/status.log;
auth_basic "NginxStatus";
allow 10.0.209.0/24;
deny all;
}
#location / {
# root html;
# index index.html index.htm;
# }
error_page 404 /404.html;
# redirect server errorpages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts toApache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts toFastCGI 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 .htaccessfiles, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
##add by fengzhanhai 3qiwangluotai
server {
listen 81;
server_name localhost;
#charset koi8-r;
charset utf-8;
index index.html index.htm index.jsplogin.jsp;
#access_log logs/host.access.log main;
root /webapp/tomcat6/webapp/mbp/;
location ~.*\.(jsp|do|action)$ #设置动态数据的请求url
{
index index.jsp;
# root /webapp/tomcat6/webapps/mbp/;
proxy_pass http://localhost:8090;#动态请求转向处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
}
location~.*\.(gif|jpg|jpeg|png|bmp|swf|html)$ #静态数据保存时效
{
proxy_pass http://localhost:8090;
# root/webapp/tomcat6/webapps/mbp/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}
location ~.*\.(js|css)?$ #js和css保存时效
{
expires 1h;
}
location ~(favicon.ico)
{
log_not_found off;
expires 30d;
break;
}
}
# another virtual host usingmix 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;
# }
#}
server {
listen 82;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_passhttp://127.0.0.1:8080/sdws/;
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server errorpages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$
{
root/webapp/tomcat6/webapps/sdws;
if (-f $request_filename){
expires 1d;
break;
}
}
location ~ .*\.(js|css)$
{
root/webapp/tomcat6/webapps/;
if (-f $request_filename){
expires 1d;
break;
}
}
}
####################################################################################
# server {
# listen 811;
# server_name localhost;
#
# #charset koi8-r;
#
# #access_log logs/host.access.log main;
#
# location / {
# proxy_passhttp://127.0.0.1:8080/mbp/;
# proxy_set_headerHost $host;
# proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
# }
#
# #error_page 404 /404.html;
#
# # redirect server error pages to thestatic page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
# {
# root/webapp/tomcat6/webapps/mbp;
# if (-f$request_filename) {
# expires 1d;
#break;
# }
# }
#location ~ .*\.(js|css)$
#{
# root/webapp/tomcat6/webapps/mbp;
# if (-f$request_filename) {
# expires 1d;
# break;
# }
# }
#}
#-->
#add by fengzhanhai
server {
listen 810;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_passhttp://127.0.0.1:8080/mbp/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect servererror pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
location ~.*\.(gif|jpg|jpeg|bmp|swf|png)$
{
root/webapp/tomcat6/webapps/mbp;
#http://127.0.0.1:8080/sdmp3
if (-f$request_filename) {
expires 1d;
break;
}
}
location ~.*\.(js|css)$
{
#http://127.0.0.1:8080/sdmp3
root/webapp/tomcat6/webapps/;
if (-f $request_filename){
expires 1d;
break;
}
}
location~(favicon.ico)
{
log_not_foundoff;
expires 30d;
break;
}
}
}