Nginx:expires标题变为404

我遇到了expires标题的问题。 当我设置到期并通过浏览器访问文件时,它变成Not Found 404。

这是我在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; } } 

正如Alexey Ten所说,根内部位置是不好的,因为您必须在每个位置指令内定义root参数。 如果忘记将root添加到你的位置,nginx会将root设置为默认的 /etc/nginx/html

相反,您必须在您的位置以外定义根 ,例如在服务器指令中。 只要根目录已更改,您就可以随时在您的位置添加根目录。 例如,在您的configuration中,您可以覆盖location ~* \.php$ root参数

另请参阅关于根内部位置问题的讨论 。

你的configuration应该是

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