1. 程式人生 > 其它 >nginx配置禁特定路徑下的反向代理

nginx配置禁特定路徑下的反向代理

近期因為專案需要再一次配置了nginx 這次的的需求設計到字串處理 需要把<域名或ip>/xhr_api/開頭的請求轉發到內網某機器,同時移除掉xhr_api,這裡直接記錄下結果,配置非常簡單

location ~ ^/(xhr\_api/)(.*)$ {
    # return 200 "$1,$2,$3"; #$2是被括號包起來的內容
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host  $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    
    # 不需要考慮到負載的,就無需配置upstream節點。
    proxy_pass http://127.0.0.1:5678/$2;# 這裡$2前面的/是因為`^/(xhr\_api/)(.*)$`正則已經匹配了`/` 所以取不到`/`在這裡補上了
    proxy_redirect off;
}

nginx配置禁特定路徑下的反向代理
nginx配置禁止訪問目錄或禁止訪問目錄下的檔案
nginx跨域處理
帶你深入瞭解nginx基本登入認證(包含配置步驟)