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..

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮