網站首頁 編程語言 正文
一. 目標:
為了通過nginx請求靜態資源(css、圖片等),通過nginx代理進行頁面預覽。
二. 實現效果:
通過瀏覽器輸入nginx代理地址以打開頁面方式訪問本地html文件,也可以通過訪問代理路由訪問接口實現頁面預覽功能.
注:我演示的是在本地windows開發環境下的配置
三. 具體配置
1. nginx配置本地靜態工程代理
找到nginx配置文件nginx.conf,配置nginx代理
server{
listen ? ? ? 80;
#前端門戶工程
location / {
?? ?alias ? D:/workspace/sc-multipl-static-web-project/;
?? ?index ?index.html;
}
說明:
D:/workspace/sc-multipl-static-web-project/ 是你的前端工程文件路徑
保存配置文件并重啟nginx,瀏覽器輸入 localhost:80 驗證
2. win10配置本地域名實現域名訪問
域名訪問實際上是通過對應ip地址,再通過ip訪問服務的,如果我們沒有開通互聯網域名,可以通過配置本地域名映射模擬域名訪問的(只在本機有效)
打開C:\Windows\System32\drivers\etc,找到hosts文件,如果沒有則自己新增一個,以管理員身份打開編輯,輸入
127.0.0.1 www.chen123.com
再打開nginx配置文件
server{
?? ?listen ? ? ? 80;
?? ?server_name ?www.chen123.com;
?? ?ssi on;
?? ?ssi_silent_errors on;
?? ?#前端門戶工程
?? ?location / {
?? ??? ?alias ? D:/workspace/sc-multipl-static-web-project/;
?? ??? ?index ?index.html;
?? ?}
?? ?
? ?}
保存配置文件并重啟nginx,瀏覽器輸入 localhost:chen123 驗證
3.nginx配置頁面預覽路由
首先,你要先實現一個頁面預覽接口,返回格式為String類型,內容其實就是html的文本內容
再打開nginx配置文件
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ? sendfile ? ? ? ?on;
? ? #tcp_nopush ? ? on;
? ? #keepalive_timeout ?0;
? ? keepalive_timeout ?65;
? ? #gzip ?on;
?? ?#cms頁面預覽路由
?? ?upstream cms_server_pool {
?? ??? ?server 127.0.0.1:31001 weight=10;
?? ?}
? ? server{
?? ?listen ? ? ? 80;
?? ?server_name ?www.xuecheng.com;
?? ?ssi on;
?? ?ssi_silent_errors on;
?? ?#前端門戶工程
?? ?location / {
?? ??? ?alias ? D:/workspace/sc-multipl-static-web-project/;
?? ??? ?index ?index.html;
?? ?}
?? ?#頁面預覽
?? ?location /cms/preview/ {
?? ??? ?proxy_pass http://cms_server_pool/cms/preview/;
?? ?}
?? ?
? ?}
}
http://cms_server_pool/cms/preview/ 就是你要實現的頁面預覽接口,通過配置路由實現跳轉到真實地址,
upstream cms_server_pool {
server 127.0.0.1:31001 weight=10;
#如果有多個服務器,可以寫在下面,例如
#server 127.0.0.1:31002 weight=10;
}
保存配置文件并重啟nginx,瀏覽器輸入 http://cms_server_pool/cms/preview 驗證
我本地的nginx配置如下
events {
? ? worker_connections ?1024;
}
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ? sendfile ? ? ? ?on;
? ? keepalive_timeout ?65;
? ? #gzip ?on;
?? ?#cms頁面預覽路由
?? ?upstream cms_server_pool {
?? ??? ?server 127.0.0.1:31001 weight=10;
?? ?}
? ? server{
?? ?listen ? ? ? 80;
?? ?server_name ?www.xuecheng.com;
?? ?ssi on;
?? ?ssi_silent_errors on;
?? ?#前端門戶工程
?? ?location / {
?? ??? ?alias ? D:/workspace/sc-multipl-static-web-project/;
?? ??? ?index ?index.html;
?? ?}
?? ?#頁面預覽
?? ?location /cms/preview/ {
?? ??? ?proxy_pass http://cms_server_pool/cms/preview/;
?? ?}
? ?}
}
原文鏈接:https://blog.csdn.net/chenhaotao/article/details/86654575
相關推薦
- 2022-05-20 springboot心跳機制,定時任務
- 2022-10-15 python中mpi4py的所有基礎使用案例詳解_python
- 2022-06-01 Android中的Coroutine協程原理解析_Android
- 2024-07-15 GIT同步修改后的遠程分支
- 2023-01-28 Flutter框架解決盒約束widget和assets里加載資產技術_Android
- 2022-11-27 C++?Array容器的顯示和隱式實例化詳細介紹_C 語言
- 2022-12-05 Python應用開發之實現串口通信_python
- 2022-05-23 C++的繼承特性你了解嗎_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同步修改后的遠程分支