adding www to my domain at nginx server

  • Posted January 21, 2014


I have been trying to add www to my domain like this:

from this:

to this:

I was following this article here:

but ended up with no result. I believe it is something related to either ect/nginx/nginx.config or my domain file in sites-available…

There is one question I also want to ask, if the domain re-directed from to, would that be considered an extra http request. is there a way how to avoid extra requests?

any idea how that can go correct??



Submit an 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.

First, a caveat: I’m new to Digital Ocean. But the following works on my other VPS, and I don’t see any reason why it shouldn’t work here: <br> <br> [from nginx.conf] <br> server { <br> listen 80; <br> server_name; <br> … <br> <br>or, if you want to use only the www.* form: <br> <br> server_name; <br> <br>If I’m not mistaken, there is no redirection when you do it this way (regardless of how many values you specify for server_name). <br> <br>Of course, you also need to make sure your DNS record includes the www.* form.

Look at here:

According to this link the solution is:

server {  
    rewrite ^(.*)$1 permanent;  

server {  
        listen 80 default_server;  
        listen [::]:80 default_server ipv6only=on;  

        root /home/user/www/domain1;  
        index index.php index.html index.htm;  


        include hhvm.conf;  


@matt, you are right. I guess there is no way around this anyways. I’ve been searching on the net for almost a day and got nothing, but only using proxy_pass or rewrite module through the site-available domain file config. <br> <br>and here is the solution to redirect from to <br>------------------------- <br>server { <br> listen 80; <br> server_name; <br> return 301$request_uri; <br>} <br> <br>server { <br> <br> listen 80; <br> listen [::]:80 default_server ipv6only=on; <br> server_name; <br>} <br>-------------------------------- <br>Thanks again. <br> <br>Regards,

Oh, I see, it’s one of those convoluted Debian configurations. I’m not saying “don’t use Debian,” but I think sometimes they make things much more complex than they need to be. <br>Hopefully some other Debian user will come along with a definitive answer. However, if you want to go exploring, my method should allow your server to handle the hostnames you want, without any redirects. Nginx configuration is pretty straightforward–if you find the main config file (I checked the Debian package DB, and there is an ‘nginx.conf’, so that should be it), it should contain one or more ‘include’ directives; if you follow those to whatever file contains the ‘server_name’ line, you should be able to make the change there. Best of luck with it!

Thanks @matt. <br> <br>I’ve already added www to my DNS. <br> <br>if I added www to the domain in the etc/nginx/sites-available/mydomain, not at nginx.conf in the nginx root folder. <br> <br>That works, but counted as an extra request. so an empty page takes 2 requests, which is a bad practice. <br> <br>I believe there a way how to do that, but not sure yet how!! <br> <br>regards

Oh, wait. Do you mean you really want a redirect, such that if a visitor types in ‘’, they will be redirected to ‘’? My response doesn’t help with that, it only enables the server to handle the ‘www’ form (or both forms, depending on which version you use).