Redirection issue

Hello All,

I am facing some issue regarding nginx redirection i’m unable to fix it.
I
want to create redirect non www to www but always redirect me to
default_server how i can fix this issue.

This is what i have

server {
listen 80 default_server;
server_name localhost;
root /home/nginx/default/public;


}

server {
listen 80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
root /home/user/public_html;

}

When i go to domain.com or www.domain.com i got default document root

If i change server_name domain.com; to server_name domain.com
www.domain.com; i got redirect loop issue.

Then how i suppose to solve this problem.

Posted at Nginx Forum:

On Fri, Oct 3, 2014 at 12:22 AM, Kurogane [email protected] wrote:

            server_name localhost;
    root  /home/user/public_html;


}

When i go to domain.com or www.domain.com i got default document root

If i change server_name domain.com; to server_name domain.com
www.domain.com; i got redirect loop issue.

Then how i suppose to solve this problem.

Do you have another server block with server_name www.domain.com which
supposed to handle the actual request?

No.

Posted at Nginx Forum:

On Fri, Oct 3, 2014 at 12:33 AM, Kurogane [email protected] wrote:

No.

There is your problem, the config supposed to looks like this:

server {
listen 80 default_server;
server_name localhost;
root /home/nginx/default/public;
}

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

server {
listen 80;
server_name www.domain.com;
root /home/user/public_html;

    # proceed with the rest of the config down here
    # ...

}

Also, it’s not mandatory, but I think you should set the default_server
on
the www.domain.com server block
instead of localhost.

Posted at Nginx Forum:

On Fri, Oct 3, 2014 at 12:39 AM, Adie N. [email protected]
wrote:

            server_name localhost;

server {
instead of localhost.


regards,
Nurahmadie

For reference,
http://nginx.org/en/docs/http/converting_rewrite_rules.html

Thats working thanks!!

I set default_server in localhost because i want to show default page
when
you go to http://1.2.3.4

Posted at Nginx Forum: