【问题标题】:NGINX - Redirect location from https to http (redirect loop)NGINX - 将位置从 https 重定向到 http(重定向循环)
【发布时间】:2019-06-16 08:35:51
【问题描述】:

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

P.S 我已经安装了 certbot (Let's Encrypt) 来获取 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/

【问题讨论】:

  • 您的第二个server 块只是将http 重定向回https。在开始重定向到 http://example.com/update/ 之前,您需要决定如何处理它。
  • 不返回结束文件?无论如何,我将第二个服务器块移到了第一个之上,这没有任何区别。对此很新,所以我不完全了解如何对其进行编辑以使其正常工作。

标签: http nginx redirect https


【解决方案1】:

我认为你需要更改这部分。

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

}

【讨论】:

  • 但是它根本不会将任何东西重定向到https?
  • 我编辑了答案,可能会在第二个服务器块处添加重定向到 https 的异常。不确定这是否是这样做的方法,或者它是否正确,也许其他人可以确认。
猜你喜欢
  • 1970-01-01
  • 2019-08-13
  • 2018-01-18
  • 2021-09-15
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多