文章詳情頁(yè)
關(guān)于Nginx動(dòng)靜分離詳解以及配置
瀏覽:141日期:2023-06-15 15:23:24
1.Nginx動(dòng)靜分離概念
動(dòng)靜分離,通過(guò)中間件將動(dòng)態(tài)請(qǐng)求和靜態(tài)請(qǐng)求進(jìn)行分離,分離資源,減少不必要的請(qǐng)求消耗,減少請(qǐng)求延時(shí)。
好處:動(dòng)靜分離后,即使動(dòng)態(tài)服務(wù)不可用,但靜態(tài)資源不會(huì)受到影響
通過(guò)中間件可以將動(dòng)態(tài)請(qǐng)求和靜態(tài)請(qǐng)求進(jìn)行分離
2.Nginx動(dòng)靜分離應(yīng)用案例
2.1.環(huán)境規(guī)劃
系統(tǒng)服務(wù)服務(wù)地址centos7.5負(fù)載均衡Nginx proxy192.168.81.210centos7.5靜態(tài)資源Nginx static192.168.81.220centos7.5動(dòng)態(tài)資源Tomcat server192.168.81.2302.2.配置靜態(tài)資源
1.創(chuàng)建動(dòng)靜分離配置文件[root@localhost ~]# cd /etc/nginx/conf.d/[root@localhost conf.d]# vim ds.conf#動(dòng)靜分離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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@localhost conf.d]# systemctl reload nginx3.準(zhǔn)備圖片[root@localhost conf.d]# mkdir /web/images[root@localhost conf.d]# wget -O /web/images/nginx.png http://nginx.org/nginx.png
2.3.配置動(dòng)態(tài)資源
1.編譯安裝tomcat[root@localhost soft]# tar xf apache-tomcat-7.0.92.tar.gz -C /application/2.寫(xiě)入動(dòng)態(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.啟動(dòng)服務(wù)[root@localhost application]# cd apache-tomcat-7.0.92/[root@localhost apache-tomcat-7.0.92]# ./bin/startup.sh
2.4.整合動(dòng)靜分離
2.4.1.配置動(dòng)靜分離負(fù)載均衡
[root@localhost conf.d]# vim lb_ds.conf#整合動(dòng)靜分離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.編寫(xiě)整合動(dòng)靜分離代碼
[root@localhost conf.d]# vim /web/ds/index.html<html lang="en"><head><meta charset="UTF-8" /><title>測(cè)試動(dòng)靜分離</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!!,請(qǐng)刷新再試");}});});</script><body><h1>測(cè)試動(dòng)靜分離</h1><h1>上面為靜態(tài)圖片,下面為動(dòng)態(tài)頁(yè)面</h1><img src="http://ds.com/nginx.png"><div id="get_data"></div></body></html>
2.5.效果
看著是一個(gè)頁(yè)面實(shí)則不同機(jī)器在做處理
到此這篇關(guān)于關(guān)于Nginx動(dòng)靜分離詳解以及配置的文章就介紹到這了,更多相關(guān)Nginx動(dòng)靜分離詳解內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
標(biāo)簽:
Nginx
相關(guān)文章:
1. nginx?反向代理負(fù)載均衡策略配置SSL訪問(wèn)匹配規(guī)則優(yōu)先級(jí)2. nginx Rewrite重寫(xiě)地址的實(shí)現(xiàn)3. 教你nginx跳轉(zhuǎn)配置的四種方式4. 阿里云ssl證書(shū)如何通過(guò)Nginx部署到服務(wù)器5. shell腳本實(shí)戰(zhàn)之部署nginx腳本實(shí)例6. Nginx如何安裝配置Lua支持7. Nginx使用ngx_http_upstream_module實(shí)現(xiàn)負(fù)載均衡功能示例8. nginx七層負(fù)載均衡配置詳解9. SpringBoot前端后端分離之Nginx服務(wù)器下載安裝過(guò)程10. nginx部署前端項(xiàng)目的超級(jí)詳細(xì)步驟記錄
排行榜
