Setting up subdomains with nginx and Namecheap

6.1k views Asked by At

There seem to be a lot of different conflicting ways of doing things, so I'm a little confused and need some help. I have a few sites running on my server on different ports. I have a project management site on port 8085, a dashboard on 3000, etc. I would like to create subdomains for each of these, and I was hoping to be able to do it using nginx rather than masking.

Right now I have a DNS A record pointing to my IP address, and it successfully shows the "Welcome to nginx!" page. How do I make it so that if I visit the page with, say, projects.url.com nginx will proxy pass it to http://localhost:8085 and the correct url will continue to show in the browser?

Thanks for any help!

This is my current sites file snippet, and it doesn't seem to do the trick:

server {
listen 80;
server_name projects.calben.xyz www.projects.calben.xyz;
location / {
    proxy_pass http://localhost:8085/;
}
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}
1

There are 1 answers

4
Tarun Lalwani On BEST ANSWER

First setup a wildcard subdomain to point to your server IP. Or if you want you can setup the specific subdomains your want.

https://www.namecheap.com/support/knowledgebase/article.aspx/597/2237/how-can-i-set-up-a-catchall-wildcard-subdomain

Assuming you have a nginx with configs maintained at /etc/nginx/sites-available

You will backup and delete the /etc/nginx/sites-available/default.conf. Then you will add file with your subdomain name

sub.example.com.conf

server {
   listen 80;
   server_name sub.example.com;

   location / {
      proxy_pass http://127.0.0.1:8085;
   }
}

Then symlink this file in /etc/nginx/sites-enabled/sub.example.com.conf and restart nginx. Now your site should be accessible at http://sub.example.com