In your location
block there is nowhere place when you pass request to php web app so I can suppose that you do it somewhere else, for example in location
block like this one:
location /index.php {
# your code
}
With your configuration when user request for a static file which exists then first if
directive isn't calculated and everything goes well. Problems starts when user request for dynamic files then nginx enter your first if
block:
if (!-e $request_filename) {
rewrite . /index.php last;
break;
}
And here what happend? You are using last
flag with rewrite
directive and what nginx's doc says about this?
last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
According to this specification when file is dynamical you made a rewrite to index.php
and execution leaves if
block and even whole location
block and following if
block for checking content-type
aren't even checked. I suppose it find then location
for url /index.php
and there you don't set expires max
.
Do you understand this expalantion of your problem?
Resolution to this is too move/copy your sequential if
block for checking content-type
to place where your config pass execution to php web app (index.php) ... or remove last
flag from rewrite
directive if it doesn't make any other troubles.
Okey, so as I promised a little fix to your conf file: change your location
block with this two:
location /index.php {
if ($upstream_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)") {
expires max;
}
if ($sent_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)") {
expires max;
}
}
location / {
if ($upstream_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)") {
expires max;
}
if ($sent_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)") {
expires max;
}
try_files $uri /index.php =404;
}
First location
block is for your index.php
and dynamic response while second one is for static files. In second one we add header expires max
as upstream header and as standard header (just to be sure). I use here one if
block for all types you defined in your configuration with regex pattern match. At the end we use try_files
directive which means if it is possible to get static file based on url it will be gained and in other way try url /index.php or just return with http 404. First location block is only for url /index.php
.
I found nowhere in your config root
directive which should point to root folder of your application. Try to add this also (root doc).
I hope this will resolve your problems.
index.php
. – Jonathan Jan 31 '13 at 17:04root
directive so nginx doesn't even know where to start searching for any files. Next you are usingindex.php
but nowhere is anyproxy_pass
orfastcgi_pass
directives to redirect nginx request to some PHP application. Nginx by his own doesn't understand how to process.php
files. Please check something of this two: kbeezie.com/nginx-configuration-examples or wiki.nginx.org/FullExample – Michał Kupisiński Feb 2 '13 at 9:59