douzi4724 2014-01-27 19:17
浏览 50
已采纳

Nginx +重写+ php-fpm =混乱

I'm moving from Apache to Nginx. I've got problem with converting Apache rewrite rules into nginx rules. What I'm trying to convert:

RewriteRule ^$ www/controller.php?_url_=index [QSA,L]
RewriteRule ^/+$ www/controller.php?_url_=index [QSA,L]
RewriteRule ^([a-zA-Z0-9_]+)(/([a-zA-Z0-9_/]*))?$ www/controller.php?_url_=$1&_req_=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9/]+)controller.php?(.*)$ www/controller.php?$2 [QSA,L]

What I tried to use:

rewrite ^/$ /www/controller.php?_url_=index break;
rewrite ^/+$ /www/controller.php?_url_=index break;
rewrite ^/([a-zA-Z0-9_]+)(/([a-zA-Z0-9_]*))?$ /www/controller.php?_url_=$1&_req_=$2 break;
rewrite ^/([a-zA-Z0-9/]+)controller.php?(.*)$ /www/controller.php?$2 break;

If I use above rules my browser is downloading php file (server is not executing it) - I guessed it's not being passed to PHP-FPM. Somewhere I found I should replace "break;" with "last;" like:

rewrite ^/$ /www/controller.php?_url_=index last;

After replacing this still I'm downloading php file from http://example.org, but when I visit http://example.org/login I get into infinite loop. I read nginx documentation and different examples (also here at StackOverflow) but sill I can't find correct configuration. Could somebody point me into the right direction?

Here is my whole config file:

server {
listen       80;
server_name  10.10.100.172;
error_log    /var/log/nginx/example.com.error.log debug;
rewrite_log on;


location / {
root   /var/www/webs;
    index  index.php index.html index.htm;
rewrite ^/$ /www/controller.php?_url_=index last;
    rewrite ^/+$ /www/controller.php?_url_=index last;
    rewrite ^/([a-zA-Z0-9_]+)(/([a-zA-Z0-9_]*))?$ /www/controller.php?_url_=$1&_req_=$2 last;
    rewrite ^/([a-zA-Z0-9/]+)controller.php?(.*)$ /www/controller.php?$2 last;
}


error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    root           /var/www/webs;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

}

EDIT:

I moved rules outside location segment and used "break;" at the end of each rule. I can reach /www/controller.php?url=login&req=/ when I go to example.org/login/ - controller.php was responsible for infinite loop. When I try to reach example.org or example.org/ I'm downloading controller.php file - like it's not being passed to the PHP-FPM. Any guess?

  • 写回答

1条回答 默认 最新

  • dongliao1948 2014-01-28 06:50
    关注

    I used above rules outside location segment and it works! I tried viewing my page in different browser and ecerything is fine. I always forget about removing cache..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog