![Nginx 代理传递和 URL 重写](https://linux22.com/image/802549/Nginx%20%E4%BB%A3%E7%90%86%E4%BC%A0%E9%80%92%E5%92%8C%20URL%20%E9%87%8D%E5%86%99.png)
仅当我在 url 中有参数时才触发此规则,否则我将匹配别名。
location ~^/static/photos/.* {
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
include /etc/nginx/proxy.conf;
}
答案1
您可以添加一个条件来检查$参数参数不为空:
location ~^/static/photos/.* {
if ($args != ''){
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
include /etc/nginx/proxy.conf;
}
}
答案2
将*
(表示 0 个或更多)替换为+
(表示 1 个或更多)
例如:
location ~^/static/photos/.* {
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
include /etc/nginx/proxy.conf;
}
变成:
location ~^/static/photos/.+ {
rewrite ^/static/photos/(.+)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
include /etc/nginx/proxy.conf;
}
答案3
现在我对这个问题有了更好的理解,下面就是我的答案:
你有没有尝试过try_files?
例如它可能看起来像这样:
try_files /DynamicPhotoQualitySwitch/photos/$args /path/for/no/args;
location /path/for/no/args {
expires 7d;
proxy_pass http://foofoofoo.com;
include /etc/nginx/proxy.conf;
}