简体   繁体   English

Nginx重写404

[英]Nginx rewrite 404

I've spent ages looking on google for any information, and asked around some people (before anyone suggests I go and do so). 我花了很长时间在Google上寻找任何信息,并询问了一些人(在有人建议我这样做之前)。

The bits of my nginx.conf that aren't working properly are below. 以下是我的nginx.conf不能正常工作的位。 What's working : rewrite to BlogHome, Home and About. 工作原理:重写为BlogHome,Home和About。

What isn't working - rewrites to C_ReadBlogURL and C_ReadAllPosts . 什么不起作用-重写为C_ReadBlogURL和C_ReadAllPosts。 These both 404 for some reason, even though the paths are correct. 尽管路径正确,但由于某种原因,这两者404。 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. 我认为这可能与它们是php文件有关,但我不知道。

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

}

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 参考: http : //wiki.nginx.org/HttpRewriteModule#break

[ UPDATE ] Based on the nginx config file from the comment below. [ 更新 ]基于以下注释中的nginx配置文件。

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. 请注意,“ break指令不同于” rewrite ... break”;我的主要更改是将2条规则移到php location块中,并将“ last”替换为“ break”,这样就不会触发另一回合了。位置搜索。

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. 我的理解是,您想匹配所有其余的php脚本。 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. 指令在IF块内。 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. [ UPDATE2 ]同样也可以将所有内容都移动到“ location /”块。

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

The () aren't necessary in the first rule, but that is just a detail. ()在第一条规则中不是必需的,但这只是一个细节。

I think that your problem is the "break;", only "last;" 我认为您的问题是“断裂”,只有“最后”; is the correct choice for this case. 是这种情况的正确选择。

I tried this and it works(test1.html and test2.html have different content and I know when is entering in each rule): 我试过了,它有效(test1.html和test2.html具有不同的内容,我知道何时在每个规则中输入):

rewrite ^/posts.php /test1.html?type=$arg_type last;
rewrite ^/(.+)/?$ /test2.html?url=$1 last;

So, for you this should work: 因此,这应该对您有效:

rewrite ^/posts.php /controllers/read/C_ReadAllPosts.php?type=$arg_type last;
rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$1 last;

or according to your last update: 或根据您的最新更新:

rewrite ^/posts\.php /controllers/read/C_ReadAllPosts.php?type=$arg_type last;
rewrite ^/?$ /views/Read/BlogHome.php last;
rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$uri last;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM