【问题标题】:Nginx in php: php files under in subdirectory thrown 404 errorphp中的Nginx:子目录下的php文件抛出404错误
【发布时间】:2017-08-07 10:45:27
【问题描述】:

我的 nginx 服务器出现问题,子文件夹下的 php 文件抛出 404 错误,但 html 文件工作正常。

文件夹结构

html<folder>
  index.php

  test<folder>
         test.php //not working
         test.html //works fine

Nginx 配置

server {
    listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /var/www/html;

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

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


}ml

当访问 &lt;ip-address&gt;/test/test.html 工作正常时

访问&lt;ip-address&gt;/test/test.php时出现404错误。

当访问&lt;ip-address&gt; 工作正常时(指向html/index.php 文件)。

我已经在 html 文件夹中添加了权限,也在 php.ini 文件中启用了cgi.fix_pathinfo=0

【问题讨论】:

标签: php nginx amazon-ec2


【解决方案1】:
server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    autoindex on;

    location / {
    index index.php index.html;
    }

    location ~\.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    try_files $uri $uri/ /index.php;
    }
}

【讨论】:

  • 这是来自error.log "/var/www/html/50x.html" failed (2: No such file or directory)的错误
  • 你能告诉我你访问的是php文件还是html文件?
  • 我正在访问 php 文件
  • try_files $uri $uri/ =404;index index.php index.html;在 php 块中添加这一行
  • 进行此更改后需要重新启动 nginx。你能确认你已经这样做了吗?
猜你喜欢
  • 2019-07-13
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 2021-11-27
  • 2010-09-28
  • 2018-04-29
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多