Nginx 代理传递和 URL 重写

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;
     }

答案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;
}

相关内容