p.magalhaes
p.magalhaes

Reputation: 8374

How to test NGINX redirect

I just configured a Nginx with the conf file below:

server {
    listen       80;
    server_name teste.mysite1.com                              
    server_name  ^/$ https://www.mysite2.com.br permanent;
}

Then I would try to see if it is responding with 301 redirect.

I tried:

curl -i -H "Host: teste.mysite1.com" http://http://ec2-18-236-182-168.us-west-2.compute.amazonaws.com

I thought it would redirect but it's still returning http 200 response.

What am I doing wrong?

Upvotes: 1

Views: 10595

Answers (2)

Tan Hong Tat
Tan Hong Tat

Reputation: 6864

server {
    listen 80;
    server_name teste.mysite1.com
    return      301  https://www.mysite2.com.br;
}

Reload Nginx to use the new configuration.

curl -i -H "Host: teste.mysite1.com" http://ec2-18-236-182-168.us-west-2.compute.amazonaws.com

Upvotes: 1

embik
embik

Reputation: 1070

Your server block is wrong, you used server_name before the redirection logic. You need to configure it like this:

server {
    listen       80;
    server_name teste.mysite1.com                              
    rewrite  ^/$ https://www.mysite2.com.br permanent;
}

Be aware that this configuration will only redirect the website root. Remove /$ if you want any url on teste.mysite1.com redirected.

Upvotes: 1

Related Questions