linux系統nginx動靜分離
阿新 • • 發佈:2020-07-23
動靜分離
注意:動靜分離,不需要運維來做(開發)
動態請求:該使用者請求,需要呼叫資料庫中的資料
靜態請求:該使用者請求,不需要呼叫資料庫
動態頁面:後端開發寫的需要呼叫資料庫的頁面(python,Java,C,PHP,nodejs...)
靜態頁面:前端開發寫的,不需要呼叫資料庫,直接訪問的頁面
部署nginx:專門存放前端寫的靜態頁
部署tomcat:專門存放後端寫的JAVA動態頁面(resin)
部署nginx:專門存放前端寫的靜態頁
部署php:專門存放後端寫的PHP動態頁面
動靜分離演示
環境準備
系統 | 作用 | 服務 | 地址 | 伺服器名稱 |
---|---|---|---|---|
Centos7.6 | 負載均衡 | nginx proxy | 10.0.0.5 | lb01 |
Centos7.6 | 靜態資源 | nginx static | 10.0.0.7 | web01 |
Centos7.6 | 動態資源 | tomcat server | 10.0.0.8 | web02 |
web01靜態資源配置
# 下載nginx [root@wb01 ~]# yum install -y nginx # 修改nginx配置檔案 [root@wb01~]# vim /etc/nginx/conf.d/static.conf server { listen 80; server_name static.com; root /code; index index.html; location ~* \.(jpg|gif|jpeg|png|mp4){ root /code/images; } } # 建立站點目錄 [root@wb01 ~]# mkdir /code/images/ -p # 進入站點目錄上傳圖片 [root@wb01 ~]# cd /code/images/ [root@wb01 images]# rz [root@wb01 images]# ^C [root@wb01 images]# rz [root@wb01 images]# ll total 24 -rw-r--r-- 1 root root 23313 Jun 1 18:34 a.jpg.jpg # 檢查nginx語法 [root@wb01 images]# nginx -t # 啟動並加入開機自啟 [root@wb01 images]# systemctl start nginx && systemctl enable nginx # 檢測埠 檢測程序 [root@wb01 images]# netstat -lntup|grep nginx && ps -ef |grep [n]ginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6739/nginx: master tcp6 0 0 :::80 :::* LISTEN 6739/nginx: master root 6739 1 0 Jun01 ? 00:00:00 nginx: master process /usr/sbin/nginx nginx 6740 6739 0 Jun01 ? 00:00:00 nginx: worker process # 域名解析 # 瀏覽器訪問
web02動態資源配置
#1.安裝tomcat [root@web02 ~]# yum install -y tomcat # 2.進入站點目錄 [root@web02 ~]# cd /usr/share/tomcat/webapps # 3.建立ROOT目錄 [root@web02 webapps]# mkdir ROOT # 4.編輯java程式碼檔案 [root@web02 ROOT]# cat java.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <HTML> <HEAD> <TITLE>王張行JSP Page</TITLE> </HEAD> <BODY> <% Random rand = new Random(); out.println("<h1>王張行隨機數:<h1>"); out.println(rand.nextInt(99)+100); %> </BODY> </HTML> # 啟動tomcat [root@web02 ROOT]# systemctl start tomcat # 檢視埠 [root@web02 ROOT]# netstat -lntup| > grep java tcp6 0 0 :::8009 :::* LISTEN 8666/java tcp6 0 0 :::8080 :::* LISTEN 8666/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8666/java # 瀏覽器訪問
lb02代理服務配置
# 下載nginx
[root@lb01 ~]# yum install -y nginx
# 編輯配置檔案
[root@lb01 ~]# !v
vim /etc/nginx/conf.d/proxy.conf
server {
listen 80;
server_name static.com;
location / {
root /code;
index index.html;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host;
}
location ~ \.jsp {
proxy_pass http://10.0.0.8:8080;
proxy_set_header Host $http_host;
}
}
# 建立站點目錄
[root@lb01 ~]# mkdir /code
# 編輯站點目錄下AJAX程式碼頁面
[root@lb01 code]# vim index.html
[root@lb01 code]# cat index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>王張行測試ajax和跨域訪問</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://static.com/java.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失敗了,回去檢查你服務去~");
}
});
});
</script>
<body>
<h1>王張行帶你測試動靜分離</h1>
<img src="http://static.com/2.jpg">
<div id="get_data"></div>
</body>
</html>
# 檢測語法
[root@lb01 ~]# nginx -t
# 啟動並加入開機自啟
[root@lb01 ~]# systemctl start nginx && systemctl enable nginx
# 域名解析
# 瀏覽器訪問