NGINX-从https重定向到http(重定向循环)

时间:2019-01-22 20:02:11

标签: http nginx redirect https

尝试将位置从https重定向到http,从而在浏览器中获得重定向循环。

P.S我已经安装了certbot(让我们加密)来获取ssl证书。

我尝试了不同的服务器块,相同的服务器块,返回301,重写但似乎无济于事。

server { 
    root /usr/share/nginx/www;
    index index.html index.htm index.php;

    server_name example.com;

    # Make site accessible from http://localhost/

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location /update {
        return 301 http://example.com$request_uri;
    }

    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot 
}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen   80;
    server_name example.com;
    return 404; # managed by Certbot
}

我想让https://example.com/update/重定向到http://example.com/update/

1 个答案:

答案 0 :(得分:0)

我认为您需要更改此部分。

server {
if ($host = example.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot

listen   80;
server_name example.com;
return 404; # managed by Certbot
}

仅此一个。

server {
listen   80;
server_name example.com;
}

编辑: 因此,如果这样做后根本不将任何内容重定向到https,那么我错了。然后,让我们尝试为/ update路径添加重定向例外,方法是在下面某处的代码中添加如下一行:location / update {}?如果有人可以尝试偷偷溜进去,那就太好了。我不确定这是否是正确的方法。

server {
if ($host = example.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot

listen   80;
server_name example.com;
return 404; # managed by Certbot

}