【问题标题】:Laravel/Homestead Nginx SSL Redirect LoopLaravel/Homestead Nginx SSL 重定向循环
【发布时间】:2015-02-15 02:53:09
【问题描述】:

我也看到过类似这样的其他主题:Nginx configuration leads to endless redirect loop,但是我尝试过的所有配置都没有改变。

问题:我想在我的 Laravel 4 应用程序中强制所有路由到 https,但它们总是会导致重定向循环。

nginx 配置位于 Laravel Homestead 环境中。这是配置:

server {
    listen 80;
    server_name dev.subdomain.mysite.com;
    root /home/vagrant/sites/work/dev.subdomain.mysite.com/public;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_redirect   off;
        proxy_max_temp_file_size 0;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/dev.subdomain.mysite.com-error.log error;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

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

server {
    listen 443;
    server_name dev.subdomain.mysite.com;
    root /home/vagrant/sites/work/dev.subdomain.mysite.com/public;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files  / /index.php?;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_redirect   off;
        proxy_max_temp_file_size 0;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/dev.subdomain.mysite.com-error.log error;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }


    ssl on;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
}

我已经禁用了我的应用程序中的所有路由,我的 .htaccess 是 Laravel 提供的默认值。我不确定还能尝试什么。

【问题讨论】:

    标签: redirect ssl laravel nginx homestead


    【解决方案1】:

    首先,Laravel Homestead 在 vagrant box 上使用 44300 作为 443 的别名。

    您的 http 到 https 重定向也应该通过以下方式完成:

    server {
        listen 80;
        server_name mysite.com;
        return 301 https://example.com$request_uri;
    }
    
    server {
        listen 443;
        server_name example.com;
        root /home/vagrant/sites/work/example.com/public;
        [...] // All other Laravel-related stuff
    }
    

    其次,您似乎正在使用 Homestead 的开发站点,请仔细检查您的 host-file 和 homestead.yaml 是否已相应设置,并且那里的一切正常。

    此外,在更改 SSL-certs 和 nginx-files 时,最安全的选择是始终执行vagrant reload 以确保重新加载所有编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 2014-06-01
      • 1970-01-01
      • 2015-04-19
      • 2017-01-09
      • 2017-04-10
      • 2013-11-27
      • 2014-03-09
      相关资源
      最近更新 更多