Nginx+Tomcat动静分离架构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+Tomcat动静分离架构相关的知识,希望对你有一定的参考价值。
nginx+Tomcat动静分离架构
Nginx+tomcat是目前主流的java web架构,Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat、Resin出来动态页面。
动静分离从目前实现角度来讲大致分为两种,一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过nginx来分开。
nginx服务器
1、安装环境准备
操作系统版本 | 内核版本 | 虚拟机IP地址 |
CentOS 7.0 | 3.10.0-229.el7.x86_64 | 192.168.137.128 |
2、安装依赖包
安装gcc,gcc-c++,automake
yum -y install gcc gcc-c++ automake
安装pcre,pcre-devel
为了支持rewrite的功能,需要安装pcre
yum -y install pcre pcre-devel
安装openssl,openssl-devel
支持ssl功能
yum -y install openssl openssl-devel
安装zlib, zlib-devel
支持gzip压缩功能
yum -y install zlib zlib-devel
3、源码编译安装
本次安装选择的官方稳定版本nginx-1.8.1
1、创建nginx系统用户
[[email protected] ~]# useradd-r nginx
[[email protected] ~]# id nginx
uid=997(nginx)gid=995(nginx) groups=995(nginx)
[[email protected] local]# cd /usr/local/src/
[[email protected] src]# rz
[[email protected] src]# ls
nginx-1.8.1.tar.gz
[[email protected] src]# tar -xzvf nginx-1.8.1.tar.gz
[[email protected] src]# cd nginx-1.8.1
[[email protected] nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[[email protected] nginx-1.8.1]# make && make install
安装完后用/usr/local/nginx/sbin/nginx启动服务然后访问可以看到测试页面
JDK,tomcat安装(tomcat服务器)
软件自行下载
[[email protected] src]#tar –xf jdk-8u73-linux-x64+%281%29_.gz
[[email protected] src]#mv jdk1.8.0_73/ /usr/java/
[[email protected] src]# vi/etc/profile最后加入下面一段话
exportJAVA_HOME=/usr/java/jdk1.8.0_73
exportCLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
exportPATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
[[email protected] src]# source/etc/profile 使其生效
测试Java
部署Tomcat
[[email protected] src]#tar -xzvf apache-tomcat-7.0.63.tar.gz
[[email protected] src]#cp apache-tomcat-7.0.63 /usr/local/tomcat1 -a
[[email protected] src]#cp apache-tomcat-7.0.63 /usr/local/tomcat2 –a
TOMCAT配置文件server.xml分别修改tomcat端口:
shutdown 端口:8005 主要负责启动关闭.
ajp端口:8009 主要负责通过ajp均衡(常用于apache和tomcat整合)
http端口:8080 可以通过web页面直接访问(nginx+tomcata整合)
tomcat1:默认端口
tomcat2:分别是8006 8010 8081
[[email protected]]# vi /usr/local/tomcat2/conf/server.xml
启动tomcat
[[email protected] src]# /usr/local/tomcat1/bin/startup.sh
[[email protected] src]# /usr/local/tomcat2/bin/startup.sh
ps -ef |grep tomcat可以查看服务以及启动
netstat -ntulp | grep java 可以查看服务启动的端口
客户端访问:
http://192.168.137.128:8080/
http://192.168.137.128:8081/
可以看到tomcat的测试页面
如果需要修改tomcat发布目录为自己制定的目录,需要做如下调整,创建两个发布目录:
mkdir-p /data/webapps/{www1,www2}
编辑vi/usr/local/tomcat1/conf/server.xml 在最后</Host>前一行加下内容
<Context path=""docBase="/data/webapps/www1" reloadable="false"/>
编辑vi/usr/local/tomcat2/conf/server.xml 在最后</Host>前一行加下内容
<Context path=""docBase="/data/webapps/www2" reloadable="false"/>
然后重启服务通过IP加端口访问测试,能正常访问
测试结果
Nginx+tomcat整合:
Nginx动静分离均衡配置:
nginx服务器上配置nginx.conf文件
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errorson;
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;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
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 {
#防止访问登录时候切换,我这里做测试将他注释
# ip_hash;
server 192.168.137.128:8080 weight=1max_fails=2 fail_timeout=30s;
server 192.168.137.128:8081 weight=1max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhostwww.lijq.com;
index index.jsp index.html index.htm;
#本地发布目录/data/www
root /data/www;
#所有请求从根走
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_app;
expires 3d;
}
#请求动静分离优先选择
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_app;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /data/www;
#请求浏览器缓存时间3天
expires 3d;
}
}
}
测试nginx配置文件
[[email protected] ~]# /usr/local/nginx/sbin/nginx-t
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
平滑重启
/usr/local/nginx/sbin/nginx -s reload
测试通过http://192.168.137.128/访问可以查看均衡成功,动静分离测试也成功
注意
平滑重启报错,解决方法有2种供大家参考
解决一
sudo nginx -c/usr/local/etc/nginx/nginx.conf
sudo nginx -s reload
解决二
[[email protected] ~]# vi/usr/local/nginx/conf/nginx.conf
注释保存重启就可以了
排错log路径
Nginx
[[email protected] ~]#tail -f /usr/local/nginx/logs/
access.log error.log nginx.pid
Tomcat
[[email protected] ~]#tail -f /usr/local/tomcat1/logs/
catalina.2017-06-15.log host-manager.2017-06-15.log localhost.2017-06-22.log manager.2017-06-15.log
catalina.2017-06-22.log host-manager.2017-06-22.log localhost_access_log.2017-06-15.txt manager.2017-06-22.log
catalina.out localhost.2017-06-15.log localhost_access_log.2017-06-22.txt
[[email protected] ~]#tail -f /usr/local/tomcat2/logs/
catalina.2017-06-15.log host-manager.2017-06-15.log localhost.2017-06-22.log manager.2017-06-15.log
catalina.2017-06-22.log host-manager.2017-06-22.log localhost_access_log.2017-06-15.txt manager.2017-06-22.log
catalina.out localhost.2017-06-15.log localhost_access_log.2017-06-22.txt
本文出自 “Arthur” 博客,转载请与作者联系!
以上是关于Nginx+Tomcat动静分离架构的主要内容,如果未能解决你的问题,请参考以下文章
LNMT架构部署:Linux+Nginx+Mysql+Tomcat(负载均衡,动静分离)