使用https和nginx进行许多重定向

时间:2017-03-14 13:56:24

标签: laravel ssl nginx https server

我正在使用cloudflare获取免费的SSL证书。现在我想将我的网站重定向到https。我在this blog post找到了配置。我的nginx配置如下所示:

server {
    listen       80;
    server_name  nielsvroman.be www.nielsvroman.be;

    # Force rewrite for everything that reaches this vhost to www.nielsvroman.be
    return 301 https://www.nielsvroman.be$uri;
}

server {
    listen 443 ssl;

    server_name www.nielsvroman.be;

    root /var/www/nielsvroman.be/html/public;

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

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

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

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

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

    location ~ /\.ht {
            deny all;
        }
}

但是当我转到https://www.nielsvroman.be时,我收到错误 ERR_TOO_MANY_REDIRECTS 。我的配置有什么问题?

我在Cloudflare中的DNS设置是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个...你需要重定向两个并使用$ request_uri ...

server {
    listen       80;
    server_name  nielsvroman.be www.nielsvroman.be;

    # Force rewrite for everything that reaches this vhost to www.nielsvroman.be
    return 301 https://$server_name$request_uri;
}