简体   繁体   中英

Symfony2, phpbrew, nginx, php 7.1 and File not found

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM