nginx redirect http to https not working

31,925

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.

Share:
31,925

Related videos on Youtube

thotho
Author by

thotho

Updated on September 18, 2022

Comments

  • thotho
    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
      Michael Hampton about 9 years
      Did you clear your browser cache?
    • thotho
      thotho about 9 years
      Yes, I also tryed it on other devices that have never visited the site before.
    • Paul
      Paul about 9 years
      Just 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
      thotho about 9 years
      nginx -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
      Navern about 9 years
      Actually 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
    thotho about 9 years
    Of course I have defined a server name? I just put in example.com as an example.
  • René Höhle
    René Höhle about 9 years
    Yes sorry but i think you have forgot to define all wo with www and without and thats why your host is not used.
  • thotho
    thotho about 9 years
    Thank you very much, the first version was working for me!
  • thotho
    thotho about 9 years
    sadly too early.. it worked on my notebook and phone once but now it's not working anymore..
  • thotho
    thotho about 9 years
    I am using this now and it is now working
  • thotho
    thotho about 9 years
    not wokring as written here: serverfault.com/questions/680580/…
  • thotho
    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
    Jchieppa about 9 years
    Check 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
    thotho about 9 years
    Well 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
    chovy over 5 years
    i 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.