Nginx服务器能够检查URL中的多个参数,使它们服务于同一个文件吗?

huangapple go评论54阅读模式
英文:

Can ngnix server check for multiple params in the url making them serve same file?

问题

由于下面的两个“if语句”检查URL中不同的参数,例如第一个检查“flag1”,第二个检查“flag2”,但都服务于相同类型的文件,我想将这两个检查合并到一个“if语句”中:

location /app/ {
location ~ "^(.)/([^/]+)_flag.(.)$" {
try_files $uri $1/$2.$3 =404;
}

if ( $args ~ "^.*flag1.*$" || $args ~ "^.*flag2.*$" ) {
    rewrite "^(.*)/([^/]+(?<!_flag))\.(.*)$" "$1/$2_flag.$3" last;
}

}

英文:

Since both of the below 'if statements' serve the same type of file but check for different parameters in the URL, like the first one checks for 'flag1' and the second one checks for 'flag2', I want to merge both of these checks into just one 'if statement' serving that file:

location /app/ {
    location ~ &quot;^(.*)/([^/]+)_flag\.(.*)$&quot; {
        try_files $uri $1/$2.$3 =404;
    }

    if ( $args ~ &quot;^.*flag1.*$&quot; ) {
        rewrite ^(.*)/([^/]+(?&lt;!_flag))\.(.*)$ $1/$2_flag.$3 last;
    }

    if ( $args ~ &quot;^.*flag2.*$&quot; ) {
        rewrite ^(.*)/([^/]+(?&lt;!_flag))\.(.*)$ $1/$2_flag.$3 last;
    }
}

Can this be possible?

答案1

得分: 1

是的,这是可能的。你应该使用or条件:

if ( $args ~ "(^.*flag1.*$)|(^.*flag2.*$)" )
英文:

Yes it's possible. You should use or condition:

if ( $args ~ &quot;(^.*flag1.*$)|(^.*flag2.*$)&quot; )

huangapple
  • 本文由 发表于 2023年4月6日 21:16:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定