nginx redirect http to https not working
Solution 1
You haven't defined a server name for your host.
server {
listen 80;
server_name *.example.com example.com;
return 301 https://$host$request_uri;
}
otherwise your host is not called. When you look to the example you can see that there is a server name defined in both cases.
Solution 2
I had a similar fault. Instead of redirecting the page kept 404.
Turned out to be a conflict between the configurations.
My config were put in /etc/nginx/conf.d/. What I did not notice was that in /etc/nginx/sites-enabled/ a default config was located which also listened on port 80 wich had higher precenednce than my conf in conf.d. Simply by removing the default config resolved my issue and the redirect worked properly.
Related videos on Youtube
thotho
Updated on September 18, 2022Comments
-
thotho over 1 year
I am trying to reroute from http to https. For example you visit example.com and you will be automatically redirected to https://example.com.
I tryed using this one:
server { listen 80; return 301 https://$host$request_uri; }
as well as this one:
server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; }
as found here : In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?
But neither of seem are wokring for me. I am staying on example.com.
Anyone got an idea?
-
Michael Hampton about 9 yearsDid you clear your browser cache?
-
thotho about 9 yearsYes, I also tryed it on other devices that have never visited the site before.
-
Paul about 9 yearsJust to cover the basics, you reloaded the nginx service? Does running
# nginx -t
tell you of any errors? Is there anything in your error logs? Are you connecting to the server from an IPv4 address? -
thotho about 9 yearsnginx -t is is telling me that the syntax is ok and that the test was succesfull and the error logs aren't showing anything either. And yes I am only using IPv4 addresses.
-
Navern about 9 yearsActually you problem lies in somewhere else. This piece of code is working. Add logging to your server definition and check nginx logs for access and errors.
-
-
thotho about 9 yearsOf course I have defined a server name? I just put in example.com as an example.
-
René Höhle about 9 yearsYes sorry but i think you have forgot to define all wo with www and without and thats why your host is not used.
-
thotho about 9 yearsThank you very much, the first version was working for me!
-
thotho about 9 yearssadly too early.. it worked on my notebook and phone once but now it's not working anymore..
-
thotho about 9 yearsI am using this now and it is now working
-
thotho about 9 yearsnot wokring as written here: serverfault.com/questions/680580/…
-
thotho about 9 years
curl -I -L http://example.com
is giving me this unreadable reply: oU<▒ ▒▒x8▒▒▒▒S#vR▒*▒ ▒▒w▒K~jO▒▒▒▒C9L▒▒/.▒S▒9tU▒▒▒1▒▒▒͚Ø)$E▒▒p!▒7|▒Hy▒o▒8 -
Jchieppa about 9 yearsCheck curl -V to see if libz is included, my guess is compressed content and curl cannot decode it. Otherwise that server block is correct, so start checking outside factors including making sure the dns entry is correct and requests are actually going to this nginx host. Check the backend if nginx isn't your primary webserver and you should also check for any other server blocks in nginx with the same server as they may be setup for port 80 and grabbing precedence. More information about your infastructure may help us provide you with better ideas.
-
thotho about 9 yearsWell I'd guess nginx is working correctly and no other servers are using Port 80 because I am getting this message
Welcome to nginx on Debian! If you see this page, the nginx web server is successfully installed and working on Debian. Further configuration is required.
when opening example.com. Let me tell you my setup. I got a Raspberrry Pi 2 with the base of OSMC. Ontop of it I installed my own cloud with Seafile and I have Seafile running behind nginx. Also pyLoad running on Port 8000. So that shouldn't interfere either. If visiting example.com everything is working fine! -
chovy over 5 yearsi have a default in ./sites-enabled and removing it solved my problem, but it works fine on one server and not on the other. Removing default makes it work but I cannot figure out why it is conflicting.