简体   繁体   English

nginx将uri重写为文件

[英]nginx rewrite uri to file

I need nginx to open files by uri like: 我需要nginx来打开uri文件,如:

http://example.com/en/foo/test-bar from /var/www/example.com/en_US/_foo_test_bar 来自/var/www/example.com/en_US/_foo_test_bar的http://example.com/en/foo/test-bar

or 要么

http://example.com/en/foo2/test2-bar from /var/www/example.com/en_US/_foo2_test2_bar 来自/var/www/example.com/en_US/_foo2_test2_bar的http://example.com/en/foo2/test2-bar

replacing all symbols except letters/numbers to underline. 替换除字母/数字之外的所有符号以加下划线。 How should look this location, may be better would be using redirect? 怎么看这个位置,可能会更好用的是重定向?

location /en/ {
rewrite ...
}

I don't know best practice for such mechanism in nginx and regexp. 我不知道nginx和regexp中这种机制的最佳实践。 Thank you. 谢谢。

You can use rewrite to recursively change the symbols until they are all changed. 您可以使用rewrite来递归更改符号,直到它们全部更改为止。 Assuming that the real files are under en_US as your example shows: 假设真实文件在en_US下,如您的示例所示:

location /en/ {
    rewrite ^/en/(.*)[^A-Za-z0-9_](.*)$ /en/$1_$2 last;
    rewrite ^/en/(.*)$ /en_US/$1 last;
}
location /en_US/ {
    root /path/to/real/files;
}

The first rewrite repeatedly matches until all of the symbols are consumed. 第一次rewrite重复匹配,直到消耗掉所有符号。 The second rewrite prepares the URI to serve the real file. 第二次rewrite准备URI以提供真实文件。

See this document for more. 有关更多信息,请参阅此文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM