Nginx整合Tomcat

Nginx是很优秀的WEB软件,性能上比Apache好很多,特别是在处理静态内容方面尤其突然。所以如果Nginx+Tomcat的话,会比Apache+Tomcat要好。
原理是,Nginx主要负责处理静态内容,而当接收到jsp动态页面的请求时,通过proxy转发到tomcat。当然,同时还可以支持php.

安装JRE

wget  http://download.oracle.com/otn-pub/java/jdk/7u3-b04/jre-7u3-linux-x64.rpm
rpm -ivh jre-7u3-linux-x64.rpm

修改环境变量

vi /etc/profile

加入以下内容:

JAVA_HOME="/usr/java/jre1.7.0_03"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME

应用变更:

source /etc/profile

安装TOMCAT

wget http://apache.etoak.com/tomcat/tomcat-7/v7.0.26/bin/apache-tomcat-7.0.26.tar.gz
tar xzvf apache-tomcat-7.0.26.tar.gz
mkdir -p /usr/local/tomcat/
cd apache-tomcat-7.0.26
mv apache-tomcat-7.0.26/* /usr/local/tomcat/

配置Tomcat的server.xml

cd /usr/local/tomcat/
vi /usr/local/tomcat/conf/server.xml

在<Host></Host>之间点加Context:

<Context path="" docBase="/var/www/test.admclub.com/WebContent/" />

创建虚拟主机主目录:

mkdir /var/www/test.admlcub.com

安全起见,需要创建一个tomcat用户,并设置Tomcat主目录和主机目录权限。防止提权:

useradd tomcat
chown -R tomcat:tomcat /usr/local/tomcat
chown -R www:www /var/www/test.admlcub.com
su tomcat
/usr/local/tomcat/bin/startup.sh

若能输出类似下以内容,则表示Tomcat成功启动:

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jre1.7.0_03
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

OK,Tomcat已经成功安装并启动了。那么接下来需要配置一下Nginx了。注意一点,在本教程中的Nginx是已经正在生产环境中的了,所以不但不需要重新安装,而且在接入Tomcat的过程中不需要重启Nginx。如果您不了解如果安装Nginx,请查看:Nginx安装教程。

cd /usr/local/nginx/conf
vi test.admclub.com.conf

添加以下内容:

server {
	listen x.x.x.x:80;
	server_name test.admclub.com;

	location ~ \.php$ {
            root          /var/www/test.admclub.com/WebContent;
            fastcgi_pass    backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/test.admclub.com/WebContent/$fastcgi_script_name;
            include        fastcgi_params;
	     #track_uploads proxied 30s;
		client_max_body_size 500m;
	    #fastcgi_index  index.php?IF_REWRITE=1;
	     if ( $fastcgi_script_name ~ \..*\/.*php ) {
			return 403;
	     }
        }

	location / {
            root   /var/www/test.admclub.com/WebContent;
            index  index.html index.htm index.jsp index.jsp;
	}

	 location ~ \.(jsp|jspx|do)?$ {
   		proxy_set_header  Host $host;
		proxy_set_header  X-Real-IP  $remote_addr;
		proxy_pass http://test.admclub.com:8080;
 	}
    }

保存,退出。接着需要修改 nginx.conf

vi /usr/local/nginx/conf/nginx.conf

需要在http{

}

之间添加

include test.admclub.com.conf;

大概是这样子的:

http{
...
...
include test.admclub.com.conf;
}

为确保配置文件的正确性,最好对新的配置进行检验:

/usr/local/nginx/sbin/nginx -t

如果输出如下,则表示配置正确。可以重新加载Nginx.

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载Nginx的配置文件:

service nginx reload

好了,到此Nginx就能处理JSP和PHP程序了。

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>