【问题标题】:Nginx rewrite HTTP to HTTPS says redirect loop?Nginx 将 HTTP 重写为 HTTPS 说重定向循环?
【发布时间】:2015-01-08 16:47:04
【问题描述】:

我正在尝试将所有 HTTP 流量重定向到 HTTPS,所以当我访问 www.domain.com 时,它会转到 https://www.domain.com。这是我当前的 .conf 文件 -

server {

listen 80;
#listen [::]:80;
server_name www.domain.net;
rewrite ^(.*) https://www.domain.net$1 permanent;
index index.html index.htm index.php default.html default.htm default.php;
root  /home/wwwroot/www.domain.net;

include other.conf;
#error_page   404   /404.html;
location ~ [^/]\.php(/|$)
    {
        # comment try_files $uri =404; to enable pathinfo
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        #include pathinfo.conf;
    }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

access_log  /home/wwwlogs/www.domain.net.log  access; }

它总是为我的域返回一个“重定向循环”错误,我尝试了许多不同的配置,但我总是遇到同样的问题。 (感谢我的 SSL 没有按应有的方式配置,但它仍然有效)

如果有人可以帮助我使其正常工作,我将不胜感激。

【问题讨论】:

  • 请把已经收录的other.conf和fastcgi.conf的内容贴出来

标签: http redirect ssl nginx https


【解决方案1】:

你把你的文件放在服务器上监听 443 端口吗?

因为如果你不配置你的 nginx 来监听 443 端口,你的服务器会从 80 重定向到 https。从 https 到 http (80) - 这会导致你的循环。

要建立你的重定向,你只需要这个:

server {
    listen 80 default;
    server_name <<your domain>>;
    return 301 https://<<your domain>>$request_uri;}

并使用您在 443 ssl 上所做的相同配置,但进行这些更改:

listen 443 ssl;
ssl_certificate <<your .crt>>;
ssl_certificate_key <<your .key>>;
ssl_client_certificate <<your client certificate .crt>>;

【讨论】:

    【解决方案2】:

    您需要将 ssl 配置放入服务器:

    监听 443 ssl 默认;
    ssl 上的 ssl_certificate >;
    ssl_certificate_key >;

    # 强制 https 重定向
    if ($scheme = http) { 返回 301 https://$server_name$request_uri; }

    【讨论】:

    • 效果非常好。我在这里关注了很多答案,但只有你的作品。
    猜你喜欢
    • 1970-01-01
    • 2019-08-13
    • 2019-06-16
    • 2021-09-15
    • 2018-01-18
    • 2011-03-29
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多