nginx redirect HTTPS to HTTP

Viewed 63832

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;
 }
4 Answers

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;
}
Related