2017-05-30 128 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; 
}