關(guān)于Nginx動靜分離詳解以及配置
1.Nginx動靜分離概念
動靜分離,通過中間件將動態(tài)請求和靜態(tài)請求進行分離,分離資源,減少不必要的請求消耗,減少請求延時。
好處:動靜分離后,即使動態(tài)服務(wù)不可用,但靜態(tài)資源不會受到影響
通過中間件可以將動態(tài)請求和靜態(tài)請求進行分離

2.Nginx動靜分離應用案例

2.1.環(huán)境規(guī)劃
| 系統(tǒng) | 服務(wù) | 服務(wù) | 地址 |
| centos7.5 | 負載均衡 | Nginx proxy | 192.168.81.210 |
| centos7.5 | 靜態(tài)資源 | Nginx static | 192.168.81.220 |
| centos7.5 | 動態(tài)資源 | Tomcat server | 192.168.81.230 |
2.2.配置靜態(tài)資源
1.創(chuàng)建動靜分離配置文件
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim ds.conf
#動靜分離
server {
listen 80;
server_name ds.com;
location / {
root /web;
index index.html;
}
location ~* .*\.(png|jpg|gif)$ {
root /web/images;
}
}
2.重載Nginx
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl reload nginx
3.準備圖片
[root@localhost conf.d]# mkdir /web/images
[root@localhost conf.d]# wget -O /web/images/nginx.png http://nginx.org/nginx.png

2.3.配置動態(tài)資源
1.編譯安裝tomcat
[root@localhost soft]# tar xf apache-tomcat-7.0.92.tar.gz -C /application/
2.寫入動態(tài)文件
[root@localhost soft]# cd /application/
[root@localhost application]# vim apache-tomcat-7.0.92/webapps/ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP Test Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>Random number:</h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
3.啟動服務(wù)
[root@localhost application]# cd apache-tomcat-7.0.92/
[root@localhost apache-tomcat-7.0.92]# ./bin/startup.sh
2.4.整合動靜分離
2.4.1.配置動靜分離負載均衡
[root@localhost conf.d]# vim lb_ds.conf
#整合動靜分離
upstream static_photo {
server 172.16.1.20:80;
}
upstream java {
server 172.16.1.30:8080;
}
server {
listen 80;
server_name ds.com;
access_log /nginx_log/lb_ds_access.log main;
location / {
root /web/ds;
index index.html;
}
location ~* .*\.(jpg|png|gif)$ {
proxy_pass http://static_photo;
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* .jsp$ {
proxy_pass http://java;
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.4.2.編寫整合動靜分離代碼
[root@localhost conf.d]# vim /web/ds/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>測試動靜分離</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://ds.com/java_test.jsp",
success: function(data) {
$("#get_data").html(data)
},
error: function() {
alert("fail!!,請刷新再試");
}
});
});
</script>
<body>
<h1>測試動靜分離</h1>
<h1>上面為靜態(tài)圖片,下面為動態(tài)頁面</h1>
<img src="http://ds.com/nginx.png">
<div id="get_data"></div>
</body>
</html>
2.5.效果
看著是一個頁面實則不同機器在做處理

到此這篇關(guān)于關(guān)于Nginx動靜分離詳解以及配置的文章就介紹到這了,更多相關(guān)Nginx動靜分離詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分享nginx+php-fpm實現(xiàn)大文件下載排坑的過程
這篇文章主要介紹了nginx+php-fpm實現(xiàn)大文件下載排坑的過程,文中通過代碼實例相結(jié)合的形式給大家介紹的非常詳細,具有一定得參考借鑒價值,需要的朋友參考下吧2018-08-08
Nginx靜態(tài)文件響應POST請求 提示405錯誤的解決方法
Apache、IIS、nginx等絕大多數(shù)web服務(wù)器,都不允許靜態(tài)文件響應POST請求,否則會返回“HTTP/1.1 405 Method not allowed”錯誤2013-04-04
nginx部署前端post請求405?not?allowed問題解決
在配置前端項目的時候遇到了一個post請求405 not allowed,簡單記錄一下如何配置,這篇文章主要給大家介紹了關(guān)于nginx部署前端post請求405?not?allowed問題解決方法,需要的朋友可以參考下2023-09-09
Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用
本文主要介紹了Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用,通過limit_req和limit_conn模塊,可以有效實現(xiàn)精確的請求頻率和連接數(shù)控制,下面就來具體介紹一下2024-05-05
詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用
這篇文章主要介紹了詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用,包括ngx_http_headers_module與它的增強版ngx_headers_more的配置使用講解,需要的朋友可以參考下2016-01-01

