Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for IF Statement in nginx

I have two different server_name in nginx.conf file:
First one as:

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

Another one as

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

Now the thing is I want to use both the server_name based on the in the server_name. If there is no extension with the server_name it should go to first location. If there is an extension, it should go to second location.
But while running the nginx, it is not moving into the second server_name
Can anyone please find some solution for this...?
I thought a solution as(may be wrong).

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

But the syntax with the if statement throws an error.

like image 492
Nithin Veer Reddy Kankanti Avatar asked Nov 09 '22 04:11

Nithin Veer Reddy Kankanti


1 Answers

There's no else directive in nginx. Also, you don't need to duplicate the location either.

try this:

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

          }
}

Also, consider that if is evil.

This is the version with a safe usage of it:

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;  
    }
like image 64
danielgpm Avatar answered Dec 06 '22 04:12

danielgpm