【问题标题】:nginx : How to append a default file name and extension to a locationnginx:如何将默认文件名和扩展名附加到位置
【发布时间】:2018-02-19 17:34:36
【问题描述】:

我使用 nginx 作为代理服务器将一些请求 (location /mnt/) 转发到专用上游:

location /mnt/ {
  expires 1y;

  add_header Cache-Control public;
  add_header X-Proxy-Cache $upstream_cache_status;

  proxy_pass http://imaginary/; # match the name of upstream directive which is defined above
  proxy_set_header Host $host;
  proxy_set_header cf-ray '';
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_redirect off;
}

我在该位置之前添加了一个重写规则以静默重定向(不更改显示的 url)/mnt/thumbnail/xxx?yyy/mnt/thumbnail?yyy

location /mnt/thumbnail/ {
  rewrite ^/mnt/thumbnail/(.*)$ /mnt/thumbnail last;
}

这是按预期工作的。但可能不是最好的方法。

如果没有文件名结束路径,我想不出一种方法来设置永久重定向以将/image.jpg 附加到向/mnt/thumbnail?yyy 发出的请求,就像一种索引页一样。 我所有的尝试都以rewrite or internal redirection cycle while processing "/mnt/thumbnail/image.jpg" 或 404 失败。

[编辑] 我设法同时拥有重定向 301 和代理转发。我必须用 proxy_pass 替换重写以转发,然后我可以添加重写以附加 /image.jpg 如果丢失。

location = /mnt/thumbnail {
  rewrite ^/mnt/thumbnail?(.*)$ /mnt/thumbnail/image.jpeg permanent;
}

location /mnt/thumbnail/ {
  proxy_pass http://imaginary/thumbnail?$args;
}

【问题讨论】:

  • 你可以试试正则匹配。
  • 我试过用=修饰符和~*也喜欢这个位置= /mnt/thumbnail { rewrite ^/mnt/thumbnail$ /mnt/thumbnail/image.jpg; } 但我在以下方面得到 500 分:[error] 1746#1746: *1 rewrite or internal redirection cycle while processing "/mnt/thumbnail", client: 172.17.0.1, server: , request: "HEAD /mnt/thumbnail/image.jpg?**** HTTP/1.1", host: "tshirtpreviewer_web_1"
  • 我不确定你想要达到什么目的。如果您想要将/mnt/thumbnail/xxx 重写为/mnt/thumbnail/mnt/thumbnail//mnt/thumbnail/image.jpg,这已经是循环的了。
  • 确实,@dereli 以某种方式循环。请注意,第一次重写并没有更改 url,而只是进行了转发代理。第二个应该是一个真正的永久重定向,如果缺少文件和扩展名,则更改 url,默认为 image.jpg。我想,对转发代理使用重写不是一个好方法,它可能与默认 image.jpg 的永久重定向不兼容。
  • 我不是在谈论代理通行证,我的两个例子都是重写的。您能否用 3 个请求的预期结果更新问题:/mnt/file1.jpg/mnt/thumbnail/file1.jpg/mnt/thumbnail

标签: nginx


【解决方案1】:

正如编辑中所说:我设法同时拥有重定向 301 和代理转发。我必须用 proxy_pass 替换重写以转发,然后我可以添加重写以附加 /image.jpg 如果丢失。

location = /mnt/thumbnail {
  rewrite ^/mnt/thumbnail?(.*)$ /mnt/thumbnail/image.jpeg permanent;
}

location /mnt/thumbnail/ {
  proxy_pass http://imaginary/thumbnail?$args;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    相关资源
    最近更新 更多