分类: 架构设计与优化
2013-02-21 22:17:38
原文地址:Nginx与Tomcat多端口服务配置实例分享 作者:fengzhanhai
由于技术研发部在开发三期业务时没有考虑到与二期业务的兼容性,为了保证三期业务上线时不影响二期用户客户端的使用,需要在同一tomcat下部署两个工程。但由于这两个工程之间需要相互调用,据博主测试这两个工程如果再同一端口下运行时总会有一个工程无法正常请求另一个工程的业务(研发部门未经测试直接交付实施上线【太可怕了】),所以在部署业务时,笔者为了保证业务按时上线采用了在一个Tomcat配置多个端口号以对应不同服务完成业务的正常交付。
一、在Tomcat下配置多个服务,用不同的端口号。下面以增加1个端口号为例 。具体实施步骤如下所示:
第一步、修改 vi /webapp/tomcat6/conf/server.xml文件
具体修改内容如下:
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" />
connectionTimeout="20000"
redirectPort="8443" />
resourceName="UserDatabase"/>
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
-->
servicename不一致-->
connectionTimeout="20000"
redirectPort="8443" />
-->
resourceName="UserDatabase"/>
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
-->
appBase属性 保证前方代理访问端口号自动调整到该服务-->
第二步、在/webapp/tomcat6创建相应的部署目录
1)进入相应目录
[root@localhost tomcat6]# cd /webapp/tomcat6/conf
2)创建目录
[root@localhost conf]# mkdir Catalinai
3)拷贝配置文件
[root@localhost
conf]# cp -r ./Catalina/localhost/ ./Catalinai
4)验证以上操作的正确性
[root@localhost
conf]# cd Catalinai/
[root@localhost Catalinai]# cd localhost/
[root@localhost
localhost]# pwd
/webapp/tomcat6/conf/Catalinai/localhost
[root@localhost
localhost]# 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_size
128;
client_header_buffer_size 32k;
large_client_header_buffers 8
32k;
# 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_size
128k;
proxy_temp_path
/usr/local/nginx/temp;
proxy_cache_path
/usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d
max_size=30g;
##end##
# upstream 124.128.153.56 {
# server ;
# server
# server
# }
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
index index.html index.htm index.jsp
login.jsp;
#access_log logs/host.access.log main;
root /webapp/tomcat6/webapps/sdmp;
location ~.*\.(jsp|do|action)$ #设置动态数据的请求url
{
index index.jsp;
proxy_pass #动态请求转向处理
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
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 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
#}
# 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;
#}
}
##add by fengzhanhai 3qiwangluotai
server {
listen 81;
server_name localhost;
#charset koi8-r;
charset utf-8;
index index.html index.htm index.jsp
login.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 动态请求转向处理
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
# 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 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;
# }
#}
server {
listen 82;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass
proxy_set_header
Host $host;
proxy_set_header
X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
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_pass
# proxy_set_header
Host $host;
# proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
# }
#
# #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;
# }
# 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_pass
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
location ~
.*\.(gif|jpg|jpeg|bmp|swf|png)$
{
root
/webapp/tomcat6/webapps/mbp;
#
if (-f
$request_filename) {
expires 1d;
break;
}
}
location ~
.*\.(js|css)$
{
#
root
/webapp/tomcat6/webapps/;
if (-f $request_filename)
{
expires 1d;
break;
}
}
location
~(favicon.ico)
{
log_not_found
off;
expires 30d;
break;
}
}
}
[root@localhost
tomcat6]# mkdir webapp
同时将工程发布到的新建的部署目录 中
第三步、创建配置文件目录和配置文件