36

How can i redireect from https to http?

i have the code below but it does not seem to work.

server {
        listen 443;
        server_name example.com;
        rewrite ^(.*) http://example.com$1 permanent;
 }
1
  • 2
    not without a SSL certificate. You need a server block with ssl certificate configuration and a redirect to the other non-ssl 80 server block. without a certificate HTTPS is not connectable, and a server that listens on 443 without ssl configurations is actually http://domain:443 not the https://domain/
    – Unicornist
    Commented Jan 10, 2020 at 21:30

4 Answers 4

28

The answer above will work, you need to generate a self signed cert (or have a real one) and configure nginx as such:

server {
  listen *:443;
  ssl on;
  server_name domain.com;
  rewrite ^(.*) http://domain.com$1 permanent;

  ssl_certificate      /data/certs/domain.crt;
  ssl_certificate_key  /data/certs/domain.key; 
 }

Keep in mind, if it is a self signed cert the browser will give you an ugly warning.

5
  • 2
    Is it not possible to have this redirect without the ugly warning, without buying a certificate we won't use? Thanks!
    – dgilperez
    Commented Sep 27, 2012 at 11:05
  • You can get a free SSL cert from startssl, that'd get rid of the warnings. self-signed certs will always throw the warnings you are talking about. Commented Oct 25, 2012 at 2:56
  • 5
    You can also replace rewrite ^(.*) http://domain.com$1 permanent; with rewrite ^(.*) http://$host$1 permanent; if you have multiple server names specified. Commented Aug 17, 2014 at 23:24
  • 5
    What is the difference between listen *:443, listen 443 and listen 443 ssl?
    – Michael
    Commented Sep 10, 2015 at 7:09
  • 1
    You can also get a free certificate from Let's Encrypt. I know I got late to the party...
    – whoan
    Commented Oct 26, 2017 at 2:59
16

Building off jberger's comment a configuration that should work would be:

server {
    listen *:80;
    server_name example.com;
}

server {
    listen              *:443 ssl;
    server_name         example.com;
    ssl_certificate     /etc/ssl/certs/example.com.cert;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    return 301 http://$server_name$request_uri;
}
1
  • 1
    Best yet, except the question is asking for https -> http . Change the https to http and move the resulting return 301 http://$server_name$request_uri; to the 443 server block.
    – here
    Commented Jan 8, 2014 at 1:09
1
    if ($host = 'foo.com') {
        rewrite  ^/(.*)$  http://www.foo.com$1  permanent;
    }
1
  • 7
    nginx Pitfalls and IfIsEvil. This code should probably be refactored into two separte server blocks, one that catches http and another that catches https then does return 301 http://$server_name$request_uri;. Commented Oct 29, 2013 at 2:01
-5

You need to create 2 separate server blocks:

  1. Port 443 (HTTPS) - Define everything like PHP, 404, home, root etc in this block. Even if you want to redirect https://www.example.com to https://example.com or vice-versa, do it over here as @coulix has done.

  2. Port 80 (HTTP) - In here you will just use:

server {
    listen    80;
    listen    [::]:80;
    server_name    example.com www.example.com;

    # Redirect HTTP to HTTPS
    return    301    https://example.com$request_uri;
}
1
  • 1
    the OP is asking to redirect HTTPS to HTTP not the other way around.
    – Unicornist
    Commented Jan 10, 2020 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.