nginx:如何将默认文件名和扩展名附加到位置

时间:2018-02-19 17:34:36

标签: nginx

我使用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;
}

我在该位置之前添加了重写规则以静默方式重定向(不更改显示的网址)/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;
}

1 个答案:

答案 0 :(得分:0)

如编辑所述:我设法同时进行重定向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;
}
相关问题