【问题标题】:Symfony2, phpbrew, nginx, php 7.1 and File not foundSymfony2、phpbrew、nginx、php 7.1 和找不到文件
【发布时间】:2017-01-20 01:42:43
【问题描述】:

我一直在尝试使用phpbrew 升级到 php 7.1,并选择使用 nginx 安装它,因为我到处都读到它比 Apache 更简单(以我的拙见,没那么简单)。

当我尝试使用 nginx 运行 Symfony2 时,我遇到了this doc page,它提供了 nginx 上 Sf2 的基本配置。

我设法将 php-fpm 配置为服务app_dev.php,并且每个以.php 结尾的文件都正确。但是,一旦我转到不同的 URL(例如/home),nginx 配置就会中断,并且在php-fpm 中出现File not found 错误。

如何配置 nginx 虚拟主机以允许重写 app_dev.phpapp.php 之后的所有内容(就像在 apache2 上使用 modrewrite 一样)?

我的nginx文件供参考:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    location /my-app {
        index web/app_dev.php;
        try_files $uri /web/app.php$is_args$args;
    }

    location /dist {
        root /usr/share/nginx/html;
        index depp/index.php;
        try_files $uri /depp/index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param REQUEST_URI $uri?$args;
    }
}

【问题讨论】:

    标签: php symfony nginx phpbrew


    【解决方案1】:

    您缺少一个重写条件来捕获所有传入的请求并将它们转发到您的前端控制器。

    尝试类似:

      # strip app.php/ prefix if it is present
      rewrite ^/app\.php/?(.*)$ /$1 permanent;
    
      location /my-app {
        index app.php;
        try_files $uri @rewriteapp;
      }
    
      location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
      }
    
     # Symfony 2 app index
       location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
         fastcgi_split_path_info ^(.+\.php)(/.*)$;
         include fastcgi_params;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
    
      # deny access to any other php files
       location ~^.*\.php(/|$) {
         deny all;
       }
    

    您当前的配置是任何.php 脚本的更通用配置,但 Symfony2 和框架通常只提供一个包罗万象的前端控制器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-05
      • 2018-02-05
      • 2014-08-04
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多