VirtualMB
By:
VirtualMB

How To Permanetly Redirect To WWW On NGINX?

August 30, 2014 5.7k views

Hi,

Could you please step by step me on how to do this task?

Thanks in advance!

2 Answers

Head to your domains configuration (most likely /etc/nginx/sites-available), than create a file named example.com and inside put the following context:

server {
    listen       80;
    server_name  www.example.com;
    ...
}

And configure www.example.com in another file with the normal configuration.

Good luck.

  • Hi Captian, thanks for the steps! I have few questions:

    1- Inside nginx I will create a file. The file extension can be .html?

    2- Configure www.example.com in another file with normal configuration.. I got lost there. Sorry, I just started to learn about all these..

  • What you need to do is basically open a VirtualHost files, each for one domain name. www.example.com and example.com are different virtual hosts.
    Inside www. file you need to create the redirection, and in the example.com put the normal nginx configuration context.

    Create a file named example.com (your domain, without any extensions):

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

    This will make every request to go to the www. host.

    Now open a new file named www.example.com and put in the following context:

    server
    {
        server_name www.example.com;
    
        access_log /var/log/nginx/www.example.com.access.log;
    
            error_log /var/log/nginx/www.example.com.error.log;
    
        root /home/the_user_home_directory;
    
        index index.php index.html index.htm;
    
        # use fastcgi for all php files
        location ~ \.php$
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        # deny access to apache .htaccess files
        location ~ /\.ht
        {
            deny all;
        }
    }
    
    • The server_name parameter is the domain name.
    • The root parameter is the home directory of the user (like /home/admin/public_html)
    • The index parameter is the root file of the domain (Nginx will search for index.php, if no results up, than moves on to index.html and so on..)
    • The fastcgi parameters are for enabling PHP-FPM
    • And finally we are disabling access to the HTACCESS file from the client.

    Good luck :)

  • Thanks! Observation: On the " root /home/ " There is nothing in this directory What I do there?

  • When you create a user on your server you can assign him a specific folder which will be his home directory.
    You need to read some articles to understand this.

  • For what it's worth, that can all go in a single config file.

You don't necessarily need to redirect. A rewrite will also work. Just add another server block to the config file for the domain.

 server {
    listen 80;
    server_name bedom.com;
    rewrite ^(.*)$ $scheme://www.bedom.com$1;
  }
  • Hi Larry,

    Could you please see what I am doing wrong here? I have changed the default.conf and now I can't see my index page. http://virtualmailbox.center

    server {
    listen 80;
    servername virtualmailbox.center www.virtualmailbox.center;
    rewrite ^(.*)$ $scheme://http://www.virtualmailbox.center$1;
    #charset koi8-r;
    #access
    log /var/log/nginx/log/host.access.log main;

    location / {
    root /var/www/virtualmailbox.center;
        index  index.html index.htm;
    }
    
    #error_page  404              /404.html;
    
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    

    }

  • The problem is probably this line:

    rewrite ^(.*)$ $scheme://http://www.virtualmailbox.center$1;

    Remove the http:// from that line. That part is handled by the $scheme://, which is just a handy way to ensure that the URL gets rewritten to the proper scheme, so that if the user is using http, it gets rewritten to http, and if https, it rewrites to https. This is what you should be using:

    rewrite ^(.*)$ $scheme://www.virtualmailbox.center$1;

    Also change the server_name line to read

    server_name virtualmailbox.center;

    Having www.virtualmailbox.center in that block may very well result in an infinite rewrite loop as www.virtualmailbox.center URLs continuously get rewritten to www.virtualmailbox.center.

    What you need is two server blocks - one that rewrites virtualmailbox.center to www.virtualmailbox.center, and the other that is actually the configuration for www.virtualmailbox.center. So what you will end up with in your config file is two server blocks like this:

    server {
        listen 80;
        server_name virtualmailbox.center;
        rewrite ^(.*)$ $scheme://www.virtualmailbox.center$1;
    }
    
    server {
        listen 80;
        server_name www.virtualmailbox.center;
    }
    
    ...All the rest of your configuration...
    

    That should work much better. Just for your information and further study, this is the page I got the rewrite code from https://blog.engineyard.com/2011/useful-rewrites-for-nginx.

  • I did Larry, but still i get the no page...

  • Did you reload the nginx configuration or restart nginx?

    service nginx reload or service nginx restart

  • I only know how to re-boot the server. Does this is the same thing as restart NGINX?

  • A reboot will also do it. Any time you make changes to the config file, the configuration must at least be reloaded or the server restarted in order for those changes to take effect.

    It doesn't much matter how you do it at this point. The reload option exists to allow changes to a running server without interrupting anyone who might be connected to the server at the time, and the restart command exists so that you can restart the web server without having to reboot virtual machine and interrupt whatever other services might be running on it.

  • I give up man.. I just can't get it to work. I asked for help in the support area but it looks like they want me to search the fix.

  • I just noticed that I did mislead you slightly on what your config file should look like.

    The rest of your configuration goes inside the second server block, not after it.

    server {
        listen 80;
        server_name virtualmailbox.center;
        rewrite ^(.*)$ $scheme://www.virtualmailbox.center$1;
    }
    
    server {
        listen 80;
        server_name www.virtualmailbox.center;
    
        ...All the rest of your configuration...
    }
    

    Just in case that makes a difference.

    Post your entire config file here and let me have a final look at it before you give up. I think it's a very simple fix. If the problem isn't what I think it is, then I will have reached the end of my usefulness to you, and then you can give up.

  • Thanks Larry! Does this look good?

    server {
    listen 80;
    server_name virtualmailbox.center;
    rewrite ^(.*)$ $scheme://www.virtualmailbox.center$1;
    }

    server {
    listen 80;
    server_name www.virtualmailbox.center;

    return 301 $scheme://www.virtualmailbox.center$request_uri;
    
    charset koi8-r; accesslog /var/log/nginx/log/host.access.log main;
    location / {
    root /var/www/virtualmailbox.center/;
        index  index.html index.htm;
    

    }

    #error_page  404              /404.html;
    
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
    

    }

  • The problem is in your second server block:

    return 301 $scheme://www.virtualmailbox.center$request_uri;

    Returning 301 is a permanent redirect. You can either rewrite or redirect. You don't need to do both, and the redirect in the second block is creating a redirect loop that continues until the browser gives up. If you prefer to redirect, that line should go in place of the rewrite in the first block. For now, just remove that line and restart, and you should see the result you were expecting.

Have another answer? Share your knowledge.