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

nginx CSS在将重写从$添加到$.php后中断,php,nginx,url-rewriting,Php,Nginx,Url Rewriting,如果我将以下行添加到我的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; } locati

如果我将以下行添加到我的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/
这样的文件夹将无法工作,我仍然发现
文件未找到
,通常它应该从文件夹中获取
index.php
index.html
,我如何提供这两个功能?将
.php
添加到文件中,并使用文件夹中的
inde.php/html

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

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

您可以使用以下两个位置块替换
location/
块来分离try\u文件并重写:

location / {
    try_files $uri $uri/ @rewriterules;
}

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

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

谢谢,这对我很有效,但您能解释一下
访问日志
过期
的作用吗?是否有其他文件类型要添加?我添加了
svg
,因为我使用了很多这些文件。@MarioFridrichovsky将
access\u log
设置为
off
可确保不记录这些文件的http访问日志
expires
将web浏览器的HTTP expires标头设置为30天,以鼓励在用户的浏览器上进行静态文件缓存。如果我只添加您的
try_文件
而不添加任何
rewrite
,它将适用于带结尾的文件夹和名称,但是,如果我只键入
test
,我将收到php文件作为下载。我用不同的解决方案编辑了我的答案,以保持
重写
,这应该会像预期的那样起作用。不会起作用,仍然是404,看起来他不属于
@rewriterule
404,到底是什么文件?因为这些位置块要么找到静态文件,要么遵循@rewriterules并尝试添加.php。文件夹
users/
起作用,文件
test.php
也起作用,但是重写到
test
将不起作用。