nginx靜態(tài)資源的服務(wù)器配置方法
一、nginx 作用
★ 靜態(tài)HTTP服務(wù)器
★ HTTP服務(wù)器(動靜分離)
★ 反向代理
★ 負載均衡
二、nginx 靜態(tài)HTTP服務(wù)器配置
Nginx本身也是一個靜態(tài)資源的服務(wù)器
,當(dāng)只有靜態(tài)資源的時候,就可以使用Nginx來做服務(wù)器,同時現(xiàn)在也很流行動靜分離,就可以通過Nginx來實現(xiàn)。
# nginx 靜態(tài)資源配置--靜態(tài)服務(wù)器(也是最簡單的配置) server { listen 80; # 監(jiān)聽端口號 server_name localhost; # 主機名 index index.html index.htm; # 默認頁名稱 root html; # 靜態(tài)資源存放目錄 location / { # 匹配路徑 root html; # 文件根目錄 index index.html index.htm; # 默認頁名稱 } error_page 500 502 503 504 /50x.html; # 報錯編碼對應(yīng)頁面 location = /50x.html { root html; } }
● url 和 uri:
網(wǎng)址是url,url=主機:端口+uri
uri 是資源,是location后面的匹配規(guī)則,即 location uri
● location uri,當(dāng)規(guī)則匹配上了就到root目錄找頁面
location / { #匹配路徑 root html; #文件根目錄 index index.html index.htm; #默認頁名稱 }
● location 配置方法
location 配置可以有兩種配置方法
① 前綴 + uri(字符串/正則表達式)
② @ + name
前綴含義
= :精確匹配(必須全部相等):大小寫敏感
~* :忽略大小寫
^~ :只需匹配uri部分
@ :內(nèi)部服務(wù)跳轉(zhuǎn)
三、nginx HTTP服務(wù)器(動靜分離--nginx+tomcat實現(xiàn)動靜分離)
靜態(tài)資源:數(shù)據(jù)不變,請求不需要后臺處理;動態(tài)資源:模板,jsp、templates等,數(shù)據(jù)需要后臺處理后渲染到網(wǎng)頁,動態(tài)網(wǎng)頁。
Nginx可以根據(jù)一定規(guī)則把不變的資源和經(jīng)常變的資源區(qū)分開,對動靜資源進行拆分,實現(xiàn)對靜態(tài)資源的做緩存,從而提高資源響應(yīng)的速度。這就是網(wǎng)站靜態(tài)化處理的核心思路。
upstream tomcat{ server localhost:8080; } server { listen 80; server_name localhost; location / { root html; index index.html; } # 所有靜態(tài)請求都由nginx處理,存放目錄為html location ~* \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root html; } # 所有動態(tài)請求都轉(zhuǎn)發(fā)給tomcat處理 location ~ *jsp$ { proxy_pass http://tomcat; # 代理轉(zhuǎn)發(fā) } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
四、 反向代理----使用proxy_pass
server { listen 80; server_name blog.yilele.site; index index.html; location / { root /shan/blog/; index index.html; } location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ { root /shan/blog/; index index.html; add_header Access-Control-Allow-Origin *; } # 反向代理 location /api { proxy_pass http://ip地址或域名:端口號; } }
? nginx 配置springboot+vue 前后端分離項目
1、思路:nginx 結(jié)合自身特性,本身一個靜態(tài)資源的服務(wù)器
,
(1) 通過nginx實現(xiàn)域名的方式訪問網(wǎng)站,以及把對數(shù)據(jù)的請求通過nginx反向代理轉(zhuǎn)發(fā)給后端容器(后端服務(wù)),避免了接口暴露的不安全
① 訪問網(wǎng)站,首先習(xí)慣上訪問網(wǎng)站的首頁,通常訪問路徑是/ [location /],然后默認頁面是首頁;
location / { root /shan/blog/; index index.html; }
② 默認頁面,首頁需要像css、js、圖片等靜態(tài)資源,才能顯示出樣式、動態(tài)效果等,需要通過匹配規(guī)則[location ~*.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$],指定root 到那個目錄下獲取這些靜態(tài)資源。
location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ { root /shan/blog/; index index.html; add_header Access-Control-Allow-Origin *; }
③ 默認首頁,需要有數(shù)據(jù)
在vue中首頁實際上編寫了很多個接口在請求數(shù)據(jù),這些動態(tài)數(shù)據(jù)是來自 springboot項目(api 服務(wù)),需要咱通過定義一個匹配接口路徑的規(guī)則[location /api],然后進行請求轉(zhuǎn)發(fā)到 springboot項目(api 服務(wù))
2、通過dockerCompose+nginx配置實現(xiàn)部署spirngboot+vue前后端分離項目
(1) dockerCompose 主要內(nèi)容:
version: "3" services: api: image: api container_name: api expose: - "8888" nginx: image: nginx container_name: nginx ports: - 80:80 - 443:443 volumes: - /mnt/docker/nginx/:/etc/nginx/ - /mnt/shan/blog:/shan/blog links: - api depends_on: - api
(2) nginx 主要配置:
upstream apistream{ server api:8888;# 通過dockerCompose編排,服務(wù)名相當(dāng)于域名 } server { listen 80; server_name blog.yilele.site; index index.html; location / { root /shan/blog/; index index.html; } location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ { root /shan/blog/; index index.html; add_header Access-Control-Allow-Origin *; } location /api {# 請求https://blog.yilele.site/api 會代理轉(zhuǎn)發(fā)到 api:8888 proxy_pass http://apistream; } }
到此這篇關(guān)于nginx靜態(tài)資源的服務(wù)器配置方法的文章就介紹到這了,更多相關(guān)nginx靜態(tài)資源服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?403?forbidden錯誤的原因以及解決方法
yum安裝nginx,安裝一切正常,但是訪問時報403 forbidden,下面這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯誤的原因以及解決方法,需要的朋友可以參考下2022-08-08利用nginx實現(xiàn)動靜分離的負載均衡集群實戰(zhàn)教程
這篇文章介紹了利用nginx實現(xiàn)動靜分離的負載均衡集群實戰(zhàn),本次用到的操作系統(tǒng)及服務(wù),本次實驗一共需要3臺服務(wù)器,一臺nginx做為負載均衡分發(fā)器和動靜分離的分發(fā)器,兩臺apache做為后端服務(wù)器,使用nginx實現(xiàn)兩臺apache服務(wù)器的負載均衡和動靜分離,需要的朋友可以參考下2023-03-03詳解nginx rewrite和根據(jù)url參數(shù)location
本篇文章主要是介紹了nginx rewrite和根據(jù)url參數(shù)location,有興趣的同學(xué)可以了解以下。2016-11-11解決nginx配置proxy_pass之后,響應(yīng)變慢的問題
這篇文章主要介紹了解決nginx配置proxy_pass之后,響應(yīng)變慢的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01