2017-05-30 64 views
0

有沒有辦法在nginx中指定一個變量? 我想有www.example.com/dm/123如何在nginx路徑中指定一個變量?

和123將被傳遞到proxy_pass爲ID = 123,這裏是一個不完整的nginx的配置:

location /dm { 
      proxy_read_timeout 300s; 
        proxy_set_header X-Forwarded-Proto https; 
      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_pass http://localhost:3000/details?id=VARIABLE; 
      # index index.html index.htm; 
      proxy_redirect off; 
    } 

回答

0

您可以使用與命名變量的正則表達式:

location ~ /dm/(?<var>.+) { 
    ... 
    ... 
    proxy_pass http://localhost:3000/details?id=$var; 
} 
0

另一種選擇是位置捕獲:

location ~ /dm/(.+) { 
    ... 
    ... 
    proxy_pass http://localhost:3000/details?id=$1; 
}