nginx+tomcat实现动静态分离
Posted 狼途
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat实现动静态分离相关的知识,希望对你有一定的参考价值。
===============Tomcat 概述:
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。目前最新版本是8.0。
===tomcat服务器中:
1》安装jdk并配置环境变量:
tar -xvf jdk-8u5-linux-x64.tar.gz
mv jdk1.8.0_05/ /usr/local/src/java
2》安装tomcat:
tar -xvf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/src/tomcat
3》配置tomcat:
#vim /usr/local/src/tomcat/conf/server.xml
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改端口为默认WEB端口80;
<Host name="localhost" appBase="che"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/opt/che">
新增最后一行修改为WEB的家目录,如果要做代理,则name为localhost;
4》启动tomcat:
5》tomcat启动测试:
=============nginx的安装与配置:
1》安装依赖库:
yum -y install gcc gcc-c++ openssl-devel zlib-devel pcre-devel
2》安装pcre库:
tar -xvf pcre-8.36.tar.gz
cd pcre-8.36
./configure && make && make install cd pcre-8.36
3》安装libmd5:
tar xf libmd5-0.8.2b.tar.gz
4》安装nginx:
ar -xvf nginx-1.9.8.tar.gz
cd nginx-1.9.8
CONFOPTS="
--user=user_00 \\
--group=users \\
--with-http_realip_module \\
--with-http_stub_status_module \\
--with-http_gzip_static_module \\
--with-md5=/soft/md5/ \\
--with-pcre=/usr/local/src/pcre-8.01 \\
--without-select_module \\
--without-poll_module \\
--without-http_ssi_module \\
--without-http_userid_module \\
--without-http_geo_module \\
--without-http_map_module \\
--without-http_memcached_module \\
--without-mail_pop3_module \\
--without-mail_imap_module \\
--without-mail_smtp_module \\
--prefix=/usr/local/services/nginx-1.9.8 \\
"
./configure $CONFOPTS && make && make install
编译参数详解:
--with-http_realip_module
此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。
-with-http_stub_status_module
这个模块可以取得一些nginx的运行状态,
--with-http_gzip_static_module
这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。
--with-md5=/soft/md5/
设定md5库文件路径
--with-sha1=auto/lib/sha1
设定sha1库文件路径
--with-pcre=/soft/pcre-8.01
设定PCRE库路径
--without-select_module
标准连接模式。默认情况下自动编译方式。您可以启用或禁用通过使用-select_module和不带- select_module配置参数这个模块
--without-poll_module
不使用poll模块
--without-http_ssi_module
不使用ngx_http_ssi_module模块,此模块处理服务器端包含文件(ssi)的处理.
--without-http_userid_module
不使用ngx_http_userid_module模块
--without-http_geo_module
这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配,该模块仅用于 0.7.63和0.8.6版本之后。但效果不太理想,对于城市的IP记录并不是特别准确,不过对于网站的来源访问区域的分析大致有一定参考性
。
--without-http_map_module
不使用ngx_http_map_module模块
--without-http_memcached_module
不使用ngx_http_memcached_module模块
--without-mail_pop3_module
不允许ngx_mail_pop3_module模块
--without-mail_imap_module
不允许ngx_mail_imap_module模块
--without-mail_smtp_module
不允许ngx_mail_smtp_module模块
4》配置说明:
配置文件位置:/usr/local/nginx/conf/nginx.conf
1. Nginx配置文件分为4个部分
2. main(全局设置)
3. server(主机设置)
4. upstream(负载均衡设置)
5. localtion(URL匹配特定位置的设置)
这四个 server继承main location继承server,upstream即不会继承,其它设置也不会被继承.
====nginx文件配置内容:
user user_00;
worker_processes 8;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 100000;
}
worker_rlimit_nofile 100000;
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
server_names_hash_bucket_size 128k;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 254k;
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
gzip on;
gzip_min_length 1k;
gzip_buffers 416k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
gzip_vary on;
charset utf-8;
access_log off;
log_not_found off;
error_page 400 403 405 408 /40x.html;
error_page 500 502 503 504 /50x.html;
server {
listen 80;
server_name www.test.com;
location / {
root html/cheche;
index index.html index.htm;
}
}
include vhost/vhost.localhost.com;
}
虚拟主机文件内容:
upstream webcount {
server 10.0.0.202:8080 weight=1 max_fails=3 fail_timeout=20s;
# server 10.0.0.203:8080 weight=1 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
server_name www.aatest.com;
charset utf-8;
location ~ (\\.jsp)|(\\.do)$ { #jsp和do的交给tomcat处理实现动静分离
index index.html index.htm index.jsp index.do;
proxy_pass http://webcount;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf|html|htm)$#html和图片的和其它的静态内容存路径html
{
root /opt/che; #可自由指定
expires 30d;
}
}~
5》启动与平滑重启
# cd /usr/local/services/nginx-0.8.55/sbin/
# ./nginx –t 检测配置文件是否有错误
# ./nginx 启动nginx
# ./nginx -s reload
6》动静分离测试
1>在tomcat上
在WEB目录创建
# cd /opt/che
# echo “this is jsp” >index.jsp
# echo “this is do” >index.do
http://10.0.0.202:8080/index.jsp
http://10.0.0.202:8080/index.do
2>在Nginx上
在Nginx的/opt/che目录下
# cd /opt/che
# echo “this is index ” >index.html
# echo “this is index a” >aa.html
3>在浏览器里面
http://www.aatest.com/
http:// www.aatest.com /index.jsp
http://www.aatest.com/index.do
==========================
可以实现控制tomcat开启与关闭的脚本:
#!/bin/bash
# Title:tomcat.sh
# Decription:Tomcat manager
# system:Use Linux
# Author:Alvin_zeng
# Version:1.0
# Date:2016-08-22
# Copyright: 2016 (c) alvin
# License: GPL
TomcatDir="/usr/local/tomcat"
#====================================
# Function-->Men_CD()
#====================================
Men_CD()
{
clear
echo
echo "========TomcatManagerTool======"
echo
tput bold
echo "1: 启动tomcat服务"
tput sgr0
echo " [*] start tomcat service"
echo
tput bold
echo "2: 关闭tomcat服务"
tput sgr0
echo " [*] stop tomcat service"
echo
tput bold
echo "3: 重启tomcat服务"
tput sgr0
echo " [*] restart tomcat service"
echo
tput bold
echo "4: 查看tomcat日志"
tput sgr0
echo " [*] check tomcat logs"
echo
tput bold
echo "5:退出"
tput sgr0
echo " [*]exit"
echo
echo "========TomcatManagerTool======="
echo
}
#====================================
# Function-->Acation_Manager
#====================================
Acation_Manager()
{
Men_CD
echo -n "Please enter you number:"
read Myenter
case $Myenter in
1)
checktomcat=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat} -eq 1 ]
then
echo
tput bold
echo " [Error]:The service is already running. Please stop the service at startup."
tput sgr0
echo
else
$TomcatDir/bin/startup.sh start
checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat1} -eq 1 ]
then
tput bold
echo "Start Tomcat.....................[OK]"
echo "Start Tomcat.....................[OK]"
echo "Start Tomcat.....................[OK]"
tput sgr0
else
tput bold
echo "Start tomcat.....................[Fail]"
echo "Start tomcat.....................[Fail]"
echo "Start tomcat.....................[Fail]"
tput sgr0
return 1
fi
fi
;;
2)
checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat1} -eq 1 ]
then
/bin/ps -ef | grep java | grep "$TomcatDir" | awk \'{print $2}\' | xargs kill -9 >>/dev/null 2>&1
if [ $? -eq 0 ]
then
echo
tput bold
echo "Tomcat Stop..............[OK]"
echo "Tomcat Stop..............[OK]"
echo "Tomcat Stop..............[OK]"
tput sgr0
echo
else
echo
tput bold
echo "Tomcat Stop...............[Fail]"
echo "Tomcat Stop...............[Fail]"
echo "Tomcat Stop...............[Fail]"
tput sgr0
echo
return 1
fi
else
echo
tput bold
echo "Tomcat The service is already close."
echo "Tomcat The service is already close."
echo "Tomcat The service is already close."
tput sgr0
echo
fi
;;
3)
ps -ef | grep java | grep "$TomcatDir" | awk \'{print $2}\' | xargs kill -9 >>/dev/null 2>&1
$TomcatDir/bin/startup.sh start
checktomcat1=`ps -ef | grep java | grep "$TomcatDir" | wc -l`
if [ ${checktomcat1} -eq 1 ]
then
echo
tput bold
echo "Restart Tomcat.....................[OK]"
echo "Restart Tomcat.....................[OK]"
echo "Restart Tomcat.....................[OK]"
tput sgr0
echo
else
tput bold
echo "Restart Tomcat.....................[Fail]"
echo "Restart Tomcat.....................[Fail]"
echo "Restart Tomcat.....................[Fail]"
tput sgr0
return 1
fi
;;
4)
tail -f $TomcatDir/logs/catalina.out
;;
5)
clear
exit 2
;;
*)
echo "Please enter |1|2|3|4|5"
;;
esac
}
#====================================
# Function-->Main
#====================================
Main()
{
Acation_Manager
if [ $? -eq 1 ]
then
exit 1
fi
}
Main;
================================
vhost.www.fanhougame.com 文件内容(原始内容,根据需要修改)
server {
listen 80 ;
server_name www.fanhougame.com fanhougame.com www.fanhouapp.com www.fanhougame.net fanhougame.net;
# server_name www.fanhougame.net fanhougame.net www.fanhouapp.net;
root /data/www/oa.com/www.fanhougame.com ;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
return 444;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
location ~ \\.php$ {
fastcgi_pass unix:/tmp/php-cgi-5313-web.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_client_abort on;
}
location ~ \\.(swf|js|css|xml|gif|jpg|jpeg|png|bmp)$ {
error_log off;
access_log off;
expires max;
}
}
以上是关于nginx+tomcat实现动静态分离的主要内容,如果未能解决你的问题,请参考以下文章