开发者

nginx redirect HTTPS to HTTP

How can i redireect from https to http?

i have the code below but it does not seem to wor开发者_运维百科k.

server {
        listen 443;
        server_name example.com;
        rewrite ^(.*) http://example.com$1 permanent;
 }


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜