網站首頁 編程語言 正文
一、修改 Windows hosts 文件
位置:C:\Windows\System32\drivers\etc
在后面追加以下內容:
# guli mall # 192.168.163.131 gulimall.com
二、Nginx 配置文件
三、分析Nginx配置文件
cat /mydata/nginx/conf/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
可以看到,在 http 塊中最后有 include /etc/nginx/conf.d/*.conf; 這句配置說明在 conf.d 目錄下所有 .conf 后綴的文件內容都會作為 nginx 配置文件 http 塊中的配置。這是為了防止主配置文件太復雜,也可以對不同的配置進行分類。
下面我們參考 conf.d 目錄下的配置,來配置 gulimall 的 server 塊配置
四、gulimall.conf
默認配置下,我們訪問 gulimall.com 會請求 nginx 默認的 index 頁面,現在我們要做的是當訪問 gulimall.com 的時候轉發到我們的商品模塊的商城首頁界面。
4.1 查看Windows ip
打開cmd 輸入 ipconfig
這里的 192.168.17.1 和 192.168.163.1 也是 Windows 的本機地址
所以我們配置當訪問 nginx /請求時代理到 192.168.163.1:10000 商品服務首頁
4.2 配置代理
server { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_pass http://192.168.163.1:10000; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
五、圖示
六、反向代理:nginx 代理網關由網關進行轉發
6.1 修改 nginx.conf
vim /mydata/nginx/conf/nginx.conf
修改 http 塊,配置上游服務器為網關地址
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream gulimall { server 192.168.163.1:88; } include /etc/nginx/conf.d/*.conf;
6.2 修改 gulimall.conf
配置代理地址為上面配置的上游服務器名
server { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_set_header Host $host; proxy_pass http://gulimall; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
?
七、訪問跳轉分析
當前通過域名的方式,請求 gulimal.com ;
根據 hosts 文件的配置,請求 gulimall.com 域名時會請求虛擬機 ip
192.168.163.131 gulimall.com
當請求到 192.168.163.131:80 時,會被 nginx 轉發到我們配置的 192.168.163.1:10000 路徑,該路徑為運行商品服務的 windows 主機 ip 地址,至此達到通過域名訪問商品服務的目的。
server { listen 80; server_name gulimall.com; location / { proxy_pass http://192.168.163.1:10000; } }
7.1 后面的跳轉分析
之后為了統一管理我們的各種服務,我們將通過配置網關作為 nginx 轉發的目標。最后通過配置網關根據不同的域名來判斷跳轉對應的服務。
原文鏈接:https://www.cnblogs.com/55zjc/p/16016030.html
相關推薦
- 2022-06-08 FreeRTOS任務控制API函數的功能分析_操作系統
- 2022-12-09 shell腳本實現Hbase服務的監控報警和自動拉起問題_linux shell
- 2022-09-04 centos搭建部署docker環境的詳細步驟_docker
- 2022-12-21 View觸發機制API實現GestureDetector?OverScroller詳解_Androi
- 2023-04-02 GoLang中的timer定時器實現原理分析_Golang
- 2022-12-03 C++時間函數整理詳解_C 語言
- 2022-06-02 Python導包模塊報錯的問題解決_python
- 2022-06-02 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同步修改后的遠程分支