【问题标题】:Nginx: how to add a rewrite rule to redirect all the requests for a file type?Nginx:如何添加重写规则来重定向文件类型的所有请求?
【发布时间】:2018-08-31 11:20:40
【问题描述】:

我想为 Nginx 编写一个重写规则,以将任何对 javascript 文件的请求重定向到特定的 JS 文件。例如“http://example.com/path1/path2/sample.js?a=1&b=2”应重定向到“http://example.com/myjavascript.js”。所以我将此规则添加到 /etc/nginx/sites-enabled/example.com 配置文件:

rewrite .*\.js.* myjavascript.js last;

因此任何包含 .js 的 URL 都应替换为 myjavascript.js。但是当我尝试使用 URL 'http://example.com/path1/path2/sample.js?a=1&b=2' 时,它会失败(404)。但是,奇怪的是,如果我将 js 文件替换为 HTML 文件,例如:

rewrite .*\.js.* /hello.html last;

它正确地重定向它。 myjavascript.js 与 hello.html 位于同一文件夹中,但它没有将其正确重定向到此路径。

你能告诉我为什么它不起作用吗?

【问题讨论】:

    标签: nginx mod-rewrite url-rewriting url-redirection


    【解决方案1】:

    问题是javascript文件名前缺少'/',所以正确的应该是:

    rewrite .*\.js.* /myjavascript.js last;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      相关资源
      最近更新 更多