Nginx + php5-fpm + Symfony2 = “未找到文件。”

Nginx + php5-fpm + Symfony2 = “未找到文件。”

这是基于的站点配置官方 Symfony2 文档

server {
    listen 80;

    server_name     project.local;
    root            /media/Storage/project/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

server {
    listen 443;

    server_name     project.local;
    root            /media/Storage/project/web;

    ssl on;
    ssl_certificate     /etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /etc/nginx/ssl/localhost.key;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

起初,我遇到了No input file specified.错误,但是我读了nginx 维基百科对于这个配置我不需要cgi.fix_pathinfo=0(实际上,他们说 Symfony2 需要它cgi.fix_pathinfo=1)。

所以我把它改回默认值(1),现在我得到了File not found.

我见过其他问题他们建议使用location ~ \.php$ {,但它们并不适用于 Symfony2 应用程序。

如果可能的话,我愿意坚持使用 Symfony2 官方配置。

为什么会发生这种情况?解决方案是什么?

编辑1:

我已将其改cgi.fix_pathinfo0并将配置更改为:

server {
    listen 80;

    server_name     project.local;
    root            /media/Storage/project/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ "^(.+\.php)($|/)" {
        fastcgi_split_path_info ^(.+\.php)(.*)$;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

server {
    listen 443 ssl;

    server_name     project.local;
    root            /media/Storage/project/web;

    ssl_certificate     /etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /etc/nginx/ssl/localhost.key;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    location ~ "^(.+\.php)($|/)" {
        fastcgi_split_path_info ^(.+\.php)(.*)$; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

然后按照 IRC 上的建议重新启动 nginx 和 php5-fpm,但是却出现了著名的Input file not specified错误。

编辑2:这是错误日志

2014/06/25 22:11:45 [错误] 11922#0:*3 FastCGI 在 stderr 中发送:“无法打开主脚本:/media/Storage/project/web/app_dev.php(没有此文件或目录)”从上游读取响应标头时,客户端:127.0.0.1,服务器:project.local,请求:“GET /app_dev.php HTTP/1.1”,上游:“fastcgi://unix:/var/run/php5-fpm.sock:”,主机:“project.local”

编辑3:这是 access.log

127.0.0.1 - - [25/Jun/2014:22:11:45 +0200] "GET /app_dev.php HTTP/1.1" 404 56 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,如 Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36"

symfony2 日志文件是空的。我认为 symfony 从未被执行过。

编辑4:我已经改变了我的配置这个 SO 答案到:

server {
    listen 80;

    server_name     project.local;
    root            /media/Storage/project/web;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location / {
        index app.php;
        if (-f $request_filename) {
          break;
        }
        rewrite ^(.*)$ /app.php last;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ (app|app_dev).php {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param HTTPS off;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

server {
    listen 443 ssl;

    server_name     project.local;
    root            /media/Storage/project/web;

    ssl_certificate     /etc/nginx/ssl/localhost.crt;
    ssl_certificate_key /etc/nginx/ssl/localhost.key;

    error_log /var/log/nginx/ssl_error.log;
    access_log /var/log/nginx/ssl_access.log;

    location / {
        index app.php;
        if (-f $request_filename) {
            break;
        }
        rewrite ^(.*)$ /app.php last;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ (app|app_dev).php {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param HTTPS on;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

现在我收到Access denied.错误。耶,有些事情变了 :)。嘘,还不能用 :(。

编辑5:我已经chown loostro:www-data /media/Storage/project -R,然后chmod g+s /media/Storage/project -R,仍然得到Access denied

编辑6/var/run/php5-fpm.sock权限包括:

srw-rw---- 1 www-data www-data 0 cze 26 11:03 php5-fpm.sock

/etc/php5/fpm/pool.d/www.conf文件是默认文件(适用于 Ubuntu 14.04 发行版),除了我按照教程取消注释的这些更改之外:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

答案1

概括:我的问题是文件权限错误。官方的 symfony2nginx 配置没问题。我将文件移动到 NTFS 存储驱动器,但移动过程中文件权限似乎发生了更改。

  • chown loostro:www-data -R /media/Storage/project- 将所有者更改为 loostro,将组更改为 www-data(loostro 是我开发应用程序的用户,php5-fpm 以 www-data 身份运行),递归
  • chmod 755 -R /media/Storage/project- 递归地将项目的文件权限更改为 rwx(所有者)rx(组,其他)
  • cd /media/Storage/project进入我的项目目录
  • chmod 775 -R app/cache app/logs web/uploads private/uploads- 递归更改可上传目录的 rwx(所有者、组)和 rx(其他)的文件权限
  • chmod g+s -R /media/Storage/project- 递归地保留目录中新创建文件的用户和组以及文件权限

相关内容