
我是 Nginx 新手。我有 nginx 和一個 python 網路伺服器,監聽連接埠:5000。
我想做類似 www.example.com/berlin 的事情,並希望從 127.0.0.1/?lat= 獲取數據柏林&lon=柏林
我不知道如何設定在請求位置時使用的查詢字串。
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:5000/;
}
location /berlin/ {
proxy_pass http://127.0.0.1:5000/?lat=52.5185931&lon=13.3941181/;
}
}
答案1
嘗試這個:
server {
listen 80;
location = / {
proxy_pass http://127.0.0.1:5000/;
}
location ~ ^\/(.*)$ {
proxy_pass http://127.0.0.1:5000/?lat=$1&lon=$1;
}
}
如果請求 example.com/berlin,第二個位置區塊將捕獲“berlin”,然後將其作為查詢參數傳遞到網頁伺服器。
但請注意,這似乎是一個非常糟糕的主意,因為這會匹配任何不要求您的主頁 ( /
) 的內容。因此,即使請求 example.com/index.html 也會以 /?lat=index.html&lon=index.html 的形式傳遞到網頁伺服器。您可以透過使用某種前綴(例如 example.com/city/berlin)或改進第二個位置區塊的正規表示式以不符合某些內容(例如 index.html)來防止這種情況發生