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.

    评论

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题