您好,登錄后才能下訂單哦!
Nginx+Tomcat動靜分離架構(gòu)
Nginx+tomcat是目前主流的java web架構(gòu),Nginx動靜分離簡單來說就是把動態(tài)跟靜態(tài)請求分開,不能理解成只是單純的把動態(tài)頁面和靜態(tài)頁面物理分離。嚴(yán)格意義上說應(yīng)該是動態(tài)請求跟靜態(tài)請求分開,可以理解成使用Nginx處理靜態(tài)頁面,Tomcat、Resin出來動態(tài)頁面。
動靜分離從目前實現(xiàn)角度來講大致分為兩種,一種是純粹的把靜態(tài)文件獨立成單獨的域名,放在獨立的服務(wù)器上,也是目前主流推崇的方案;另外一種方法就是動態(tài)跟靜態(tài)文件混合在一起發(fā)布,通過nginx來分開。
nginx服務(wù)器
操作系統(tǒng)版本 | 內(nèi)核版本 | 虛擬機(jī)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
本次安裝選擇的官方穩(wěn)定版本nginx-1.8.1
1、創(chuàng)建nginx系統(tǒng)用戶
[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啟動服務(wù)然后訪問可以看到測試頁面
軟件自行下載
[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 主要負(fù)責(zé)啟動關(guān)閉.
ajp端口:8009 主要負(fù)責(zé)通過ajp均衡(常用于apache和tomcat整合)
http端口:8080 可以通過web頁面直接訪問(nginx+tomcata整合)
tomcat1:默認(rèn)端口
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可以查看服務(wù)以及啟動
netstat -ntulp | grep java 可以查看服務(wù)啟動的端口
客戶端訪問:
http://192.168.137.128:8080/
http://192.168.137.128:8081/
可以看到tomcat的測試頁面
如果需要修改tomcat發(fā)布目錄為自己制定的目錄,需要做如下調(diào)整,創(chuàng)建兩個發(fā)布目錄:
mkdir-p /data/webapps/{www1,www2}
編輯vi/usr/local/tomcat1/conf/server.xml 在最后</Host>前一行加下內(nèi)容
<Context path=""docBase="/data/webapps/www1" reloadable="false"/>
編輯vi/usr/local/tomcat2/conf/server.xml 在最后</Host>前一行加下內(nèi)容
<Context path=""docBase="/data/webapps/www2" reloadable="false"/>
tomcat1發(fā)布目錄內(nèi)容:index.jsp
1 2 3 4 5 6 | <html> <body> <h2>TOMCAT_1 JSP Test Page< /h2 > <%=new java.util.Date()%> < /body > < /html > |
tomcat2發(fā)布目錄內(nèi)容:index.jsp
1 2 3 4 5 6 | <html> <body> <h2>TOMCAT_2 JSP Test Page< /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;
#本地發(fā)布目錄/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;
}
#請求動靜分離優(yōu)先選擇
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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。