nginx redirect http to https not working [closed]
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?
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.