Nginx 配置

Nginx 配置

我已经查看了大约 5 个问题和博客文章,并尝试了他们告诉我的所有方法,但都没有成功。我可以浏览 www.example.com/phpinfo.php,但不能浏览 www.example.com/index.php。我甚至将 phpinfo.php 设为 index.php 的符号链接,phpinfo.php 仍然有效,而 index.php 无效。

Nginx 配置

[admin@web01 www]$ cat /etc/nginx/conf.d/example.com.conf 
server {
    listen       80;
    server_name  www.example.com;

    # Document root
    index  index.php;
    root   /var/www/sites/example.com/www;

    # Pass PHP scripts to FastCGI server
    location ~* \.php$ {
        include       fastcgi.conf;
        try_files     $uri =404;
        fastcgi_pass  127.0.0.1:9000;
    }
}

[admin@web01 www]$ cat /etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

权限

[admin@web01 www]$ ls -al /var/www/sites/example.com/www/*.php
-rw-rw-r--. 1 admin admin 17 Jan 24 10:37 /var/www/sites/example.com/www/index.php
lrwxrwxrwx. 1 admin admin  9 Jan 24 10:37 /var/www/sites/example.com/www/phpinfo.php -> index.php

[admin@web01 www]$ ls -alZ /var/www/sites/example.com/www/*.php
-rw-rw-r--. admin admin unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/sites/example.com/www/index.php
lrwxrwxrwx. admin admin unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/sites/example.com/www/phpinfo.php -> index.php

[admin@web01 ~]# ls -al /var
drwxr-xr-x. 19 root root  4096 Dec 15 18:02 .
dr-xr-xr-x. 23 root root  4096 Dec 15 17:48 ..
drwxr-xr-x.  7 root root  4096 Dec 15 18:13 www

[admin@web01 ~]# ls -al /var/www
drwxr-xr-x.  7 root  root  4096 Dec 15 18:13 .
drwxr-xr-x. 19 root  root  4096 Dec 15 18:02 ..
drwxr-xr-x.  7 admin admin 4096 Jan 23 11:35 sites

[admin@web01 ~]# ls -al /var/www/sites
drwxr-xr-x. 7 admin admin 4096 Jan 23 11:35 .
drwxr-xr-x. 7 root  root  4096 Dec 15 18:13 ..
drwxrwxr-x. 4 admin admin 4096 Jan 23 11:21 example.com

[admin@web01 ~]# ls -al /var/www/sites/example.com
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 .
drwxr-xr-x.  7 admin admin 4096 Jan 23 11:35 ..
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 html
drwxrwxr-x.  2 admin admin 4096 Jan 27 09:00 logs
lrwxrwxrwx.  1 admin admin   11 Jan 23 11:21 www -> html/public

[admin@web01 ~]# ls -al /var/www/sites/example.com/html
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 .
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 ..
drwxrwxr-x. 11 admin admin 4096 Jan 23 11:21 application
drwxrwxr-x.  8 admin admin 4096 Jan 23 11:21 data
drwxrwxr-x.  3 admin admin 4096 Jan 23 11:21 docs
drwxrwxr-x.  3 admin admin 4096 Jan 23 11:22 library
drwxrwxr-x.  6 admin admin 4096 Jan 27 15:57 public
drwxrwxr-x.  3 admin admin 4096 Jan 24 09:33 scripts
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 tests

[admin@web01 ~]# ls -al /var/www/sites/example.com/html/public
drwxrwxr-x.  6 admin admin 4096 Jan 27 15:57 .
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 ..
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 assets
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 css
-rw-rw-r--.  1 admin admin   17 Jan 24 10:37 index.php
drwxrwxr-x.  4 admin admin 4096 Jan 24 09:33 js
lrwxrwxrwx.  1 admin admin    9 Jan 24 10:37 phpinfo.php -> index.php

错误日志

2014/01/24 10:37:52 [error] 8286#0: *15 FastCGI sent in stderr:
"Unable to open primary script: /var/www/sites/example.com/www/index.php
(No such file or directory)" while reading response header from upstream,
client: XXX, server: www.example.com, request: "GET /index.php HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "www.example.com"

更新

  1. 更新了我的配置以包含所有内容
  2. 增加了 /、/var、/var/www 等的权限
  3. 完全关闭 SELinux 没有任何效果

更新 #2

从行中删除该try_fileslocation ... .php$并编辑 php.ini 的cgi.fix_pathinfo=0。意识到我整个过程中都没有重新启动 php-fpm。神奇地开始工作了!如果您可以解释为什么 phpinfo.php 始终运行而 index.php 却不运行(尽管它们位于同一个目录中,并且 phpinfo.php 是 index.php 的符号链接!!),您将获得赏金。

答案1

尝试将其添加到您的 nginx.conf:

fastcgi_index index.php;

例如,nginx.conf 文件的相关部分将如下所示:

# Pass PHP scripts to FastCGI server
location ~ \.php$ {
    fastcgi_index index.php;
    include       fastcgi.conf;
    try_files     $uri =404;
    fastcgi_pass  127.0.0.1:9000;
}

这样做的目的是告诉 fastcgi 如果未指定文件则使用 index.php。为了便于记录,我使用的是修补后的 nginx 1.2.9(我将 ~* 更改为 ~,因为在我的 nginx.conf 中它就是这样的)

答案2

在 nginx 服务器块配置中你的 root 是 root /var/www/sites/example.com/www

文件路径是/var/www/sites/example.com/html/public

我知道您的 phpinfo.php 可以工作,但请尝试使路径相同。

此外,如果 phpinfo 有效,请将 phpinfo.php 重命名为 index.php 并查看它是否有效。

相关内容