1. 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

Discussion in 'Server Configuration and Hosting' started by Markos, Feb 21, 2015.

  1. Markos

    Markos Well-Known Member

    I need to have my site aivable only on site.com.

    And www.site.com should redirect on site.com

    I found this tutorial but the code doesn't work to me....tried to past on .htaccess file without results.

    How can i do this on Nginx?
     
  2. Nicky Vermeersch

    Nicky Vermeersch Active Member

    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.
     
  3. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    nginx do not support .htaccess so you have to directly edit the .conf file associated with the domain and paste the changes there :)
     
  4. Nicky Vermeersch

    Nicky Vermeersch Active Member

    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;
        }
    }
     
  5. Sheratan

    Sheratan Well-Known Member

    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;
    ...
    
     
    Nicky Vermeersch likes this.
  6. Markos

    Markos Well-Known Member

    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.
     
  7. SneakyDave

    SneakyDave Well-Known Member

    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
     
  8. Tracy Perry

    Tracy Perry Well-Known Member

    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.
     
    SneakyDave likes this.

Share This Page