亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

nginx靜態資源的服務器配置方法

瀏覽:30日期:2023-03-13 15:37:21
目錄
  • 一、nginx 作用
  • 二、nginx 靜態HTTP服務器配置
  • 三、nginx HTTP服務器(動靜分離--nginx+tomcat實現動靜分離)
  • 四、 反向代理----使用proxy_pass
    • ? nginx 配置springboot+vue 前后端分離項目
      • 1、思路:nginx 結合自身特性,本身一個靜態資源的服務器,
      • 2、通過dockerCompose+nginx配置實現部署spirngboot+vue前后端分離項目

一、nginx 作用

★ 靜態HTTP服務器

★ HTTP服務器(動靜分離)

★ 反向代理

★ 負載均衡

二、nginx 靜態HTTP服務器配置

Nginx本身也是一個靜態資源的服務器,當只有靜態資源的時候,就可以使用Nginx來做服務器,同時現在也很流行動靜分離,就可以通過Nginx來實現。

# nginx 靜態資源配置--靜態服務器(也是最簡單的配置)
server {
	listen 80; # 監聽端口號
	server_name localhost; # 主機名
	index index.html index.htm; # 默認頁名稱
	root html; # 靜態資源存放目錄
	location / { # 匹配路徑
		root html; # 文件根目錄
		index index.html index.htm; # 默認頁名稱
	}
	error_page 500 502 503 504 /50x.html; # 報錯編碼對應頁面
	location = /50x.html {
		root html;
	}
}

● url 和 uri:

網址是url,url=主機:端口+uri

uri 是資源,是location后面的匹配規則,即 location uri

● location uri,當規則匹配上了就到root目錄找頁面

location / { #匹配路徑
		root html; #文件根目錄
		index index.html index.htm; #默認頁名稱
	}

● location 配置方法

location 配置可以有兩種配置方法

① 前綴 + uri(字符串/正則表達式)

② @ + name

前綴含義

= :精確匹配(必須全部相等):大小寫敏感

~* :忽略大小寫

^~ :只需匹配uri部分

@ :內部服務跳轉

三、nginx HTTP服務器(動靜分離--nginx+tomcat實現動靜分離)

靜態資源:數據不變,請求不需要后臺處理;動態資源:模板,jsp、templates等,數據需要后臺處理后渲染到網頁,動態網頁。

Nginx可以根據一定規則把不變的資源和經常變的資源區分開,對動靜資源進行拆分,實現對靜態資源的做緩存,從而提高資源響應的速度。這就是網站靜態化處理的核心思路。

upstream tomcat{  
	server localhost:8080;  
}   
server {  
	listen       80;  
	server_name  localhost;  
	location / {  
		root   html;  
		index  index.html;  
	}  
	# 所有靜態請求都由nginx處理,存放目錄為html  
	location  ~* \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {  
		root   html;  
	}  
	# 所有動態請求都轉發給tomcat處理  
	location ~ *jsp$ {  
		proxy_pass  http://tomcat; # 代理轉發
	}  
	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 結合自身特性,本身一個靜態資源的服務器

(1) 通過nginx實現域名的方式訪問網站,以及把對數據的請求通過nginx反向代理轉發給后端容器(后端服務),避免了接口暴露的不安全

① 訪問網站,首先習慣上訪問網站的首頁,通常訪問路徑是/ [location /],然后默認頁面是首頁;

location / {
     root /shan/blog/;
     index index.html;
 }

② 默認頁面,首頁需要像css、js、圖片等靜態資源,才能顯示出樣式、動態效果等,需要通過匹配規則[location ~*.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$],指定root 到那個目錄下獲取這些靜態資源。

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 *;
 }

③ 默認首頁,需要有數據

在vue中首頁實際上編寫了很多個接口在請求數據,這些動態數據是來自 springboot項目(api 服務),需要咱通過定義一個匹配接口路徑的規則[location /api],然后進行請求轉發到 springboot項目(api 服務)

2、通過dockerCompose+nginx配置實現部署spirngboot+vue前后端分離項目

(1) dockerCompose 主要內容:

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編排,服務名相當于域名
}

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 會代理轉發到 api:8888
					  proxy_pass http://apistream;
      }  
}

到此這篇關于nginx靜態資源的服務器配置方法的文章就介紹到這了,更多相關nginx靜態資源服務器內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: Nginx
主站蜘蛛池模板: 日韩高清特级特黄毛片 | 黄色变态网站 | 欧美成人免费大片888 | 国产成人18黄网站麻豆 | 青青自拍| 国产精品推荐 | 欧美一级毛片免费看 | 国产一级性生活片 | 亚洲综合亚洲综合网成人 | 产国语一级特黄aa大片 | 黄网站免费观看 | 国产码欧美日韩高清综合一区 | 久久久久久免费视频 | 久爱www免费人成福利播放 | 你懂的最新网址 | 日本免费va毛片在线看大 | 麻豆网站 | 亚洲操片 | 香港三级理论在线影院 | 欧美成人免费xxx大片 | 久久精品亚洲热综合一本奇米 | 国产a级网站 | 日韩亚洲精品不卡在线 | 蝌蚪蚪窝视频在线视频手机 | 国产亚洲欧美一区二区 | 久久久久999 | 国产大学生自拍视频 | 国产免费专区 | 91亚洲精品一区二区福利 | 亚洲无吗在线视频 | 99ri国产在线观看 | 日本高清免费不卡视频 | 黄色片网站在线免费观看 | 亚洲自拍偷拍视频 | 香蕉草草久在视频在线播放 | 国内精品一区视频在线播放 | 亚洲欧美综合国产精品一区 | 欧美三级免费看 | 青青青国产视频 | 欧美成人看片黄a免费看 | 欧美一级aa天码毛片 |