doue2666 2013-03-31 22:12
浏览 166

Nginx重写404

I've spent ages looking on google for any information, and asked around some people (before anyone suggests I go and do so).

The bits of my nginx.conf that aren't working properly are below. What's working : rewrite to BlogHome, Home and About.

What isn't working - rewrites to C_ReadBlogURL and C_ReadAllPosts . These both 404 for some reason, even though the paths are correct. I don't understand why - and I've been puzzling over this all day. I think it may have something to do with them being php files, but I've no idea.

Any help would be greatly appreciated :)

server {
listen   80;


server_name blog.example.com;

root /usr/share/nginx/www/example;
index /views/Read/BlogHome.php;


location / {
    rewrite ^/?$ /views/Read/BlogHome.php last; break;
    rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$1 last; break;
}
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

server {
listen 80;
server_name example.com;

root /usr/share/nginx/www/example;
index /controllers/read/C_ReadLatestPost.php;

location ~ ^(/posts\.php) {
    rewrite ^(/posts\.php)  /controllers/read/C_ReadAllPosts.php?type=$arg_type last; break;
}

location ~ ^/?$ {
    rewrite ^/?$ /controllers/read/C_ReadLatestPost.php last; break;


}

location ~ ^(/about)/?$ {
    rewrite ^(/about)/?$ /views/Read/About.php last; break;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

}
  • 写回答

2条回答 默认 最新

  • dougou6727 2013-03-31 23:54
    关注

    Remove every "break;" in each of your rewrite rules. It doesn't belong here. Any rewrite rules after the first "break;" directive will be ignored. Don't think that's what you want.

    reference: http://wiki.nginx.org/HttpRewriteModule#break

    [UPDATE] Based on the nginx config file from the comment below.

    Note that "break directive is different from "rewrite...break;" My major change is to move the 2 rules into the php location block, and replace 'last' by 'break' such that it won't trigger another round to location search.

    Your 2nd rewrite rule is wrong (using "[]" is different from "()" in regex). My understanding is that you want to match all the remaining php scripts. So I changed that rule.

    I also remove another appearance of "break;" from "location /" block. You may only want to put a 'break;' directive inside an IF block. Other than that, I don't see any practical usage of that directive.

    [UPDATE2] Also it makes sense to move everything to the "location /" block as well.

    server {
        listen 80;
        server_name blog.example.com;
        root /usr/share/nginx/www/example;
        index /views/Read/BlogHome.php;
    
        location / {
            rewrite ^(/posts\.php) /controllers/read/C_ReadAllPosts.php?type=$arg_type break;
            rewrite ^/?$ /views/Read/BlogHome.php break;
            rewrite ^ /controllers/read/C_ReadBlogURL.php?url=$uri break;
    
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100