【问题标题】:Rewrite all requests for non existing files to index.php with try_files with Nginx使用 Nginx 使用 try_files 将所有对不存在文件的请求重写为 index.php
【发布时间】:2014-01-03 07:31:47
【问题描述】:

我正在尝试将琐碎的 htaccess 文件转换为 Nginx,但无法使其工作。它返回 404 错误。 这是htaccess的内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

这是我当前的 nginx 配置:

server {
listen 80;
server_name domain.biz;
root /var/www/domain.biz;
charset utf-8;
autoindex off;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include        fastcgi_params;
fastcgi_pass   unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}

【问题讨论】:

    标签: php .htaccess nginx


    【解决方案1】:
    1. 你直接调用的其他php文件怎么样?例如只有一个

      的 info.php

      phpinfo();

      里面?

      我问这个是因为你的服务器配置文件似乎使用 try_files 恰到好处,但我不确定你是否正确地提供 php 脚本。

    2. ¿你的 fastcgi 池是否在监听那只袜子? ¿ 例如,您确定它没有在端口 9000 中侦听吗?无论如何,我更喜欢在 http 部分定义一个上游,稍后在服务器部分使用它

      http {
      
          upstream phpbackend {
              server unix:/var/run/php5-fpm.sock;
          }
          ...
      
      
          server {
              ...
              location ~ \.php$ {
                  include       fastcgi_params;
                  fastcgi_pass  phpbackend;
                  fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
              }    
          }
      }
      
    3. 您确定您的 php.ini 将 cgi.fix_pathinfo 设置为 false 吗?

    【讨论】:

    • 谢谢,在你的回答之后,我重新启动了 nginx,现在它工作正常 :) 似乎重新加载还不够
    猜你喜欢
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多