Nginx: expires header becomes 404
I'm having problems with the expires header. When I set the expires and access the file through the browser it becomes Not Found 404.
Here is my virtual server setup on nginx.conf
server {
listen 80;
server_name blgourl.com www.blogourl.com
location / {
root /data/file/static/blogourl;
index index.html index.htm index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~* \.php$ {
ssi on;
root /data/file/static;
fastcgi_param HTTP_USER_AGENT $http_user_agent;
fastcgi_index index.php;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /data/file/static/blogourl$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
Solution 1:
As Alexey Ten said, root inside location is bad because you must define root
parameter inside every location directive. And if forget to add the root
in one of your location, nginx will set root
to default /etc/nginx/html
Instead you must define root outside your location, e.g. in server directive. You can always add root
in your location whenever your root directory changed. For example in your config, you can override root
parameter inside location ~* \.php$
See also this discussion regarding root inside location issue.
Your config should be
root /data/file/static/blogourl;
location / {
index index.html index.htm index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~* \.php$ {
ssi on;
root /data/file/static;
fastcgi_param HTTP_USER_AGENT $http_user_agent;
fastcgi_index index.php;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /data/file/static/blogourl$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}