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

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

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止