dongzhizhai4070 2017-01-19 15:38 采纳率: 0%
浏览 68

Symfony2,phpbrew,nginx,php 7.1和文件未找到

I've been attempting to upgrade to php 7.1 using phpbrew, and elected to install it with nginx, as I read everywhere that it was simpler than Apache (not that simple, in my humble opinion).

As I was trying to run Symfony2 with nginx, I came across this doc page, which gives a basic config for Sf2 on nginx.

I managed to configure php-fpm to serve app_dev.php, and every file ending in .php correctly. However, as soon as I go to a different URL (/home for instance), the nginx config breaks and I get a File not found error in php-fpm.

How do I configure the nginx virtual host to allow for everything after app_dev.php or app.php to be rewritten (as it would with modrewrite on apache2)?

My nginx file for reference:

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;
    }
}
  • 写回答

1条回答 默认 最新

  • drj26159 2017-01-19 17:42
    关注

    You are missing a rewrite condition to catch-all the incoming requests and forward them to your front controller.

    Try something like:

      # 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;
       }
    

    You current configuration is a more general configuration for any .php script but Symfony2 and framework in general only provide a catch-all front-controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64