nginx redirect HTTPS to HTTP

2019-01-11 10:03发布

问题:

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:

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.



回答2:

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


回答3:

    if ($host = 'foo.com') {
        rewrite  ^/(.*)$  http://www.foo.com$1  permanent;
    }