I’m a bit confused, I really thought this was going to be a simple 5 minute task, but no. I followed the documentation for a redirect, but I can’t get it working properly, it is just appending multiples of the new url (not sure how many, but I counted at least 10 off the edge of the url bar) to the original one. Here is my server block config:
server {
listen 80;
server_name old.i.p.address;
return 301 new.i.p.address/redirect_page/;
location ~ /si/home/0/ {
return 301 /si/home/;
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /home/rob/pcc_django/staticfiles/;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Apologies if indentation is wrong, it is formatted correctly on my machine, I haven’t worked out how to keep formatting in code blocks on this site correctly.
I have tried multiple versions with and without "http://" at the start of the new ip address, I’ve tried it with and without the "/homepage" also. I’ve also tried to use "rewrite" as initially I just wanted a permanent redirect, but this was the only iteration that would allow ngingx server to start and it’s giving strange results in the url bar:
old.i.p.address/homepage/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/new.i.p.address/redirect_page/...etc ad infinitum
How can I fix this please?
2
Answers
301 redirect will be cached in browser, you could clear the browser cache, and try
return 301 http://new.i.p.address/redirect_page/;
again.The
new.i.p.address/redirect_page/
string is treated by browser as relative URI on the same server giving you that endless loop with the concatenation ofnew.i.p.address/redirect_page/
part on every iteration. For example, if you’d had areturn 301 /new.i.p.address/redirect_page/;
rule, that string would be treated as absolute URI giving you the same address on every iteration. The only right way is to usehttp://
(orhttps://
) prefix. As already said by @emptyhua, HTTP 301 permanent redirects are cached by the browsers so you should clear your cache or try from the incognito window.