• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

WWW to NON-WWW on Nginx

#2
Can you paste the file from /etc/nginx/conf.d/default.conf or whatever you are using as name here? Then I'll add the block and explain what it will do for you.
 
#4
In short, you need to add a new server block outside of the already excisting server block that is written in /etc/nginx/conf.d/default.conf (pay attention though, as this file could be named something different as well)

In this case I already had code required to run xenforo. The snippet starting with server { at the top is the one you need to add at the top or at the end of the file, it should redirect all non-www requests to a www equivalent by redirecting it through nginx.

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

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$uri&$args;
    }
    location /internal_data/ {
        internal;
    }
    location /library/ {
        internal;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
 

Sheratan

Well-known member
#5
In short, you need to add a new server block outside of the already excisting server block that is written in /etc/nginx/conf.d/default.conf (pay attention though, as this file could be named something different as well)

In this case I already had code required to run xenforo. The snippet starting with server { at the top is the one you need to add at the top or at the end of the file, it should redirect all non-www requests to a www equivalent by redirecting it through nginx.

Code:
server {
    listen       80;
    server_name  yourdomainname.com;
    return       301 www.yourdomainname.com$request_uri;
}
server {
    listen       80;
    server_name  www.yourdomainname.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$uri&$args;
    }
    location /internal_data/ {
        internal;
    }
    location /library/ {
        internal;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
That's non-WWW to WWW. This is WWW to non-WWW:

Code:
server {
listen 80;
server_name www.yourdomainname.com;
return 301 yourdomainname.com$request_uri;
}
server {
listen 80;
server_name yourdomainname.com;
...
 

Markos

Well-known member
#6
Sorry for late reply and thank you all! I forgot to mention that i use Centmin.

Currently i haven't resolved...Tried to find /etc/nginx/conf.d/default.conf without luck.
 

SneakyDave

Well-known member
#7
By default centminmod should have some commented out section at he top of your site's nginx configuration. You can use that, and just reverse it to what you want, it's what I do.

This is how mine looks after editing it (redirecting www to non-www).

You also have to remove the www name from the "server_name" directive:
Code:
server {
            listen   80;
            server_name www.sitename.com;
            return 301 $scheme://sitename.com$request_uri;
      }

server {
  server_name sitename.com;
..snip...
Your configuration files for centminmod nginx should be in
Code:
/usr/local/nginx/conf/conf.d
 

Tracy Perry

Well-known member
#8
CentMinMod does the vhosts in the /usr/local/nginx/conf/conf.d location if I remember correctly. The default should be there also (I deleted mine and use custom ones so not sure).
The files themselves for the vhosts are normally kept in the /home/nginx/domains location.