2

I have seen other topics like this one: Nginx configuration leads to endless redirect loop, however all of the configurations I have tried with no change.

Problem: I want to force ALL routes to https in my Laravel 4 application but they always result in a redirect loop.

The nginx config is inside a Laravel Homestead environment. Here is the config:

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;
}

I have disabled all routes in my application, my .htaccess is the Laravel provided default. I'm not sure what else to try.

Community
  • 1
  • 1
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123

1 Answers1

1

First off, Laravel Homestead uses 44300 as an alias for 443 on the vagrant box.

Your http to https redirect should also be accomplished simply by:

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
}

Secondly, you seem to be using a dev-site with Homestead, double check that your host-file and homestead.yaml is set up accordingly and everything is on order there.

Also, when making changes to SSL-certs and nginx-files, your safest bet is always to do a vagrant reload to make sure all edits are reloaded.

Marcus Olsson
  • 2,485
  • 19
  • 35