Report this

What is the reason for this report?

Nginx redirect only root domain but not subdomain to www

Posted on June 28, 2017

Currently I’m using setting below to redirect non-www domain to www domain and it’s working fine:

server { listen 80; server_name example.com; return 301 http://www.example.com$request_uri; }

server { listen 80; server_name www.example.com; location / { proxy_pass http://www.example.com:8888; 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; } }

However, now I would like to allow wildcard subdomain but it seems like all the subdomains were being redirect to www.domain.com. So my question is how can I make it only redirect the root domain to www only and excluding all other subdomain? Thanks.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hi @menuassist

With that configuration, Nginx should not redirect wildcard subdomains. Are you sure you don’t have another configuration file some place messing with the redirect? Can you show the wildcard subdomain configuration example?

Also, please use the </> button in the comment editor to save the code formatting.

@menuassist

Normally, to setup Wild Cards, you’d need to have a * A entry in your DNS (pointing to your Droplet IP) and then a Wild Card entry in server_name.

i.e.

A       *       DROPLET_IP
server_name example.com *.example.com;

The above server_name would direct any sub-domain that doesn’t have an existing server block to the block you set it up in.

Your current block shouldn’t technically allow sub-domains to resolve to that server block, unless you do have another server block setup that’s conflicting with the one you’ve posted.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.