如何在nginx路径中指定变量?

时间:2017-05-30 14:57:56

标签: nginx

有没有办法在nginx中指定变量?   我想要www.example.com/dm/123

和123将作为?id = 123传递给proxy_pass,这里是一个不完整的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;
    }

2 个答案:

答案 0 :(得分:0)

您可以将正则表达式与命名变量一起使用:

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

答案 1 :(得分:0)

另一种选择是位置捕捉:

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