I’m looking through several documents but for some reason I can’t wrap my head around the configuration necessary for what I’m trying to do. Basically I want:
-
All directory requests should keep their url but be processed by index.php
-
All .js, .css, .jpeg and .png files in their respective directories (/js/, /css/, and /images/) should be provided without php processing.
-
All other file access should be denied (including all .php).
I’ve tried setting up locations for the .php processing but I can never figure out how to get it working correctly. I’m going to keep at it but was hoping for some quick help as well 🙂
Thanks!
Edit 2: I think I came up with something decent. It may not be perfect but it should work.
server {
listen 80;
server_name _;
server_tokens off;
root /mypath/www;
access_log /mypath/access.log;
error_log /mypath/error.log;
index index.html;
location ^~ /app {
return 404;
}
location ~ \.conf {
return 404;
}
location ~ \.php {
return 404 ;
}
location ~ /images/.*.(jpg|jpeg|png|gif) {
try_files $uri $uri = 404;
expires max;
}
location ~ /css/.*.css {
try_files $uri $uri = 404;
expires 1d;
}
location ~ /js/.*.js {
try_files $uri $uri = 404;
expires 1d;
}
location / {
try_files $uri @PHPProxy;
error_page 500 501 502 503 504 505 $document_root/error.html;
}
location @PHPProxy {
include fastcgi_params;
fastcgi_pass unix:/tmp/php.socket;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
This was kind of fun to think about. I’ve previously just made sure to block directories or make sure that I don’t have any files I don’t want being accessed in my doc root.
I think I found a way to do it. It doesn’t actually give a 404 or 403 for other files, it just rewrites everything except the specified directories to /index.php.
Here is the config I tested with:
So anything in /images, /css, or /js will just load, but everything else gets rewritten to the index file.
Just keep in mind that any files not in those 3 folders will be totally overlooked.