Nginx的代理传递和URL重写

只有当我有url中的参数时才能触发这个规则,否则我会匹配一个别名。

location ~^/static/photos/.* { rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break; expires 7d; proxy_pass http://foofoofoo.com; include /etc/nginx/proxy.conf; } 

您可以添加一个条件来检查$ args参数是否为空:

 location ~^/static/photos/.* { if ($args != ''){ rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break; expires 7d; proxy_pass http://foofoofoo.com; include /etc/nginx/proxy.conf; } } 

* (表示0或更多)replace为a(表示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; } 

现在我的答案是我更好地理解这个问题:

你尝试过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; }