Wildcard subdomains in Nginx

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Wildcard subdomains in Nginx

Kunal Punjabi
I've been struggling with setting up nginx subdomains on my linode instance and setting up CNAME redirects. 

What I need is to be able to do:

1. First set up wildcard subdomains on my server (tinyadults.com), so that users can go to abc.tinyadults.com, xyz.tinyadults.com, etc. 
My server is running nuxt.js on port 4001 (default port is 3000 but I chose to use 4001 as a non-standard port), so I guess I have to use reverse proxies:

2. Then for my users I need to set up CNAME redirects from domain1.com to abc.tinyadults.com, and from domain2.com to xyz.tinyadults.com, so that if I visit domain1.com , it would serve the contents (without redirecting me) of abc.tinyadults.com. For testing purposes I have an additional domain (passivefinance.com) that we could use.

However, I've not been able to get step 1 working. Can someone who is experienced with nginx setup please guide me? 

Below is my nginx config from sites-available/tinyadults.com.conf:

server {

    index index.html index.htm;

    server_name tinyadults.com www.tinyadults.com;


    location / {

        # WARNING: https in proxy_pass does NOT WORK!! I spent half a day debugging this.

        #proxy_pass https://localhost:4001;

        proxy_pass http://localhost:4001;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection 'upgrade';

        proxy_set_header Host $host;

        proxy_cache_bypass $http_upgrade;

    }


    # Kunal: create a custom 404 nginx page, from https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

    error_page 404 /custom_404.html;

    location = /custom_404.html {

        root /etc/nginx/sites-available/custom_nginx_error_pages;

        internal;

    }


    listen [::]:4001 ssl http2; # managed by Certbot, modified by Kunal to add http2

    listen 4001 ssl http2; # managed by Certbot, modified by Kunal to add http2


    #Install SSL certificates and configure https:// on a per-domain-basis by running:

    #sudo certbot --nginx

    #(when prompted, be sure to select the option to set up redirects from http to https and effectively "disable" http)

    ssl_certificate /etc/letsencrypt/live/tinyadults.com-0001/fullchain.pem; # managed by Certbot

    ssl_certificate_key /etc/letsencrypt/live/tinyadults.com-0001/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}


server {

    server_name tinyadults.com;

    if ($host = tinyadults.com) {

        return 301 https://$host$request_uri;

    } # managed by Certbot


    listen 80 default_server;

    listen [::]:80 default_server;

    return 404; # managed by Certbot

}


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Wildcard subdomains in Nginx

Francis Daly
On Tue, Jul 21, 2020 at 11:41:31PM -0700, Kunal Punjabi wrote:

Hi there,

> I've been struggling with setting up nginx subdomains on my linode instance
> and setting up CNAME redirects.

I don't fully understand what you are trying to do here.

For example -- what do you mean by a CNAME redirect?

> *What I need is to be able to do:*
>
> 1. First set up wildcard subdomains on my server (tinyadults.com), so that
> users can go to abc.tinyadults.com, xyz.tinyadults.com, etc.

server_name *.tinyadults.com;

(See http://nginx.org/r/server_name)

Or, if the names should be handled differently:

server { server_name abc.tinyadults.com; }
server { server_name xyz.tinyadults.com; }

> My server is running nuxt.js on port 4001 (default port is 3000 but I chose
> to use 4001 as a non-standard port), so I guess I have to use reverse
> proxies:
> proxy_pass <a href="http://localhost:4001;">http://localhost:4001;

Ok. Does your nuxt.js service care whether the original request was for
the hostname abc or xyz? If so, you may want to indicate to it what the
original hostname was.

> 2. Then for my users I need to set up CNAME redirects from domain1.com to
> abc.tinyadults.com, and from domain2.com to xyz.tinyadults.com, so that if
> I visit domain1.com , it would serve the contents (without redirecting me)
> of abc.tinyadults.com.

I don't know what you mean by that.

Might it be

server { server_name abc.tinyadults.com domain1.com; }

or perhaps

server { server_name domain1.com;
  location / { proxy_pass https://abc.tinyadults.com; }
}

?

> However, I've not been able to get step 1 working. Can someone who is
> experienced with nginx setup please guide me?

Can you give some specific examples of "I make *this* request, and I
want to get *this* response"?

It looks like you have a nuxt.js http service listening on port 4001, and
you want an nginx https service to listen on port 443 and reverse-proxy
the 4001 service.

But your suggested nginx config seems to try to do something different
from that.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx