nginx - nginx 中 IF 语句的语法

标签 nginx syntax url-rewriting reverse-proxy

我在 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 语句的语法会引发错误。

最佳答案

没有 否则 nginx 中的指令。
此外,您也不需要复制位置。

尝试这个:

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

关于nginx - nginx 中 IF 语句的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32825703/

相关文章:

linux - Nginx 上的 Scalatra : how to set up?

nginx 504 网关超时

redirect - Nginx 非 www 到 www 和 www 到非 www

Javascript初学者语法问题

syntax - 这两种语法之间的区别

c# - 将 HTTPS 页面上的 URL 重写为 HTTP

ruby-on-rails - 带有 Nginx 和 SSL 握手的 Websockets

syntax - 关键字 var 后的下划线和接口(interface)名称是什么意思?

regex - URL 安全字符 RegEx 将允许使用 UTF-8 口音!

regex - IIS 重写规则以在包含特定特定查询字符串时删除查询字符串