十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Nginx+Tomcat动静分离架构
创新互联建站是一家专注于网站制作、成都网站制作与策划设计,安化网站建设哪家好?创新互联建站做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:安化等地区。安化做网站价格咨询:028-86922220
Nginx+tomcat是目前主流的java web架构,Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat、Resin出来动态页面。
动静分离从目前实现角度来讲大致分为两种,一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过nginx来分开。
nginx服务器
操作系统版本 | 内核版本 | 虚拟机IP地址 |
CentOS 7.0 | 3.10.0-229.el7.x86_64 | 192.168.137.128 |
yum -y install gcc gcc-c++ automake
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel
yum -y install zlib zlib-devel
本次安装选择的官方稳定版本nginx-1.8.1
1、创建nginx系统用户
[root@bogon ~]# useradd-r nginx
[root@bogon ~]# id nginx
uid=997(nginx)gid=995(nginx) groups=995(nginx)
[root@bogon local]# cd /usr/local/src/
[root@bogon src]# rz
[root@bogon src]# ls
nginx-1.8.1.tar.gz
[root@bogon src]#tar -xzvf nginx-1.8.1.tar.gz
[root@bogon src]#cd nginx-1.8.1
[root@bogon nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@bogon nginx-1.8.1]# make && make install
安装完后用/usr/local/nginx/sbin/nginx启动服务然后访问可以看到测试页面
软件自行下载
[root@bogon src]#tar –xf jdk-8u73-linux-x64+%281%29_.gz
[root@bogon src]#mv jdk1.8.0_73/ /usr/java/
[root@bogon 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
[root@bogon src]# source/etc/profile使其生效
部署Tomcat
[root@bogon src]#tar -xzvf apache-tomcat-7.0.63.tar.gz
[root@bogon src]#cp apache-tomcat-7.0.63 /usr/local/tomcat1 -a
[root@bogon 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
[root@bogonsrc]# vi /usr/local/tomcat2/conf/server.xml
启动tomcat
[root@bogon src]#/usr/local/tomcat1/bin/startup.sh
[root@bogon 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在最后前一行加下内容
编辑vi/usr/local/tomcat2/conf/server.xml在最后前一行加下内容
tomcat1发布目录内容:index.jsp
1 2 3 4 5 6 |
/h2 > <%=new java.util.Date()%> < /body > < /html > |
tomcat2发布目录内容:index.jsp
1 2 3 4 5 6 |
/h2 > <%=new java.util.Date()%> < /body > < /html > |
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_errors on;
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;
}
}
}
[root@bogon ~]# /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种供大家参考
[root@bogon conf]# /usr/local/nginx/sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/nginx/nginx.pid"
解决一
sudo nginx -c/usr/local/etc/nginx/nginx.conf
sudo nginx -s reload
解决二
[root@bogon ~]# vi/usr/local/nginx/conf/nginx.conf
注释pid保存重启就可以了
[root@bogon ~]#tail -f /usr/local/nginx/logs/
access.log error.log nginx.pid
[root@bogon ~]#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
[root@bogon ~]#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