Nginx反向代理如何到訪問者機器上(后端調(diào)試)
Nginx反向代理到訪問者機器上
起因
因一名后端開發(fā),不想使用postman等工具進行接口調(diào)試,因為web系統(tǒng)需要經(jīng)過N多步驟的前置動作,不能保證參數(shù)的有效性,因此,需要直接點擊web系統(tǒng),觸發(fā)本地后端代碼。
但是,總不可能動不動就讓前端給自己起一個項目吧,于是就用nginx進行部署前端項目,代理轉(zhuǎn)發(fā)的形式,將接口轉(zhuǎn)發(fā)到后端機器上。
因此有了關(guān)于這個配置的折騰。
配置
以下配置是用shell腳本生成的,需要參考腳本生成配置的,可以自己去翻下
配置內(nèi)容,就以注釋形式進行說明
# financeapi server配置 由腳本生成
server {
listen 1001;
### 因為涉及到第三方的文件服務器,當特定的接口匹配時,轉(zhuǎn)發(fā)到第三方文件服務器上
### 307的作用是:在轉(zhuǎn)發(fā)過程中不改變原是請求的任何內(nèi)容(請求方式、請求頭、請求體等)
location /fronteapi/fs/uploadFile/ {
return 307 http://10.10.10.10/fronteapi/fs/uploadFile/;
}
### 后端接口主要在這個路由里進行匹配
location /frontapi/ {
### 因為proxy_pass是自己拼接的,不是寫死的字符串,因此在進行代理轉(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)前端項目的根目錄路徑配置,相當于入口
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";
}
### 因為認證用的是3方認證服務,因此當如下路徑觸發(fā)時候,需要重寫當前路徑
location = /auth/oauth2/authorize {
rewrite ^ http://xxx/auth/oauth2/authorize;
}
}
# financeapi server配置 生成成功
Nginx反向代理實現(xiàn)指定接口訪問指定機器
location表達式類型
- location = 表示精確匹配
- location ^~ 表示uri以指定字符或字符串開頭
- location ~ 表示區(qū)分大小寫的正則匹配
- location ~* 表示不區(qū)分大小寫的正則匹配
- location / 通用匹配,任何請求都會匹配到
匹配的優(yōu)先級順序
( localtion = ) > ( localtion 完整url ) > ( localtion ^~ ) > ( localtion ,* ) > ( lcoaltion部分起始路徑 ) > ( / )
需求
nginx反向代理某服務有兩個負載,要求 某個 或者 某系列 請求 打向指定負載
- 定義upstream
#7080 7091 2負載服務
upstream gateway-all {
server 127.0.0.1:7080;
server 127.0.0.1:7081;
}
#7080服務
upstream gateway-7080 {
server 127.0.0.1:7080;
}
#7081服務
upstream gateway-7081 {
server 127.0.0.1:7081;
}要求 /api/v1/login 請求 7080機器,/api/v1/logout 請求 7081機器,其他請求走負載,/test-gateway 是反向代理服務的上下文
- 實現(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;
}- 實現(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è)務場景來做選擇
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx ServerName匹配規(guī)則實現(xiàn)
本文主要介紹了nginx ServerName匹配規(guī)則實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2007-02-02
詳解php+nginx 服務發(fā)生500 502錯誤排查思路
這篇文章主要介紹了詳解php+nginx 服務發(fā)生500 502錯誤排查思路,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07

