Question

Ignore fix subdomain and route to specific path

Posted February 27, 2020 579 views
Nginx

I am using Nginx. and I have domain ex:- example.com

and now what I want is I have to setup this rule in nginx.conf if anyone hit any other subdomain just like abc.example.com then it will refer to example.com/sites/fivespan except admin.example.com, manage.example.com, sync.example.com, schedule.example.com. be sure that it will not redirect to that URL. so how to do that and whats the syntax.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello,

You could add a wildcard server block for all of your domains like this:

server {
    listen  80;
    server_name *.domain.com;

    root /var/www/example.com/sites/fivespan;

}

If you are not sure how to create server blocks you could take a look at this tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Then after that you can create separate server blocks for each subdomain name that you would like to have specific content for.

Hope that this helps!
Regards,
Bobby

by Justin Ellingwood
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
  • No its not working for me. I want to ignore this subdomain admin , sync , manage , scheduler in Nginx. here is my nginx.conf file

    server {
        listen 80;
        server_name *.example.com;
    
        location = / {
            proxy_pass http://demo-server:4000/sites/fivespan/;
    
        }
    
    
       location ~ /(.*) {
          proxy_pass  http://demo-server:4000/$1;
        }
    
       }
    
    
    
    • Hi there @bittu,

      I’ve set up a new Nginx server and tested this config and it works as expected.

      One thing that you need to keep in mind is that in order to exclude those subdomain names that you’ve mentioned, you need to explicitly create separate server block configs for them as well.

      That way Nginx would serve the specific content of those subdomains rather than the catch-all one.

      For more information you could take a look at the official documentation here:

      http://nginx.org/en/docs/http/server_names.html

      Hope that this helps!
      Regards,
      Bobby

      • Yes, I am asking how to exclude these subdomains in my nginx.conf, show me some sample.

        • Hi there @bittu,

          You would just need to add another Nginx server block for the other subdomains, for exmaple, let’s say that you have your wildcard server block at:

          /etc/nginx/sites-enabled/wildcard_example.com
          

          And it has the following content:

          server {
              listen 80;
              server_name *.example.com;
          
              location = / {
                  proxy_pass http://demo-server:4000/sites/fivespan/;
          
              }
          
          
             location ~ /(.*) {
                proxy_pass  http://demo-server:4000/$1;
              }
          }
          

          Now the next thing that you need to do is create another server block, you can do that by adding another file in your sites-available folder and then creating a symlink to your sites-enabled folder as described here.

          Then the new file that you have would be available at: /etc/nginx/sites-enabled/admin.example.com, and it would have the following content:

          server {
              listen 80;
              server_name admin.example.com;
              root /var/www/html;
          
                  location / {
                          # First attempt to serve request as file, then
                          # as directory, then fall back to displaying a 404.
                          try_files $uri $uri/ =404;
                  }
          }
          

          This way you will have your wildcard server block but also have a separate server block for any specific subdomain name that you would like to be excluded from the wildcard.

          Hope that this helps!
          Regards,
          Bobby

          by Justin Ellingwood
          When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
Submit an Answer