【问题标题】:nginx rewrite uri to filenginx重写uri到文件
【发布时间】:2017-04-07 10:44:13
【问题描述】:

我需要 nginx 通过 uri 打开文件,例如:

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

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

将除字母/数字以外的所有符号替换为下划线。应该如何看待这个位置,使用重定向可能会更好?

location /en/ {
rewrite ...
}

我不知道 nginx 和 regexp 中这种机制的最佳实践。谢谢。

【问题讨论】:

    标签: regex redirect nginx url-rewriting nginx-location


    【解决方案1】:

    您可以使用rewrite 递归更改符号,直到它们全部更改。假设真实文件位于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;
    }
    

    第一个rewrite 重复匹配,直到所有符号都用完。第二个 rewrite 准备 URI 以提供真实文件。

    请参阅this document 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-26
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多