nginx CSS在添加了从$到$.php的重写后中断


nginx CSS break after adding rewrite from $ to $.php

如果我将以下行添加到我的nginx配置中,它将破坏我的网站,并且将在没有CSS的情况下运行:

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|svg|xml)$ {
  access_log        off;
  expires           30d;
}
location / {
    try_files $uri $uri/ $uri.php =404;
    rewrite ^/(.+)$ /$1.php last;
}
location ~ '.php$ {
    fastcgi_split_path_info ^(.+'.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

如果我评论重写条件,一切都会好起来的。

我能做些什么来让重写条件和css样式表这两件事都起作用?

编辑:我遇到了一个新问题,现在所有像test.php这样的文件在不写.php的情况下都能正常工作,但像users/这样的文件夹不能工作,我仍然显示File not found,通常应该从文件夹中提取index.phpindex.html,我如何提供这两种功能?是否将.php添加到文件并使用文件夹中的inde.php/html

您可以通过用以下两个位置块替换location /块来分离try_files并重写:

location / {
    try_files $uri $uri/ @rewriterules;
}
location @rewriterules { 
    rewrite ^/(.+)$ /$1.php last;
}

这样一来,try_files首先进行,如果没有找到文件,则会进行重写,并将.php添加到请求中,然后由不需要修改的.php位置块执行。

将以下块添加到配置中以处理静态文件。

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
  access_log        off;
  expires           30d;
  root /path/to/public/root;
}