手机版
你好,游客 登录 注册
背景:
阅读新闻

Nginx+Tomcat实现动静分离架构实现

[日期:2013-06-10] 来源:Linux社区  作者:老徐_kevin [字体: ]

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

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

1
yum install -y pcre*

安装nginx

  1. tar zxvf nginx-1.4.1.tar.gz

  2. cd nginx-1.4.1/

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

  4. make

  5. make install

nginx.conf配置

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_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

  1. rpm -ivh jdk-7u21-linux-i586.rpm

在/etc/profile里设置环境变量
  1. JAVA_HOME=/usr/java/jdk1.7.0_21

  2. CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib

  3. PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin

  4. export PATH CLASSPATH JAVA_HOME

然后在source /root/.bash_profile

安装tomcat

  1. tar zxvf apache-tomcat-7.0.40.tar.gz

  2. cp -R apache-tomcat-7.0.40 /usr/local/tomcat

启动tomcat

  1. /usr/local/tomcat/bin/startup.sh

之后打开http://localhost:8080就能看到tomcat的默认界面了
下面我们来修改tomcat的首页
我在$tomcat/webapps/下建了个html目录作为我网站的默认目录,在html中有一个index.html文件,该文件要作为我网站的���认主页。
首先,修改$tomcat/conf/server.xml文件。
在server.xml文件中,有一段如下:
  1. ……

  2. <enginename="Catalina"defaultHost="localhost">

  3. <hostname="localhost"appBase="webapps"

  4. unpackWARs="true"autoDeploy="true"

  5. xmlValidation="false"xmlNamespaceAware="false">

  6. ……

  7. <host>

  8. </engine>

  9. ……

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

  1. <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文件中,有一段如下:

  1. <welcome-file-list>

  2. <welcome-file>index.html</welcome-file>

  3. <welcome-file>index.htm</welcome-file>

  4. <welcome-file>index.jsp</welcome-file>

  5. </welcome-file-list>

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

  1. <welcome-file>html</welcome-file>

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

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

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

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款