1

我是 nginx 新手,真的不知道如何做到这一点。例如,下面的代码在 htaccess 中的作用是

如果浏览器从这个网址请求这个 css 文件

http://www.example.com/folder1/css/style-30493094.css

然后它显示这个文件

http://www.example.com/folder1/css/style.css

此代码中所有其他定义的扩展也是如此。css, js, png, jpg, gif 就像浏览器从这个网址请求这个 js 文件一样

http://www.example.com/folder1/js/javascript-2938293.js

然后它显示这个文件

http://www.example.com/folder1/js/javascript.js

其实我正在努力做到这一点。http://derek.io/blog/2009/auto-versioning-javascript-and-css-files/

RewriteEngine On
RewriteRule ^folder1/(css|js|images)/([a-z]+)-([0-9]+)\.(css|js|png|jpg|gif)$ /folder1/$1/$2.$4 [L]
4

1 回答 1

1

基于此:http ://wiki.nginx.org/HttpRewriteModule#rewrite 看起来您可以使用当前的正则表达式并在 nginx.conf 中使用它。

rewrite ^folder1/(css|js|images)/([a-z]+)-([0-9]+)\.(css|js|png|jpg|gif)$ /folder1/$1/$2.$4 last;

所以你的整个服务器块可能看起来像:

server {
    listen 192.168.0.1;
    server_name blah.blah.com;

    location ~ \.(css|js|png|jpg|gif) {
        rewrite ^folder1/(css|js|images)/([a-z]+)-([0-9]+)\.(css|js|png|jpg|gif)$ /folder1/$1/$2.$4 last;
    }
}

编辑:您只需要适当地调整正则表达式。在您提供的情况下:

rewrite ^(.*/?)([a-z]+)-([0-9]+)\.(css|js|png|jpg|gif)$ /$1$2.$4 last;

这将重写:

[Anything with an optional trailing slash (or nothing)] followed by [at least one alphabetic character (lowercase only)] followed by [a hyphen] followed by [at least one numeric character] followed by [a dot] followed by [one of css|js|png|jpg|gif]

[That first optional text followed by an optional slash][the alphabetic part (i.e. style)].[the final css|js|png|jpg|gif]
于 2012-10-01T12:08:26.673 回答