nginx+tomcat实现动静分离架构

最近又要准备新的项目架构,涉及到apache-tomcatcluster的相关问题,主要是以nginx+tomcat,博文为生成环境简化,重在架构思想。

151521278.png

准备安装环境,安装pcre,可以使用pcre源码或者rpm包安装

yum install -y pcre*

安装nginx

 
 
  1. tarzxvfnginx-1.4.1.tar.gz

  2. cdnginx-1.4.1/

  3. ./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module

  4. make

  5. makeinstall

nginx.conf配置

 
 
user  nobody nobody;
worker_processes 2;
error_log  /usr/local/nginx/logs/error.log  crit;
pid        /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include       mime.types;
default_type  application/octet-stream;
#charset  gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush     on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone  crawler  $binary_remote_addr  10m;
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log  /usr/local/nginx/logs/access.log  access;
server
{
listen   80;
server_name  web1.example.com;
index index.html index.htm index.php;
root  /usr/local/nginx/html;
#limit_conn   crawler  20;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
root         /usr/local/nginx/html;
expires      30d;
}
location ~ (\.jsp)|(\.do)$
{
proxy_pass http://192.168.1.2:8080;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~ .*\.(js|css)?$
{
expires      1h;
}
}
}

2、安装、配置tomcat

先安装jdk

rpm -ivh jdk-7u21-linux-i586.rpm
在/etc/profile里设置环境变量
JAVA_HOME=/usr/java/jdk1.7.0_21
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
然后在source/etc/profile

安装tomcat

tar zxvf apache-tomcat-7.0.40.tar.gz
cp -R apache-tomcat-7.0.40 /usr/local/tomcat

启动tomcat

/usr/local/tomcat/bin/startup.sh
之后打开http://localhost:8080就能看到tomcat的默认界面了
下面我们来修改tomcat的首页
我在$tomcat/webapps/下建了个html目录作为我网站的默认目录,在html中有一个index.html文件,该文件要作为我网站的默认主页。
首先,修改$tomcat/conf/server.xml文件。
在server.xml文件中,有一段如下
<enginename="Catalina"defaultHost="localhost">
<hostname="localhost"appBase="webapps"
unpackWARs="true"autoDeploy="true"
xmlValidation="false"xmlNamespaceAware="false">
<host>
</engine>

在<host></host>标签之间添加上:

<Contextpath=""docBase="html"debug="0"reloadable="true"/>
path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空;

docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个html目录,让该目录作为我的默认目录。

debug和reloadable一般都分别设置成0和true。

然后,修改$tomcat/conf/web.xml文件。
在web.xml文件中,有一段如下:

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

在<welcome-file-list>与<welcome-file>index.html</welcome-file>之间添加上:

<welcome-file>html</welcome-file>

在/usr/local/tomcat/webapps/html目录下新建一个index.jsp,一个index.do

然后在浏览器上访问静态页面index.html

144132738.png

在浏览器上访问动态页面index.jsp、index.do

144230172.png

151755629.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值