nginx+apache+nginx tomcat 配置,为什么不直接nginx+nginx tomcat 配置

Nginx+Tomcat的服务器端环境配置详解
投稿:goldensun
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Nginx+Tomcat的服务器端环境配置详解,包括Nginx与Tomcat的监控开启方法,需要的朋友可以参考下
Nginx+tomcat是目前主流的java web架构,如何让nginx+tomcat同时工作呢,也可以说如何使用nginx来反向代理tomcat后端均衡呢?直接安装配置如下:
1、JAVA JDK安装:
#下载相应的jdk软件包,然后解压安装,我这里包名称为:jdk-7u25-linux-x64.tar.gz&&&
tar -xzf jdk-7u25-linux-x64.tar.mkdir -p /usr/java/ ;mv jdk1.7.0_25/ /usr/java/ 下.
#然后配置环境变量,这样可以任何地方引用jdk,如下配置:&&&
#vi /etc/profile 最后面加入以下语句:&&&
export JAVA_HOME=/usr/java/jdk1.7.0_25
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
#source /etc/profile&& #使环境变量马上生效&&
#java& --version&&& #查看java版本,看到jdk1.7.0_25版本即代表java jdk安装成功。
2、Nginx安装:
wget http://nginx.org/download/nginx-1.2.6.tar.gz
useradd www
tar zxvf nginx-1.2.6.tar.gz
cd nginx-1.2.6/
./configure --user=www --group=www --prefix=/usr/local/nginx
\--with-http_stub_status_module --with-http_ssl_module
make && make install
#Nginx安装完毕,然后使用命令:/usr/local/nginx/sbin/nginx -t 测试OK,代表nginx安装成功。&&
/usr/local/nginx/sbin/nginx 回车启动nginx,可以通过访问http://ip/看到nginx默认页面。
3、Tomcat安装:
#官方网站下载tomcat 6.0.30或者其他版本:&&&
cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz
#直接解压就可以使用,解压完成执行,同时拷贝两个tomcat,命名为tomcat1 tomcat2&&&
mv apache-tomcat-6.0.30 /usr/local/tomcat1
cp /usr/local/tomcat1 /usr/local/tomcat2 -r
#分别修改tomcat1和tomcat2 端口,这里有三个端口需要修改,分别如下:&&
shutdown 端口:8005& 主要负责启动关闭.&&
ajp端口:8009 主要负责通过ajp均衡(常用于apache和tomcat整合)&&
http端口:8080 可以通过web页面直接访问(nginx+tomcata整合)&&
#注* 如果tomcat1三个端口分别为:80 ,那么tomcat2端口在此基础上都+1,即为:81&&
#一台服务器上,端口不能重复,否则会报错。&&
#修改完端口后,然后启动两个tomcat,启动命令为:&&
#如何提示没有这个文件或者权限不足,需要tomcat 的bin目录对sh文件赋予执行权限:chmod o+x&& *.sh&&&
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
#启动后,使用netstat -tnl 可以看到6个端口,即代表tomcat1 tomcat2成功启动。你可以使用http://ip:8080& http://ip:8081访问tomcat默认页面。&
#如果需要修改tomcat发布目录为自己制定的目录,需要做如下调整,创建两个发布目录:
mkdir -p /usr/webapps/{www1,www2}
编辑vi /usr/local/tomcat1/conf/server.xml 在最后&/Host&前一行加下内容:
&Context path="" docBase="/usr/webapps/www1" reloadable="false"/&
编辑vi /usr/local/tomcat2/conf/server.xml 在最后&/Host&前一行加下内容:
&Context path="" docBase="/usr/webapps/www2" reloadable="false"/&
tomcat1发布目录内容:
&h1&TOMCAT_1 JSP Test Page&/h1&
&%=new java.util.Date()%&
tomcat2发布目录内容:
&h1&TOMCAT_2 JSP Test Page&/h1&
&%=new java.util.Date()%&
然后访问http://ip:查看测试内容。
4、Nginx+tomcat整合:
整合主要是修改nginx.conf配置,给一个完整的nginx.conf线上配置,部分参数可以自己根据实际需求修改:
worker_processes 8;
pid /usr/local/nginx/nginx.
worker_rlimit_nofile 102400;
worker_connections 102400;
default_type application/octet-
fastcgi_intercept_
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
keepalive_timeout 60;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout
proxy_send_timeout
proxy_buffer_size
proxy_buffers
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip_min_length 1k;
gzip_buffers
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types
text/plain application/x-javascript text/css application/
### change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
upstream web_app {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
listen 80;
index index.jsp index.html index.
#发布目录/data/www
root /data/
location /
proxy_next_upstream http_502 http_504 error timeout invalid_
proxy_set_header Host $
proxy_set_header X-Real-IP $remote_
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_
proxy_pass http://web_
#注* server段 proxy_pass定义的web_app需要跟upstream 里面定义的web_app一致,否则server找不到均衡。&&&
#如上配置,nginx+tomcat反向代理负载均衡配置完毕,如果要做动静分离,只需要在nginx添加如下配置就OK了。
&#配置Nginx动静分离&&&
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
root /data/
#expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
5、开启nginx的监控
1)、nginx简单状态监控
在nginx.conf中添加如下代码即可监控nginx当前的状态,然后访问http://serverip/status即可访问
location /status {
一般显示为
Active connections: 16
server accepts handled requests
226 305915
Reading: 0 Writing: 1 Waiting: 15
ctive connections: 对后端发起的活动连接数.
Server accepts handled requests: Nginx总共处理了24个连接,成功创建24次握手(证明中间没有失败的),总共处理了129个请求.
Reading: Nginx 读取到客户端的Header信息数.
Writing: Nginx 返回给客户端的Header信息数.
Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.
注意的,本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定:
./configure –with-http_stub_status_module
&查看已安装的 Nginx 是否包含 stub_status 模块
#/usr/local/nginx/sbin/nginx -V
TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module
2)、nginx的图形化监控-nginx-RRD stats
nginx-rrd是nginx官方推荐的一款Nginx监控工具,利用nginx-rrd可以很方便的生成图表,便于我们查看。
a、运行环境(centos):
在安装前需要安装好rrdtool这个画图工具和相应的perl模块,可以先运行:
yum install rrdtool libhtml-parser-perl libwww-perl librrds-perl librrd2-dev
确保rrdtool和相应的perl被安装上。
b、安装配置
wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz
tar zxvf nginx-rrd-0.1.4.tgz
进入nginx-rrd目录,
cd nginx-rrd-0.1.4/
复制主程序:
cp usr/sbin/* /usr/sbin
复制配置文件
cp etc/nginx-rrd.conf /etc
复制定时执行文件:
cp etc/cron.d/nginx-rrd.cron /etc/cron.d
创建nginx-rrd生成目录:
mkdir /home/wwwroot/nginx && mkdir /home/wwwroot/nginx/rrd
cp html/index.php /home/wwwroot/nginx
编辑/home/wwwroot/nginx/index.php修改登录密码
header("Content-Type: text/ charset=utf-8");
$password = "admin";
编辑配置文件nginx-rrd.conf,修改完成后如下:
#####################################################
# dir where rrd databases are stored
RRD_DIR="/home/wwwroot/nginx-rrd/";
# dir where png images are presented
WWW_DIR="/home/wwwroot/nginx/";
# process nice level
NICE_LEVEL="-19";
BIN_DIR="/usr/sbin";
# servers to test
# server_server_name
SERVERS_URL="http://127.0.0.1/127.0.0.1"
多个server,可以SERVERS_URL中空格分开,前部分为nginx_status的地址,后面为被监控主机的域名。
SEVERS_URL 格式
注意,nginx.conf虚拟主机server{}中,需要已经加入:
location /status {
以上设置就完成,可以自行运行一下:/usr/sbin/nginx-collect ,启动收集程序。cron会15分钟生成一次数据。
如果没有定时执行生成数据,可以在/etc/crontab最后面加上:
* * * * * root /usr/sbin/nginx-collect
*/15 * * * * root /usr/sbin/nginx-graph
然后输入然后访问http://serverip/nginx/即可访问。
6、开启tomcat的监控
1)、tomcat6的配置
修改tomcat/conftomcat-users.xml文件中&/tomcat-users&节点之前添加如下代码即可。
&user username="admin" password="admin" roles="manager"/&
然后输入然后访问http://serverip:8080/manager/status即可访问。
2)tomcat7的配置
修改tomcat/conftomcat-users.xml文件中&/tomcat-users&节点之前添加如下代码即可。
&role rolename="manager-gui"/&
&user username="tomcat" admin="admin" roles="manager-gui"/&
然后输入然后访问http://serverip:8080/manager/status即可访问。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具用户名:陈明乾
文章数:162
评论数:819
访问量:1232409
注册日期:
阅读量:1297
阅读量:3317
阅读量:446558
阅读量:1131848
51CTO推荐博文
大纲一、Tomcat 基本配置1.为Tomcat提供SysV脚本 2.catalina 脚本讲解 3.telnet 登录管理Tomcat 4.配置Tomcat虚拟主机 5.Tomcat图形管理接口manager 管理应用程序的部署及监控 host-manager 虚拟主机的管理 6.部署JSP网站案例布署JavaCenter Home(开源的SNS网站) 二、Nginx反向代理Tomcat服务器1.Nginx将请求反向代理到后端Tomcat 2.Nginx将图片缓存到本地 3.Nginx将请求实现动静分离注,实验环境说明,操作系统:CentOS 6.4 x86_64,软件版本:jdk-7u40、apache-tomcat-7.0.42、Nginx-1.4.2,博客中所用到的软件请到这里下载:。一、Tomcat 基本配置1.为Tomcat提供SysV脚本 注,在上一篇博文中我们已经演示安装了Tomcat,这里我们就不在演示,不清楚的博友可以参考这篇博文,,在上一篇博文中我们没有增加,SysV脚本,在这篇博文中我们来增加一下,下面我们就来具体演示一下。[root@tomcat ~]# vim /etc/init.d/tomcat
# Tomcat init script for Linux.
# chkconfig:
# description: The Apache Tomcat servlet/JSP container.
CATALINA_HOME=/usr/local/tomcat #注意你的脚本路径
export CATALINA_HOME
# export CATALINA_OPTS="-Xms128m -Xmx256m"
exec $CATALINA_HOME/bin/catalina.sh $*下面我们来增加执行权限,并加入服务列表设置开机自启动,[root@tomcat ~]# chmod +x /etc/init.d/tomcat
[root@tomcat ~]# chkconfig --add tomcat
[root@tomcat ~]# chkconfig tomcat --listtomcat 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭下面我们来启动一下Tomcat并测试一下,[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar查看一下启动的端口号,[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1121/master
0 127.0.0.1:6010
12988/sshd
0 127.0.0.1:6011
13053/sshd
13088/java
1121/master
0 ::1:6010
12988/sshd
0 ::1:6011
13053/sshd
0 ::ffff:127.0.0.1:8005
13088/java
13088/java用浏览器访问一下,好了,到这里Tomcat的SysV脚本增加完成,下面我们来说一下catalina脚本。2.catalina 脚本讲解首先我们来查看一下这个脚本,[root@tomcat bin]# catalina.sh -h
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.7.0_40
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
Start Catalina in a debugger
debug -security
Debug Catalina with a security manager
jpda start
Start Catalina under JPDA debugger
Start Catalina in the current window
run -security
Start in the current window with security manager
Start Catalina in a separate window
start -security
Start in a separate window with security manager
Stop Catalina, waiting up to 5 seconds for the process to end
Stop Catalina, waiting up to n seconds for the process to end
stop -force
Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force
Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest
Run a basic syntax check on server.xml - check exit code for result
What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined注,从帮助上来看,这个脚本使用还是挺简单的。下面我们来说几个常用的选项,catalina.shstart 启动Tomcat stop 关闭Tomcat configtest 测试配置文件是否有错 version 查看Tomcat版本 下面我们就来具体演示一下,[root@tomcat ~]# catalina.sh configtest
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.7.0_40
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
九月 21, :26 下午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
九月 21, :27 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
九月 21, :27 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
九月 21, :27 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1269 ms注,使用configtest选项时,得关闭Tomcat,不然会报错。启动Tomcat,[root@tomcat ~]# catalina.sh start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.7.0_40
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar关闭Tomcat,[root@tomcat ~]# catalina.sh stop
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.7.0_40
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar查看Tomcat版本,[root@tomcat ~]# catalina.sh version
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.7.0_40
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.42
Server built:
Server number: 7.0.42.0
OS Version:
2.6.32-358.el6.x86_64
Architecture:
JVM Version:
1.7.0_40-b43
JVM Vendor:
Oracle Corporation好了,catalina脚本,我们就说到这里了,下面我们来说一下telnet管理Tomcat。3.telnet 登录管理Tomcat注,在说telnet管理Tomcat之前,我们得先看一下默认的配置文件,这里面定义了默认的管理端口,[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
&Server port="8005" shutdown="SHUTDOWN"&说明,定义了一个管理端口为8005,我们可以用telnet直接登录进本机的8005端口,来执行SHUTDOWN命令,来关闭Tomcat实例。下面我们来具体演示一下,先安装telnet客户端,[root@tomcat ~]# yum install -y telnet下面我们一测试并查看,[root@tomcat ~]# telnet localhost 8005
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SHUTDOWN #输入SHOWDOWN就可以直接关闭Tomcat服务。
Connection closed by foreign host.
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1121/master
0 127.0.0.1:6010
12988/sshd
0 127.0.0.1:6011
13053/sshd
1121/master
0 ::1:6010
12988/sshd
0 ::1:6011
13053/sshd注,大家可以看到Tomcat服务器已经关闭。好了,telnet管理我们就说到这里,下面我们来说一下,Tomcat虚拟主机的配置。4.配置Tomcat虚拟主机注,在说Tomcat虚拟主机之前,咱们来详细的看看默认的配置文件,虽然在上一篇博客中全部有讲解,在这篇博客中我还是再和大家简单说一下,下面是默认配置文件。大家可以看到,绝大部分的配置文件是注释,包含在&!-- --&、全是注释。下面我们就来具体的看看,注释我们就不说了,说具体的定义的内容[root@tomcat ~]# cat /usr/local/tomcat/conf/server.xml
&?xml version='1.0' encoding='utf-8'?&
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
&!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
&Server port="8005" shutdown="SHUTDOWN"& #大家可以看到,这里是我们刚才讲解的,定义一个管理接口
&!-- Security listener. Documentation at /docs/config/listeners.html
&Listener className="org.apache.catalina.security.SecurityListener" /&
&!--APR library loader. Documentation at /docs/apr.html --&
&Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /&
&!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --&
&Listener className="org.apache.catalina.core.JasperListener" /&
&!-- Prevent memory leaks due to use of particular java/javax APIs--&
&Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /&
&Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /&
&Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /&
&!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
&GlobalNamingResources&
&!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
&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&
&!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
&Service name="Catalina"& #定义一个Service命令为Catalina
&!--The connectors can use a shared executor, you can define one or more named thread pools--&
&Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/&
&!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
&Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /& #这里定义了一个连接器,协议为http,端口为8080,最大连接超时为20s,这里还定义了一个SSL的重定向端口8443。我们可以根据需要进行修改。一般我们都用80端口与443端口。
&!-- A "Connector" using the shared thread pool--&
&Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /&
&!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation --&
&Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" /& #这里定义了一个SSL的案例,主要定义相关密钥与证书。
&!-- Define an AJP 1.3 Connector on port 8009 --&
&Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /& #这里定义了一个支持AJP协议的连接器。
&!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html --&
&!-- You should set jvmRoute to support load-balancing via AJP ie :
&Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"&
&Engine name="Catalina" defaultHost="localhost"& #这里定义了一个名为Catalina的引擎,并定义了一个默认主机为localhost。
&!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) --&
&Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&
&!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack --&
&Realm className="org.apache.catalina.realm.LockOutRealm"&
&!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. --&
&Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/&
&Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"& #这里就是定义的虚拟主机。
&!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html --&
&Valve className="org.apache.catalina.authenticator.SingleSignOn" /&
&!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" --&
&Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &%r& %s %b" /& #这里定义了一个日志相关的属性。
&/Service&
&/Server&好了,在这里我们又简单的说明一下,配置文件下面我们就来具体演示一下怎么配置虚拟主机。首先,我们来修改一下配置文件,[root@tomcat conf]# vim server.xml #增加下面几行&Host name="" appBase="/web/webapp"
unpackWARs="true" autoDeploy="true"&
&Context path="/" docBase="/web/webapp" reloadable="true"/&
&/Host&接下来我们来创建文档目录与测试页面,[root@tomcat ~]# mkdir -pv /web/webapp
[root@tomcat ~]# cd /web/webapp
[root@tomcat webapp]# vim index.jsp
&%@ page language="java" %&
&%@ page import="java.util.*" %&
&title&JSP test page.&/title&
&% out.println("Welcome to test. Site, "); %&
&/html&现在我们来测试一下我们修改的配置文件,[root@tomcat ~]# service tomcat stop
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
^[[A[root@tomcat service tomcat configtest
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 22, :47 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 22, :47 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 22, :47 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 22, :47 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1294 ms注,大家可以看到,我们这里没有报错,说明配置都是正确的,若配置有错误,会在最后一行提醒你。再下面我们来启动Tomcat并测试一下,[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1121/master
0 127.0.0.1:6010
13368/sshd
0 127.0.0.1:6011
13387/sshd
0 127.0.0.1:6012
13407/sshd
13557/java
1121/master
0 ::1:6010
13368/sshd
0 ::1:6011
13387/sshd
0 ::1:6012
13407/sshd
13557/java注,还有一点得说明一下,我这里为了方便测试,将默认端口8080修改为了80端口。下面是测试效果,为了帮助大家理解,我们这里再次讲解一下,Host组件与Context组件以及相关属性。Host组件:位于Engine容器中用于接收请求并进行相应处理的主机或虚拟主机,如前面我们自定义的内容:&Host name="" appBase="/web/webapp"
unpackWARs="true" autoDeploy="true"&
&Context path="/" docBase="/web/webapp" reloadable="true"/&
&/Host&常用属性说明:name:定义虚拟主机的域名appBase:此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件的目录路径;可以使用基于$CATALINA_HOME的相对路径; autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true; unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true; 主机别名定义:如果一个主机有两个或两个以上的主机名,额外的名称均可以以别名的形式进行定义,如下: &Host name="" appBase="webapps" unpackWARs="true"&
&Alias&&/Alias&
Context组件:
Context在某些意义上类似于apache中的路径别名,一个Context定义用于标识tomcat实例中的一个Web应用程序。如下面的定义:
&!-- Tomcat Root Context --&
&Context path="" docBase="/web/webapps"/&
&!-- buzzin webapp --&
&Context path="/bbs"
docBase="/web/threads/bbs"
reloadable="true"&
&/Context&
&!-- chat server --&
&Context path="/chat" docBase="/web/chat"/&
&!-- darian web --&
&Context path="/darian" docBase="darian"/&在Tomcat中,每一个context定义也可以使用一个单独的XML文件进行,其文件的目录为$CATALINA_HOME/conf/&engine name&/&host name&。可以用于Context中的XML元素有Loader,Manager,Realm,Resources和WatchedResource。常用的属性定义有:docBase:相应的Web应用程序的存放位置;也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径;切记,docBase的路径名不能与相应的Host中appBase中定义的路径名有包含关系,比如,如果appBase为deploy,而docBase绝不能为deploy-bbs类的名字; path:相对于Web服务器根路径而言的URI;如果为空“”,则表示为此webapp的根路径;如果context定义在一个单独的xml文件中,此属性不需要定义; reloadable:是否允许重新加载此context相关的Web应用程序的类;默认为false; 为了便于大家理解,我们这里再定义一个Context并测试一下,我们先来修改一下配置文件[root@tomcat conf]# vim server.xml
&Host name="" appBase="/web/webapp"
unpackWARs="true" autoDeploy="true"&
&Context path="/" docBase="/web/webapp" reloadable="true"/&
&Context path="/test" docBase="/web/test" reloadable="true"/& #增加这一行
&/Host&下面来增加目录文档与测试文件,[root@tomcat webapp]# mkdir /web/test
[root@tomcat webapp]# cd /web/test
[root@tomcat test]# vim index.jsp
&%@ page language="java" %&
&head&&title&TomcatA&/title&&/head&
&h1&&font color="red"&TomcatA &/h1&
&table align="centre" border="1"&
&td&Session ID&/td&
&% session.setAttribute("abc","abc"); %&
&td&&%= session.getId() %&&/td&
&td&Created on&/td&
&td&&%= session.getCreationTime() %&&/td&
&/html&测试一下配置文件是否有错并启动Tomcat,[root@tomcat ~]# service tomcat configtest
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 22, :57 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 22, :58 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 22, :58 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 22, :58 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1352 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1121/master
0 127.0.0.1:6010
13587/sshd
0 127.0.0.1:6011
13387/sshd
0 127.0.0.1:6012
13407/sshd
13945/java
1121/master
0 ::1:6010
13587/sshd
0 ::1:6011
13387/sshd
0 ::1:6012
13407/sshd
0 ::ffff:127.0.0.1:8005
13945/java
13945/java下面我们就用浏览器测试一下,好了,到这里我们的Tomcat虚拟主机的讲解就到这里了,下面我们来说一下Tomcat图形管理接口。5.Tomcat图形管理接口manager 管理应用程序的部署及监控 host-manager 虚拟主机的管理 我们先来看一下默认的图形配置界面,注,大家注意看右上角,我用红色方框标记出来的,大家可以看有三个按钮,分别为Server Status 主要用来查看服务器的状态 Manager App 主要用来管理应用程序的部署及监控 Host Manager 主要用来管理虚拟主机 下面我们就来具休的配置一下,大家可以看到,你点击任何一个按钮都要输入用户名和密码的,在我们配置之前我们先来说一下,Tomcat的Manager功能,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 注,这里我说一下,上面的英文比较简单我就不在里翻译了,大家自己看一下。下面我们就来启用manager功能,修改tomcat-user.xml文件,添加如下行:&role rolename="manager-gui"/&
&role rolename="admin-gui"/&
&user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/&简单解释一下,Tomcat有内置的角色,我们这里增加了两个角色一个为manager-gui,另一个为admin-gui,用户名和密码都为tomcat。注,增加的内容一定要在&tomcat-users&&/tomcat-users&之间。不然,不会生效。好了,下面我们一来测试一下配置文件,并重新启动一下Tomcat[root@tomcat ~]# service tomcat configtest
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 22, :44 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 22, :44 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 22, :44 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 22, :44 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1213 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1121/master
0 127.0.0.1:6010
13587/sshd
0 127.0.0.1:6011
13387/sshd
0 127.0.0.1:6012
13407/sshd
14197/java
1121/master
0 ::1:6010
13587/sshd
0 ::1:6011
13387/sshd
0 ::1:6012
13407/sshd
0 ::ffff:127.0.0.1:8005
14197/java
14197/java好了,下面我们用浏览器查看一下,注,点击Server Status按钮,让你输入用户名和密码。我这里全部设置是tomcat。然后,会出现以下界面。显示全部服务器运行状态!大家可以仔细的看一下,我就不带着大家看了。下面是应用程序部署管理界面,下面是虚拟主机管理页面,注,我们一般在生产环境中用的最多是应用程序部署界面,可以进行热布署应用程序,很方便,大家可以尝试一下。好了,图形管理界面我们就说到这里了,下面我们来说一下,Tomcat的一个小案例。我们说了那么多,有朋友就会说了,怎么一个案例也没有呢?下面我们就来布署一个社交网站的案例JavaCenter Home。6.部署JSP网站案例首页,我们来修改一下配置文件,[root@tomcat conf]# vim server.xml
&Host name="" appBase="/web"
unpackWARs="true" autoDeploy="true"&
&Context path="/" docBase="webapp" reloadable="true"/&
&/Host&注,增加一下虚拟主机,文件目录为/web/webapp。下面我们来解压一下我们下载好的JavaCenter Home网站程序,[root@tomcat src]# tar xf JavaCenter_Home_2.0_GBK.tar.bz2接下来将解压好的JavaCenter Home程序移动到/web/webapp下,[root@tomcat src]# cd JavaCenter_Home_2.0_GBK
[root@tomcat JavaCenter_Home_2.0_GBK]# mv * /web/webapp/下面我们来测试一下配置文件并启动Tomcat服务,[root@tomcat ~]# service tomcat configtest
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.6.0_21
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 23, :18 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/jdk1.6.0_21/jre/lib/amd64/server:/usr/java/jdk1.6.0_21/jre/lib/amd64:/usr/java/jdk1.6.0_21/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 23, :20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 23, :20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 23, :20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2493 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.6.0_21
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1256/master
0 127.0.0.1:6010
0 0.0.0.0:3306
1165/mysqld
1256/master
0 ::1:6010
1499/java下面我们用浏览器访问一下,(注,要想用域名访问,必须配置本机有hosts文件,Windows7hosts文件目录,C:\Windows\System32\drivers\etc\hosts)注,上面的错误说明我们连接Mysql数据库失败。因为我们这里还没有安装与配置嘛。下面我们赶快来配置一下,先安装数据库,我们这里就用yum安装一下,[root@tomcat ~]# yum install -y mysql-server下面来启动并配置mysql,[root@tomcat ~]# service mysqld start
Starting mysqld:
[root@tomcat ~]# mysql
Welcome to the MySQL monitor. C or \g.
Your MySQL connection id is 2
Server version: 5.1.69 Source distribution
Copyright (c) , Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type '' or '\h' for help. Type '\c' to clear the current input statement.
mysql&好了,到这里我们mysql就安装完成了,下面我们来看一下我们的程序目录,[root@tomcat ~]# cd /web/webapp/
[root@tomcat webapp]# ls
avatar.jsp
userapp.jsp
admincp.jsp config.properties do.jsp
contact.jsp
editor.jsp
space.jsp xmlrpc.jsp
attachment
crossdomain.xml
favicon.ico invite.jsp network.jsp theme大家可以看到里面有个install的目录,下面我们用浏览器访问一下,,会跳出一个安装界面,如下图从图中,我们可以看出,所以环境配置完成,都符合要求。下面我们点击“接受授权协议,开始安装JavaCenter Home”,会跳出下一界面,如下图从图中,我们可以看出得输入,数据库名称、数据库用户名、数据库密码。下面我们就来增加一下,[root@tomcat ~]# mysql
Welcome to the MySQL monitor. C or \g.
Your MySQL connection id is 13
Server version: 5.1.69 Source distribution
Copyright (c) , Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type '' or '\h' for help. Type '\c' to clear the current input statement.
mysql& cre
Query OK, 1 row affected (0.00 sec)
mysql& grant all privileges on jcenter.* to jcenter@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql& grant all privileges on jcenter.* to jcenter@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)上面创建一个jcenter数据库,授权访问一个jcenter用户,密码为123456。下面我们继续配置,我们输入刚才设置的数据库用户名、数据库密码、数据库名称。点击“设置完毕,检测我的数据库配置”,图显示的3和4会看自动进行安装,我们只等一会即可。安装完成的效果如下图,下面我们开通一个管理员空间,用户名和密码都为admin,如下图点击“开通管理员空间”,会跳出另一个界面,如下图 我们点击“进入空间首页”,效果如下图好了,到这里我们的JavaCenter Home就全部配置完成了,我们第一阶段的基本配置就这里全部完成,下面我们主要讲解Nginx反向代理Tomcat服务器。二、Nginx反向代理Tomcat服务器0.测试环境准备阶段下面先看一下实验拓扑,接着来同步各节点时间,[root@tomcat ~]# ntpdate 202.120.2.101
[root@nginx ~]# ntpdate 202.120.2.101下面我们来安装nginx服务器,首先来解决nginx的依赖关系,[root@nginx ~]# yum groupinstall -y "Development Tools" "Server Platform Deveopment"
[root@nginx ~]# yum install -y openssl-devel pcre-devel下面我们来新建nginx用户,[root@nginx ~]# groupadd -r -g 108 nginx
[root@nginx ~]# useradd -r -g 108 -u 108 nginx
[root@nginx ~]# id nginx
uid=108(nginx) gid=108(nginx) 组=108(nginx)接着我们来开始编译和安装,[root@nginx src]# tar xf nginx-1.4.2.tar.gz
[root@nginx src]# cd nginx-1.4.2
[root@nginx nginx-1.4.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.4.2]# ./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
[root@nginx nginx-1.4.2]# make && make install说明:Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。下面我们为nginx提供SysV init脚本,[root@nginx ~]# cat /etc/init.d/nginx
# nginx - this script starts and stops the nginx daemon
# chkconfig:
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
proxy and IMAP/POP3 proxy server
# processname: nginx
/etc/nginx/nginx.conf
/etc/sysconfig/nginx
# pidfile:
/var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2&&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2&&1 | grep 'configure arguments:'`
for opt in $ do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
[ $retval -eq 0 ] && touch $lockfile
return $retval
echo -n $"Stopping $prog: "
killproc $prog -QUIT
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
restart() {
configtest || return $?
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
force_reload() {
configtest() {
$nginx -t -c $NGINX_CONF_FILE
rh_status() {
status $prog
rh_status_q() {
rh_status &/dev/null 2&&1
case "$1" in
rh_status_q && exit 0
rh_status_q || exit 0
restart|configtest)
rh_status_q || exit 7
force-reload)
force_reload
condrestart|try-restart)
rh_status_q || exit 0
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
esac而后为此脚本赋予执行权限,[root@nginx ~]# chmod +x /etc/init.d/nginx添加至服务管理列表,并让其开机自动启动, [root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on而后就可以启动服务并测试了,[root@nginx ~]# service nginx start
正在启动 nginx:
[root@nginx ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:80
14006/nginx
0 0.0.0.0:22
0 127.0.0.1:25
1105/master
0 127.0.0.1:6010
1105/master
0 ::1:6010
1345/sshd下面是测试结果,好了,到这里我们准备工作就全部完成了,下面们来简单的配置一下Nginx反向代理Tomcat服务器。1.Nginx将请求反向代理到后端Tomcat 首先,我们来修改一下nginx的配置文件,[root@nginx ~]# cd /etc/nginx/
[root@nginx nginx]# cp nginx.conf nginx.conf.bak
[root@nginx nginx]# vim nginx.conf
worker_processes 1;
#error_log logs/error.
#error_log logs/error.
#error_log logs/error.
logs/nginx.
worker_connections 1024;
default_type application/octet-
#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.
#keepalive_timeout 0;
keepalive_timeout 65;
#charset koi8-r;
#access_log logs/host.access.
location / {
#index index.html index.
proxy_pass http://192.168.18.201/; #注释默认两行,新增一行。
#error_page 404
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504 /50x.
location = /50x.html {
}重新加载一下配置文件,[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:
[确定]下面进行测试一下,(注,首先你得保证你的tomcat服务器能正常访问,下面我们先来访问一下tomcat服务器,如下图)大家可以看到我们的tomcat服务器可以正常访问,下面我们来看测试一下nginx可不可以进行反向代理。(注,还有问题,这里为了方便测试我们先将tomcat的默认主机设置为)[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
&Engine name="Catalina" defaultHost=""&重新启动一下tomcat并测试,[root@tomcat ~]# service tomcat stop
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.6.0_21
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:
/usr/local/tomcat
Using CATALINA_HOME:
/usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:
/usr/java/jdk1.6.0_21
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
0 0.0.0.0:22
0 127.0.0.1:25
1256/master
0 127.0.0.1:6010
0 127.0.0.1:6011
0 0.0.0.0:3306
2382/mysqld
1256/master
0 ::1:6010
0 ::1:6011
3299/java下面我们来访问测试一下tomcat服务器,下面我们来测试看nginx是否能代理tomcat服务器,好了,大家可以看到我们成功设置了nginx反向代理tomcat服务器。好了,大家可以看到,我们网站上有很多的图片,每次访问都要去后端的tomcat服务器上去取,很消耗服务器资源。我们下面将设置在nginx服务器上缓存图片。2.Nginx将图片缓存到本地 同样的,我们先来修改配置文件,[root@nginx nginx]# cat nginx.conf
worker_processes 1;
#error_log logs/error.
#error_log logs/error.
#error_log logs/error.
logs/nginx.
worker_connections 1024;
default_type application/octet-
#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.
#keepalive_timeout 0;
keepalive_timeout 65;
proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G; #新建缓存路径与相关属性
upstream backend { #建立后端tomcat服务器
server 192.168.18.201 weight=1;
#charset koi8-r;
#access_log logs/host.access.
location / {
#index index.html index.
#proxy_pass http://192.168.18.201/; #注释原来的代理设置
proxy_pass http://backend/; #启动后端服务器
location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" { #缓存图片与静态内容
proxy_pass http://
proxy_cache_valid 200 24h; #200状态缓存24小时
proxy_cache_valid 302 10m; #302状态缓存10分钟
add_header X-Cache-Status $upstream_cache_ #在http头部增加一个字段显示是否命令缓存
#error_page 404
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504 /50x.
location = /50x.html {
}下面我们来新建缓存目录,[root@nginx ~]# mkdir -pv /nginx/cache
mkdir: 已创建目录 "/nginx"
mkdir: 已创建目录 "/nginx/cache"测试一下配置文件是否有错,[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful重新加载配置文件,[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:
[确定]那么下面我们就来测试一下,大家可以看到我们访问的所有的静态内容都是命中的,X-Cache-Status: HIT,下面们来看一下缓存的目录,[root@nginx ~]# cd /nginx/cache/
[root@nginx cache]# ls
0 1 2 3 4 5 6 7 8 9 b c d e大家可以看到,缓存目录当中有我们缓存的内容,好了到这里我们的nginx缓存服务就配置完成了,下面我们看一下如何实现动静分离。3.Nginx将请求实现动静分离首先,我们来说一下我们要实现的效果,上面我们已经将静态内容缓存在nginx服务器上,我们想让用户请求的静态内容到nginx去取,动态内容到tomcat服务器上去取,这就能实现动静分享效果。同样的首先我们来修改配置文件,[root@nginx nginx]# cat nginx.conf
worker_processes 1;
#error_log logs/error.
#error_log logs/error.
#error_log logs/error.
logs/nginx.
worker_connections 1024;
default_type application/octet-
#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.
#keepalive_timeout 0;
keepalive_timeout 65;
proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G;
upstream backend {
server 192.168.18.201 weight=1;
#charset koi8-r;
#access_log logs/host.access.
index index.jsp index.
location ~* "\.(jsp|do)$" { #当请求的是jsp或do文件时直接到tomcat上去取
#index index.html index.
#proxy_pass http://192.168.18.201/;
#proxy_pass http://backend/;
proxy_pass http://
location = / {
rewrite ^/ http://192.168.18.201/index.
location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {
proxy_pass http://
proxy_cache_valid 200 24h;
proxy_cache_valid 302 10m;
add_header X-Cache-Status $upstream_cache_
#error_page 404
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504 /50x.
location = /50x.html {
}下面我们来检查一下配置文件是否有误,[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful重新加载一下配置文件,[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:
[确定]下面我们来访问测试一下,大家可以看到我们的静态内容来自缓存,动态内容全部代理到后端的tomcat服务器上了,说明我们动态分离配置完成,好了到这里我们的tomcat的基本配置与nginx反向代理tomcat的配置就全部完成了,最后希望大家有所收获^_^……本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
11:51:13 19:53:15 09:29:37 19:52:05 11:38:10}

我要回帖

更多关于 nginx和tomcat区别 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信