【问题标题】:Syntax for IF Statement in nginxnginx 中 IF 语句的语法
【发布时间】:2015-12-25 20:31:33
【问题描述】:

我在 nginx.conf 文件中有两个不同的 server_name:
第一个为:

server_name ~^(?<subdomain>.+)\.nithinveer\.com$;
    location /
            {
                    proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
                    access_log /var/log/nginx/true.log;
            }

另一个是

server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>)$;
            location /extension 
            {
                    proxy_pass      http://192.168.6.190;
                    access_log /var/log/nginx/false.log;
            }

现在问题是我想同时使用基于 server_name 中的 server_name。如果没有带有 server_name 的扩展名,它应该转到第一个位置。如果有分机,它应该转到第二个位置。
但是在运行 nginx 时,它并没有移动到第二个 server_name
任何人都可以为此找到一些解决方案吗...?
我认为一个解决方案(可能是错误的)。

server_name ~^(?<subdomain>.+)\.nithinveer\.com\.(?<extension>.+)$;
            if($<extension> == NULL)
             {
                   location /
                        {
                    proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
                    access_log /var/log/nginx/true.log;
                         }
              }
              else
            {       location /
                        {
                          proxy_pass      http://192.168.6.190;
                          access_log /var/log/nginx/false.log;
            }

但是 if 语句的语法会抛出错误。

【问题讨论】:

  • 在if条件下,扩展变量不需要'
  • 并在删除 '\.' 后尝试com之后

标签: nginx syntax url-rewriting reverse-proxy


【解决方案1】:

nginx 中没有 else 指令。 此外,您也不需要复制该位置。

试试这个:

server {
    server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;

    location / {
            This will match hosts terminating with ".com"
            if ($extension = "")
              {
                proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
                access_log /var/log/nginx/true.log;
              } 

            This will match hosts with something after ".com", e.g: "foo.nithinveer.com.me", the $extension will be ".me"
            if ($extension != "")
              {                    
                proxy_pass      http://192.168.6.190;
                access_log /var/log/nginx/false.log;
              }                

          }
}

另外,考虑if is evil

这是具有安全用法的版本:

server_name ~^(?<subdomain>.+).nithinveer\.com(?<extension>\..+)$;
    location / {
        error_page 418 = @with_extension;

        #This will match hosts with something after ".com", 
        # e.g: "foo.nithinveer.com.me", the $extension will be ".me"
        if ($extension != "")
          {                    
            return 418;
          }                

        #  This will match hosts terminating with ".com"       
        proxy_pass http://192.168.6.190/Profiles/$subdomain/default.aspx$request_uri/;
        access_log /var/log/nginx/true.log;

      }

    location @with_extension {        
        proxy_pass      http://192.168.6.190;
        access_log /var/log/nginx/false.log;  
    }

【讨论】:

  • 你能把这两个 server_name 合并为一个,就像上面提到的那样是邪恶的吗
  • 哪两个 server_names?我更新的答案只有一个。邪恶的部分是在 if 块内有除“return”或“rewrite”之外的东西。
  • map "$extension" $is_extension_empty 怎么样。然后在带有 if ($is_extension_empty) { return 418 } 的服务器块中使用这个值,如果在该位置之外?我知道,if + return 的位置还不错,但如果可以的话,我迟早会避免使用它们,否则,由于时间限制,人们会在其中放入其他东西。
猜你喜欢
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多