dsgtew3241 2010-11-27 15:56
浏览 501
已采纳

nginx重定向302而不是重写200

If a user requests any .php file on my webserver I want his request (e.g. he requested mysite.com/testfile.php) to be handled by index.php

In index.php I use $_SERVER['REQUEST_URI'] to see which page the user wants to see (e.g. /testfile.php), so the index.php can generate the appropriate html-code. Notice that testfile.php doesn't need to exist.

My problem is that I always get a redirect 302 to / when I try to request testfile.php (as a result $_SERVER['REQUEST_URI'] is /). But what I want is a 200 OK for testfile.php and that index.php generates the right html-code for this.

This is a snippet from my buggy nginx.conf:

 server {
 [...]

   root /home/test;

   # When I go to mysite.com redirect my to mysite.com/
   location = / {
   index index.php;
   }

   # For any php file request: let index.php handle the request
   location ~ \.(php)$ {
   include fastcgi_params;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_param  SCRIPT_FILENAME  /home/test/index.php;
   }
 }

Anyone can help me out? Thanks.

  • 写回答

1条回答 默认 最新

  • duanjiaonie6097 2010-11-27 16:16
    关注

    If the files never exist you can do this:

    try_files $uri $uri/ /index.php
    

    you can also rewrite the request like this:

    location / {
      rewrite ^.*$ /index.php last;
    }
    

    in both cases you should be able to read the original request with PATH_INFO or REQUEST_URI.

    Have a look at these links for some more suggestions: http://drupal.org/node/110224, http://michaelshadle.com/2010/08/20/front-controller-patterns-and-nginx/, Zend Framework on nginx

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站