Skip to content

Instantly share code, notes, and snippets.

@jackmcdade
Created August 23, 2016 17:04
Show Gist options
  • Save jackmcdade/a578a29f8acd80d762629e56a22d0dbc to your computer and use it in GitHub Desktop.
Save jackmcdade/a578a29f8acd80d762629e56a22d0dbc to your computer and use it in GitHub Desktop.
server {
listen 80;
server_name example.com;
root /var/www/example.com/public/;
ssl_protocols TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location = / {
try_files /static/index.html /index.php?$query_string;
}
location / {
try_files $uri $uri/ /static/$uri /static/$uri/index.html /static/ $uri /index.php?$query_string;
}
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/example.com-error.log error;
error_page 404 /index.php;
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;
}
}
@jasonmccreary
Copy link

jasonmccreary commented Aug 24, 2016

I thought I had nginx installed locally, but I did not. In addition, Laravel Shift was a distraction today.

Anyway, here are some quick thoughts:

Try changing try_files $uri $uri/ /static/$uri /static/$uri/index.html /static/ $uri /index.php?$query_string; to try_files $uri $uri/ /static/$uri /static/$uri/index.html /index.php?$query_string;. From what I understand, once the home pages is cached /static/ will always be a hit. Furthermore, you check for $uri first, so this is redundant anyway.

Next, I would toy around with your index config and remove the html options. This will remove any confusion about which index page nginx attempt to load and will result in clearer try_files statements as that's the only time you'd use index.html.

Finally, as a hacky alternative, you could ditch try_files and simply utilize error_page 404 /index.php as a sort of fallback resource when files can't directly be loaded from /static/.

If none of that gets you anywhere, let me know and I'll use my rep on StackOverflow to get an answer as you got me curious.

@jackmcdade
Copy link
Author

Tinkering with this. Thanks! Do you know if it's possible to only do these static try_files rules if the $request_method is GET?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment