douping5015 2016-05-01 19:07
浏览 70
已采纳

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

If I will add the following line to my nginx configuration it will break my website and will run without 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;
}

if I comment the rewrite condition everything will work fine.

What can I do to bring both things to work, the rewrite condition and the css style sheet?

Edit: I got a new problem, now all files like test.php work fine without writing .php, but folders like users/ won't work, I still reveive File not found, normaly it should take the index.php or index.html from the folder, how can I provide both functions? add .php to files and use inde.php/html from folder?

  • 写回答

2条回答 默认 最新

  • douyan1927 2016-05-01 21:58
    关注

    You can separate the try_files and rewrite by replacing the location / block with the following two location blocks:

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

    That way try_files goes first, and if no file is found there is a rewrite and .php is added to the request, which is then executed by the .php location block which should need no modification.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失