如何在别名中使用regexp& root在nginx?

时间:2016-07-16 08:46:28

标签: nginx location alias root

我需要通过nginx为用户提供一些文件。用户的请求URL与服务器中的真实文件路径之间的位置不同。所以我尝试在位置使用别名

请求网址:
1)http://info.myservice.com/statement/ 12345678 /12/34/A.html
2)http://info.myservice.com/statement/ 57837873 /56/78/B.html
...

服务器中的实际路径:
1)/data/statement/0/12/34/A.html
2)/data/statement/0/56/78/B.html


位置设置:

 location ~ /statement/[0-9]+ {
    if ($request_uri ~ "/(d+|-)(.*)") {
            #access_log off;
            access_log /var/log/nginx/access.log  main;
    }
    alias /data/statement/0;
}

但它不起作用。需要一些建议..

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用root指令和rewrite ... break来使网址与实际路径匹配:

location /statement/ {
    root /data/statement/0;
    rewrite ^/statement/[0-9]+(/.*)$ $1 break;
}

有关详细信息,请参阅this document