Nginx反向代理如何到訪問者機(jī)器上(后端調(diào)試)
Nginx反向代理到訪問者機(jī)器上
起因
因一名后端開發(fā),不想使用postman等工具進(jìn)行接口調(diào)試,因?yàn)閣eb系統(tǒng)需要經(jīng)過N多步驟的前置動作,不能保證參數(shù)的有效性,因此,需要直接點(diǎn)擊web系統(tǒng),觸發(fā)本地后端代碼。
但是,總不可能動不動就讓前端給自己起一個項(xiàng)目吧,于是就用nginx進(jìn)行部署前端項(xiàng)目,代理轉(zhuǎn)發(fā)的形式,將接口轉(zhuǎn)發(fā)到后端機(jī)器上。
因此有了關(guān)于這個配置的折騰。
配置
以下配置是用shell腳本生成的,需要參考腳本生成配置的,可以自己去翻下
配置內(nèi)容,就以注釋形式進(jìn)行說明
# financeapi server配置 由腳本生成
server {
listen 1001;
### 因?yàn)樯婕暗降谌降奈募?wù)器,當(dāng)特定的接口匹配時,轉(zhuǎn)發(fā)到第三方文件服務(wù)器上
### 307的作用是:在轉(zhuǎn)發(fā)過程中不改變原是請求的任何內(nèi)容(請求方式、請求頭、請求體等)
location /fronteapi/fs/uploadFile/ {
return 307 http://10.10.10.10/fronteapi/fs/uploadFile/;
}
### 后端接口主要在這個路由里進(jìn)行匹配
location /frontapi/ {
### 因?yàn)閜roxy_pass是自己拼接的,不是寫死的字符串,因此在進(jìn)行代理轉(zhuǎn)發(fā)時,
### 會丟失請求參數(shù),需要提前保存,最后拼在proxy_pass上
if ($request_uri ~* "financeapi(/.*$)") {
set $path_remainder $1;
}
### 代理轉(zhuǎn)發(fā)的主要配置,$remote_addr為nginx內(nèi)置的變量,可表示訪問者的IP,本文主題就是這個變量體現(xiàn)的
proxy_pass http://$remote_addr:8079/$path_remainder;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cookie_path / "/; HTTPOnly; SameSite=Lax; Max-Age=86400";
}
### 靜態(tài)前端項(xiàng)目的根目錄路徑配置,相當(dāng)于入口
location / {
root /home/docker/frontApp/front-web/dist;
index index.html;
try_files $uri $uri/ /index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cookie_path / "/; HTTPOnly; SameSite=Lax; Max-Age=86400";
}
### 因?yàn)檎J(rèn)證用的是3方認(rèn)證服務(wù),因此當(dāng)如下路徑觸發(fā)時候,需要重寫當(dāng)前路徑
location = /auth/oauth2/authorize {
rewrite ^ http://xxx/auth/oauth2/authorize;
}
}
# financeapi server配置 生成成功
Nginx反向代理實(shí)現(xiàn)指定接口訪問指定機(jī)器
location表達(dá)式類型
- location = 表示精確匹配
- location ^~ 表示uri以指定字符或字符串開頭
- location ~ 表示區(qū)分大小寫的正則匹配
- location ~* 表示不區(qū)分大小寫的正則匹配
- location / 通用匹配,任何請求都會匹配到
匹配的優(yōu)先級順序
( localtion = ) > ( localtion 完整url ) > ( localtion ^~ ) > ( localtion ,* ) > ( lcoaltion部分起始路徑 ) > ( / )
需求
nginx反向代理某服務(wù)有兩個負(fù)載,要求 某個 或者 某系列 請求 打向指定負(fù)載
- 定義upstream
#7080 7091 2負(fù)載服務(wù)
upstream gateway-all {
server 127.0.0.1:7080;
server 127.0.0.1:7081;
}
#7080服務(wù)
upstream gateway-7080 {
server 127.0.0.1:7080;
}
#7081服務(wù)
upstream gateway-7081 {
server 127.0.0.1:7081;
}要求 /api/v1/login 請求 7080機(jī)器,/api/v1/logout 請求 7081機(jī)器,其他請求走負(fù)載,/test-gateway 是反向代理服務(wù)的上下文
- 實(shí)現(xiàn)一:定義不同的localtion,路由不同的uri
location /test-gateway/api/v1/login {
#指向上面的upstream
proxy_pass http://gateway-7080;
}
location /test-gateway/api/v1/logout {
#指向上面的upstream
proxy_pass http://gateway-7081;
}
location /test-gateway {
#指向上面的upstream
proxy_pass http://gateway-all;
}- 實(shí)現(xiàn)二:一個localtion,路由不同的uri
location /test-gateway {
if ($request_uri ~* "/api/v1/login"){
proxy_pass http://gateway-7080;
}
if ($request_uri ~* "/api/v1/logout"){
proxy_pass http://gateway-7081;
}
proxy_pass http://gateway-all;
}location 靈活多變,具體哪種適合自己,根據(jù)自己的業(yè)務(wù)場景來做選擇
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx could not build the server_names_hash 錯誤的解決辦法
這篇文章主要介紹了Nginx could not build the server_names_hash 錯誤的解決辦法,需要的朋友可以參考下2014-03-03
nginx部署前端項(xiàng)目location時root和alias配置指南
nginx指定文件路徑有兩種方式root和alias,下面這篇文章主要給大家介紹了關(guān)于nginx部署前端項(xiàng)目location時root和alias配置的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Nginx域名轉(zhuǎn)發(fā)使用場景代碼實(shí)例
這篇文章主要介紹了Nginx域名轉(zhuǎn)發(fā)使用場景代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Nginx geoip模塊實(shí)現(xiàn)地區(qū)性負(fù)載均衡
相信做過awstats的都用過開源的geoip.dat ip數(shù)據(jù)庫,剛好nginx wiki上有g(shù)eoip 模塊,這樣就可以實(shí)現(xiàn)地區(qū)性的負(fù)載均衡,但是maxmind 的ip數(shù)據(jù)庫對中國的支持不算太好,不過現(xiàn)在也不錯了~2010-12-12
詳解nginx rewrite和根據(jù)url參數(shù)location
本篇文章主要是介紹了nginx rewrite和根據(jù)url參數(shù)location,有興趣的同學(xué)可以了解以下。2016-11-11

