FMCB
By:
FMCB

adding www to my domain at nginx server

January 21, 2014 10.1k views
Hi. I have been trying to add www to my domain like this: from this: http://domain.com to this: http://www.domain.com I was following this article here: https://www.digitalocean.com/community/articles/how-to-create-temporary-and-permanent-redirects-with-apache-and-nginx 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 http://domain.com to http://www.domain.com, would that be considered an extra http request. is there a way how to avoid extra requests? any idea how that can go correct?? Thanks
6 Answers
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:

[from nginx.conf]
server {
listen 80;
server_name mydomain.com www.mydomain.com;
...

or, if you want to use only the www.* form:

server_name www.mydomain.com;

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

Of course, you also need to make sure your DNS record includes the www.* form.
Oh, wait. Do you mean you really want a redirect, such that if a visitor types in 'domain.com', they will be redirected to 'www.domain.com'? 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).
Thanks @matt.

I've already added www to my DNS.

if I added www to the domain in the etc/nginx/sites-available/mydomain, not at nginx.conf in the nginx root folder.

That works, but counted as an extra request. so an empty page takes 2 requests, which is a bad practice.

I believe there a way how to do that, but not sure yet how!!

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.
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!
@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.

and here is the solution to redirect from mydomain.com to www.mydomain.com:
-------------------------
server {
listen 80;
server_name mydomain.com;
return 301 http://www.mydomain.com$request_uri;
}

server {

listen 80;
listen [::]:80 default_server ipv6only=on;
server_name www.mydomain.com;
}
--------------------------------
Thanks again.

Regards,

Look at here:

https://www.digitalocean.com/community/questions/configuring-multiple-domains-and-subdomains-dns-nginx-issues-abound

According to this link the solution is:

server {  
    server_name  domain1.com;  
    rewrite ^(.*) http://www.domain1.com$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;  

        server_name www.domain1.com;  

        include hhvm.conf;  

} 
Have another answer? Share your knowledge.