symfony 2.8 production app.php does not take URL correctly from Nginx

285 Views Asked by At

I have the following Nginx configuration:

server {
    listen 80 default;
    listen [::]:80 default;
    server_name localhost;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate /www/letsencrypt/easyPost/cert.pem;
    ssl_certificate_key /www/letsencrypt/easyPost/privkey.pem;

    ssl     on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    keepalive_timeout   70;

    root /var/www/html/web;

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

    location ~ ^/(widget|app)\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index app.php;
        send_timeout 1800;
        fastcgi_read_timeout 1800;
        #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_pass php:9000;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }
}

If I call https://localhost/ or https://localhost/app.php it works, but I have other Routes defined that I am not able to reach, like https://localhost/app.php/posts or https://localhost/posts. FastCGI is called and Symfony returns a 404 not found. I guess it's an Nginx misconfiguration. Any thoughts? Thanks

Update

Symfony logs:

[2017-09-07 21:47:50] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /posts"" at /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 176 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /posts\" at /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:176, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0):  at /var/www/html/app/cache/prod/appProdProjectContainerUrlMatcher.php:39)"} []

but this is quite weird since I have a Route well defined:

/**
 * @Route(
 *     "/posts.{_format}",
 *     name="posts",
 *     defaults={"_format": "json"},
 *     requirements={
 *         "_format": "json"
 *     }
 * )
 */
public function getPostsAction($_format, Request $request)

root@b86232f28c81:/var/www/html# app/console debug:router --env=prod
 ---------- -------- -------- ------ ------------------ 
  Name       Method   Scheme   Host   Path              
 ---------- -------- -------- ------ ------------------ 
  homepage   ANY      ANY      ANY    /                 
  posts      ANY      ANY      ANY    /posts.{_format}  
 ---------- -------- -------- ------ ------------------ 
0

There are 0 best solutions below