生活随笔
收集整理的這篇文章主要介紹了
Nginx动静分离配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Nginx動靜分離
一、動靜分離介紹
動靜分離,通過中間件將動態(tài)請求和靜態(tài)請求進(jìn)行分離
通過中間件將動態(tài)請求和靜態(tài)請求分離,可以減少不必要的請求消耗,同時能減少請求的延時。
通過中間件將動態(tài)請求和靜態(tài)請求分離,邏輯圖如下:
1.單臺機(jī)器動靜分離
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-NaockIvC-1623251222252)(img/image-20201204141634031.png)]
[root@web01 ~
]
server
{listen
80;server_name linux.wp.com
;location /
{root /code/wordpress
;index index.php
;}location ~*
\.
(jpg
|png
|gif
)$
{root /code/wordpress
;}location ~*
\.php$
{fastcgi_pass
127.0.0.1:9000
;fastcgi_param SCRIPT_FILENAME /code/wordpress/
$fastcgi_script_name;include fastcgi_params
;}
}
2.多臺機(jī)器的動靜分離
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-8q8hbq8E-1623251222260)(img/image-20201207133847547.png)]
主機(jī)作用服務(wù)IP
| lb01 | 負(fù)載均衡 | nginx | 10.0.0.4 |
| web01 | 靜態(tài)資源 | nginx | 10.0.0.7 |
| web02 | 動態(tài)資源 | tomcat | 10.0.0.8 |
[root@web01 ~
]
server
{listen
80;server_name linux.dj.com
;location ~*
\.
(jpg
|png
|mp4
|gif
)$
{root /code/picture
;}
}
[root@web01 ~
]
[root@web01 ~
]
[root@web01 ~
]
[root@web01 picture
]
[root@web01 picture
]
total
1756
-rw-r--r--.
1 root root
156617 Dec
7 08:54
1.jpg
-rw-r--r--.
1 root root
47542 Dec
7 08:54
2.jpg
-rw-r--r--.
1 root root
1586108 Dec
7 08:54
3.jpg配置hosts
10.0.0.7 linux.dj.com請求靜態(tài)資源http://linux.dj.com/1.jpg
[root@web02 ~
]
[root@web02 ~
]
[root@web02 webapps
]
[root@web02 webapps
]
<%@ page
language="java" import="java.util.*" pageEncoding="utf-8"%
>
<HTML
><HEAD
><TITLE
>測試動態(tài)資源
</TITLE
></HEAD
><BODY
><%Random rand
= new Random
();out.println
("<h1>隨機(jī)數(shù):<h1>");out.println
(rand.nextInt
(99)+100
);%
></BODY
>
</HTML
>
[root@web02 ~
]配置hosts
10.0.0.8 linux.dj.com訪問http://linux.dj.com:8080/java_test.jsp
[root@lb01 ~
]
upstream dt
{server
10.0.0.8:8080
;
}upstream jt
{server
10.0.0.7
;
}server
{listen
80;server_name linux.dj.com
;location /
{root /code/dj
;index index.html
;}location ~*
\.
(jpg
|png
|gif
)$
{proxy_pass http://jt
;include proxy_params
;}location ~*
\.
(php
|jsp
)$
{proxy_pass http://dt
;include proxy_params
;}
}
[root@lb01 ~
]1)配置hosts
10.0.0.4 linux.dj.com
2)訪問http://linux.dj.com/java_test.jsphttp://linux.dj.com/1.jpg
[root@lb01 ~
]
[root@lb01 ~
]
<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://linux.dj.com/java_test.jsp",success: function
(data
){$("#get_data").html
(data
)},error:
function() {alert
("哎呦喂,失敗了,回去檢查你服務(wù)去~");}});
});
</script
><body
><h
1>測試動靜分離
</h
1><img
src="http://linux.dj.com/1.jpg"><div
id="get_data"></div
></body
>
</html
>
[root@lb01 ~
]
結(jié)論:靜態(tài)資源出現(xiàn)問題不影響動態(tài)資源,動態(tài)資源出問題不影響靜態(tài)資源
二、nginx資源分離
主機(jī)IP功能
| lb01 | 10.0.0.4,172.16.1.4 | 負(fù)載均衡 |
| web01 | 172.16.1.7 | Android頁面 |
| web02 | 172.16.1.8 | iPhone頁面 |
| web03 | 172.16.1.9 | PC端頁面 |
[root@web01 ~
]
server
{listen
80;server_name linux.sj.com
;charset utf8
;location /
{root /code/android
;index index.html
;}
}
[root@web01 ~
]
[root@web01 ~
]
[root@web01 ~
]
[root@web01 ~
]
配置hosts
10.0.0.7 linux.sj.com
[root@web02 ~
]
server
{listen
80;server_name linux.sj.com
;charset utf8
;location /
{root /code/iphone
;index index.html
;}
}
[root@web02 ~
]
[root@web02 ~
]
[root@web02 ~
]
[root@web02 ~
]
配置hosts
10.0.0.8 linux.sj.com
[root@web03 ~
]
server
{listen
80;server_name linux.sj.com
;charset utf8
;location /
{root /code/pc
;index index.html
;}
[root@web03 ~
]
[root@web03 ~
]
[root@web03 ~
]
[root@web02 ~
]
配置hosts
10.0.0.9 linux.sj.com
[root@lb01 ~
]
upstream android
{server
10.0.0.7
;
}upstream iphone
{server
10.0.0.8
;
}upstream pc
{server
10.0.0.9
;
}server
{listen
80;server_name linux.sj.com
;location /
{if ($http_user_agent ~*
"Android") { proxy_pass http://android
; }if ($http_user_agent ~*
"iPhone") { proxy_pass http://iphone
; }if ($http_user_agent ~*
"WOW64") { return 403; }proxy_pass http://pc
; include proxy_params
;}
}
[root@lb01 ~
]
10.0.0.4 linux.sj.com
http://linux.sj.com/
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-ayyUHwF9-1623251222266)(img/image-20201207133935994.png)]
總結(jié)
以上是生活随笔為你收集整理的Nginx动静分离配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。