【问题标题】:NGINX force www and https causes too many redirectsNGINX 强制 www 和 https 导致重定向过多
【发布时间】:2018-12-29 15:12:48
【问题描述】:

我正在尝试在我的 nginx 服务器上强制使用 www 和 https。

就像这里建议的其他问题一样,我实现了以下内容。

server_name example.com www.myexample.com
return 301 https://www.example.com$request_uri;

然后在下面我有以下内容是我们加密添加的:

if ($scheme != "https") {
    return 301 "https://www.$host$request_uri";
}

当我访问该站点时,它转到 https://www,example.com,但我收到消息“www.example.com 重定向您的次数过多。”

如果我注释掉 Let's Encrypt 添加的位,我仍然会收到重定向消息。

只有当我注释掉以下内容时它才有效:

return 301 https://www.example.com$request_uri;

有人对如何设置有更好的想法吗?

我已经看到了另一个答案,但 OP 使用的是 cloudflare。我没有使用任何 CDN。

谢谢

这是我的所有信息。我的第一个 return 301 行被注释掉了,因为它导致了太多的重定向: NGINX:版本 1.10.3 Ubuntu:16.04.3 LTS(xenial)

# Default server configuration
#
server {

     listen 80;
          listen [::]:80;

    client_max_body_size 25M;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/example.com/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;
    # return 301 https://www.example.com$request_uri;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    # hide user.ini file
    location ~ ^/\.user\.ini {
    deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }

     location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
         expires 365d;
     }

    location ~*  \.(pdf)$ {
        expires 30d;
    }





    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



    if ($scheme != "https") {
        return 301 https://www.$host$request_uri;
    } # managed by Certbot


}

【问题讨论】:

  • 您好!请记住发布完整的虚拟主机配置,以便我们确定是否存在任何其他问题。此外,说明发行版名称、版本和 nginx 版本总是有帮助的,因为不推荐使用的功能和最佳实践发生了变化。

标签: redirect nginx https


【解决方案1】:

如果您将 certbot 用于 Let's Encrypt,并且它在 Web 服务器配置中插入行,那是因为您有意或无意地避免使用 certonly 选项。

如果您不希望 certbot 修改您的配置文件,请使用它:

certbot certonly --standalone --preferred-challenges http -d example.com

关于您的重定向,您可以删除您的重定向并将其从 Certbot 中保留,因为该重定向正在检查协议是否为 https,如果不是,它将应用重定向。

if ($scheme != "https") {
    return 301 "https://www.$host$request_uri";
}

我会像这样使用它,如果你想确保它被重定向到一个特定的域而不是将它留给标头请求,请替换 $host 变量:

if ($scheme != "https") {
    return 301 "https://www.example.com$request_uri";
}

ps。您应该发布完整的虚拟主机配置,包括 http 和 https 的服务器块,看看是否还有其他问题。

【讨论】:

  • 这就是我让它工作的方式,但它不会重定向到 www
  • 您在 Certbot 的重定向中看到我对 www.$host 的评论了吗?这可能就是 www 不重定向的原因,它假设它不存在并添加它。你可以删除那里的www 或者使用我写的第二个例子。
  • 谢谢 Leo,就是这样
猜你喜欢
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 2014-05-08
  • 2019-06-04
相关资源
最近更新 更多