Without any configuration on my part, nginx serves website for www. as well as the domain I set in server_name. I have no idea how this is happening, but I'd like to disable this.

Meaning: I've configured a server for subdomain.example.com, that works fine, however www.subdomain.example.com also works!

This is my nginx conf:

worker_processes  1;

events {
    worker_connections  1024;

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;

And then an example file from /etc/nginx/conf.d/:

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

    location / {
        proxy_pass http://656.655.665.655:3277;



I've checked and I've got no wildcards in any of the other configs.

When I add:

server {
  server_name www.subdomain.example.com;
  return 301 $scheme://subdomain.example.com$request_uri;

That works, and www. is redirected, but I don't like that solution, for various reasons I'd like to simply just disable the www. completely.

Any ideas?

  • I think it's likely your browser is doing this, as I believe there was some change to both chrome and firefox to 'ignore' the www. appended to a URL. I personally think it's a dumb idea but alas. Try doing a dns lookup for both of the urls, with and without the www. The one without should fail unless you've got a wildcard. If it does fail, it's clearly the browser interfering. – djsmiley2k - CoW Nov 1 '18 at 12:05
  • @djsmiley2k I don't think that's it. I've tried using curl and still goes through, without any mention of www in nginx config – mikicz Nov 1 '18 at 21:47

This Stack Overflow answer suggests using this code to remove www before any domain:

if ($host ~* ^www\.(.*)$) {
    rewrite / $scheme://$1 permanent;

Note that If Is Evil, but is said to work with rewrite.

  • This doesn't really answer my question though, I even mentioned that when I do the redirect it redirects correctly, I would like to disable the www.subdomain.example.com completely, but nginx just somehow makes it work, which is weird since I did not explicitly say anywhere that www.subdomain.example.com should do anything. Yes, if is evil, that's why I'm not using it in the redirect example... – mikicz Nov 1 '18 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.