8

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.

2
  • in if condition, you shouldn't need '<' for extension variable, it can be just $extension. Commented Sep 28, 2015 at 18:47
  • and try after removing '\.' after com Commented Sep 28, 2015 at 19:00

1 Answer 1

5

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;  
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Can you merge those two server_names as a single one as if is evil with the same functionality as mentioned above
which two server_names? my updated answer has only one. The evil part is having something other than "return" or "rewrite" inside the if block.
What about a map "$extension" $is_extension_empty. Then use this value in the server block with if ($is_extension_empty) { return 418 } , putting that if outside the location? I know, if + return in location aren't bad, but still if I can I tend to avoid them otherwise sooner or later, due to time constraints, one puts something else inside that if.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.