2

我见过类似这样的其他主题:Nginx 配置导致无限重定向循环,但是我尝试过的所有配置都没有改变。

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

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 提供的默认值。我不确定还能尝试什么。

4

1 回答 1

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 证书和 nginx 文件进行更改时,最安全的选择始终是vagrant reload确保重新加载所有编辑。

于 2015-02-26T18:20:16.173 回答