1. 程式人生 > >Nginx 根據URL帶的引數轉發

Nginx 根據URL帶的引數轉發

使用場景:

    需要根據擷取URL動態配置跳轉路徑,常見於訪問內網不固定ip地址的檔案圖片,

請求地址:http://11.19.1.212:82/bimg4/32.52.62.42:222/DownLoadFile?filename=LOC:12/data/20180208/15/2e0ae54dfd752210083404deed15269c_222403
實際需要訪問的內網地址:http://32.52.62.42:222/DownLoadFile?filename=LOC:12/data/20180208/15/2e0ae54dfd752210083404deed15269c_222403

nginx配置檔案

server {
    listen 83;
    server_name localhost;
    index index.html index.htm;

    root D: /workspace-xxxx/xxx_Web;

    error_page 500 502 503 504 / 50x.html;
    location = /50x.html {
            root   html;
        }
    location ^~ /xxx/ {
        proxy_pass http: //192.168.60.36:8090/xxxx/;
        proxy_redirect default;
        proxy_set_header Host $host;
        proxy_set_header X - Real - IP $remote_addr;
        proxy_set_header X - Forwarded - Host $host;
        proxy_set_header X - Forwarded - Server $host;
        proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

# 此處為擷取請求地址中bimg4之後的路徑,並賦給轉發地址
    location ^ ~ / bimg4 / {
        if ($request_uri~ / bimg4 / (. * )) {
            set $bucketid $1;
        }
        proxy_pass http: //$bucketid;
    }

}