nginx redirect HTTPS to HTTP

前端 未结 4 344
情话喂你
情话喂你 2020-12-03 04:28

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.         


        
相关标签:
4条回答
  • 2020-12-03 05:04

    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.

    0 讨论(0)
  • 2020-12-03 05:09

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 05:11

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 05:20
        if ($host = 'foo.com') {
            rewrite  ^/(.*)$  http://www.foo.com$1  permanent;
        }
    
    0 讨论(0)
提交回复
热议问题