網站首頁 編程語言 正文
需求
有多個前端服務需要通過Nginx部署。
Nginx多個前端服務配置方式
可以通過多個server配置或者多個location配置來配置多個前端服務。
多個location配置
location中root和alias的區別:location后面的路徑是真實路徑用root,虛擬路徑用alias
真實路徑就是本地訪問地址里面有的路徑
例如vue前端
服務設置了publicPath='/allow-cost-calc'
,
前端訪問路徑為:http://localhost:8005/allow-cost-calc/#/login,/allow-cost-calc
就是真實路徑,則使用 location /allow-cost-calc
配置時里面使用root
來指定前端服務路徑(如下服務3配置)。
若前端訪問路徑為:http://localhost:8005/#/login,如果此時我們使用root
來配置,那么location后面的路徑只能使用真實路徑,只能使用 /
,但是多個服務配置時/
有可能已被使用(例如下面被服務1配置了),所以需要使用虛擬路徑來配置,如下服務2配置:使用/s2 來作為虛擬路徑,使用alias
來指定服務位置,部署后的訪問方式要帶上虛擬路徑http://localhost:8005/s2/#/login
http { #嵌入其他配置文件 語法:include /path/file #參數既可以是絕對路徑也可以是相對路徑(相對于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; #服務1 location / { root dist; index index.html; } #服務2:由于/r2 是虛擬路徑,所以用alias,會為訪問dist3下面的首頁 location /r2 { alias dist3; #服務3:由于/allow-cost-calc 是真實路徑,所以用root,會訪問/allow-cost-calc/dist2下面的首頁 #(vue打包時設置了publicPath = '/allow-cost-calc',同時打包后的文件也必須放到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; } } }
多個server配置
每個前端服務獨自使用一個server服務。nginx.conf部分配置如下:
http { #前端服務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; } } #前端服務2 root dist2;#前端包位置 listen 7002; }
原文鏈接:https://blog.csdn.net/qq_38332722/article/details/123647654
相關推薦
- 2022-05-23 如何使Python中的print()語句運行結果不換行_python
- 2023-07-24 前端實現電子簽名(web、移動端)通用
- 2022-05-20 ElasticSearch 7.X系列之:查詢分析索引磁盤使用空間_disk_usage
- 2022-07-13 File類的基本運用、查找、刪除
- 2022-08-13 前端less入門
- 2022-01-18 ES6新語法(解構賦值、模板字符串)
- 2022-06-21 C++超詳細講解友元的使用_C 語言
- 2022-07-26 C++智能指針之shared_ptr的具體使用_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支