網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
需求
有多個(gè)前端服務(wù)需要通過(guò)Nginx部署。
Nginx多個(gè)前端服務(wù)配置方式
可以通過(guò)多個(gè)server配置或者多個(gè)location配置來(lái)配置多個(gè)前端服務(wù)。
多個(gè)location配置
location中root和alias的區(qū)別:location后面的路徑是真實(shí)路徑用root,虛擬路徑用alias
真實(shí)路徑就是本地訪問(wèn)地址里面有的路徑
例如vue前端
服務(wù)設(shè)置了publicPath='/allow-cost-calc'
,
前端訪問(wèn)路徑為:http://localhost:8005/allow-cost-calc/#/login,/allow-cost-calc
就是真實(shí)路徑,則使用 location /allow-cost-calc
配置時(shí)里面使用root
來(lái)指定前端服務(wù)路徑(如下服務(wù)3配置)。
若前端訪問(wèn)路徑為:http://localhost:8005/#/login,如果此時(shí)我們使用root
來(lái)配置,那么location后面的路徑只能使用真實(shí)路徑,只能使用 /
,但是多個(gè)服務(wù)配置時(shí)/
有可能已被使用(例如下面被服務(wù)1配置了),所以需要使用虛擬路徑來(lái)配置,如下服務(wù)2配置:使用/s2 來(lái)作為虛擬路徑,使用alias
來(lái)指定服務(wù)位置,部署后的訪問(wèn)方式要帶上虛擬路徑http://localhost:8005/s2/#/login
http { #嵌入其他配置文件 語(yǔ)法:include /path/file #參數(shù)既可以是絕對(duì)路徑也可以是相對(duì)路徑(相對(duì)于Nginx的配置目錄,即nginx.conf所在的目錄) include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #限制上傳文件大小 client_max_body_size 20m; server { client_max_body_size 100M; listen 1004; server_name localhost, 127.0.0.1; #服務(wù)1 location / { root dist; index index.html; } #服務(wù)2:由于/r2 是虛擬路徑,所以用alias,會(huì)為訪問(wèn)dist3下面的首頁(yè) location /r2 { alias dist3; #服務(wù)3:由于/allow-cost-calc 是真實(shí)路徑,所以用root,會(huì)訪問(wèn)/allow-cost-calc/dist2下面的首頁(yè) #(vue打包時(shí)設(shè)置了publicPath = '/allow-cost-calc',同時(shí)打包后的文件也必須放到allow-cost-calc文件夾下 dists2/allow-cost-calc/前端包文件) location /allow-cost-calc { root dist2; #后端代理,后端代理不受前端路徑的影響 location /api/ { proxy_pass http://10.51.105.7:31500/; proxy_pass_request_headers on; proxy_set_header Host $host; proxy_set_header X-Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
多個(gè)server配置
每個(gè)前端服務(wù)獨(dú)自使用一個(gè)server服務(wù)。nginx.conf部分配置如下:
http { #前端服務(wù)1 server { root dist1;#前端包位置 client_max_body_size 100M; listen 7001; server_name localhost, 127.0.0.1; location /api/ { proxy_pass http://10.51.105.7:31500/; proxy_pass_request_headers on; proxy_set_header Host $host; proxy_set_header X-Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #前端服務(wù)2 root dist2;#前端包位置 listen 7002; }
原文鏈接:https://blog.csdn.net/qq_38332722/article/details/123647654
相關(guān)推薦
- 2022-09-19 教你使用Python從文件中提取IP地址_python
- 2023-07-14 express+element實(shí)現(xiàn)上傳圖片
- 2022-06-02 python?面向?qū)ο箝_發(fā)及基本特征_python
- 2023-05-06 C語(yǔ)言順序表的基本結(jié)構(gòu)與實(shí)現(xiàn)思路詳解_C 語(yǔ)言
- 2022-08-16 一篇文章徹底搞懂Python切片操作_python
- 2023-01-13 Pytorch中的modle.train,model.eval,with?torch.no_grad
- 2022-05-31 Python必備技巧之Pandas數(shù)據(jù)合并函數(shù)_python
- 2022-08-25 python數(shù)學(xué)建模之三大模型與十大常用算法詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支