nginx XSendfile "rewrite or internal redirection cycle …" using nginx docker image

I’m working on a PHP application which runs using a docker-compose stack with a seperate nginx container for the HTTP handling.

I’m trying to serve static files through PHP and NGINX using NGINX’s XSendfile feature.
While this works locally IF i bind my application directory to the NGINX container, it refuses to work if i remove the binding (all other php routes will continue to work)

Is there a way to prevent nginx looking for the index file when using the X-Accel-Redirect?

error.log

2019/12/17 23:26:23 [error] 7#7: *1 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 172.18.0.1, server: _, request: "GET /download/id/3 HTTP/1.1", upstream: "fastcgi://172.18.0.6:9000", host: "localhost"

nginx.conf

server {
    listen 80;
    server_name _;
    root /var/www/app/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location /dl/ {
        internal;
        alias /media/;
    }

    location ~* .(jpg|jpeg|gif|css|png|js|ico|html|eof|woff|ttf)$ {
        if (-f $request_filename) {
            expires 30d;
            access_log off;
        }
    }

    location ~ .php$ {
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

php code (running in a separate container)

<?php

namespace AppController;

use AppEntityFile;
use AppRepositoryFileRepository;
use DoctrineORMEntityManagerInterface;
use PsrLogLoggerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
use SymfonyComponentRoutingAnnotationRoute;

class DownloadController extends AbstractController
{
    /**
     * @var FileRepository
     */
    private $fileRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        EntityManagerInterface $entityManager,
        LoggerInterface $logger
    )
    {
        $this->fileRepository = $entityManager->getRepository(File::class);
        $this->logger = $logger;
    }

    /**
     * @Route("/download/id/{id}", name="download_id")
     */
    public function downloadUsingId(int $id)
    {
        if($file = $this->fileRepository->find($id)) {
            return $this->serveDownloadUsingXSendfile($file);
        }

        throw new NotFoundHttpException();
    }

    private function serveDownloadUsingXSendfile(File $file)
    {
        return new RedirectResponse('/dl_complete', 302, [
            'X-Accel-Redirect'=> '/'.$path,
            'Content-Type' => mime_content_type($file->getPath()),
            'Content-Disposition' => $file->getFilename(),
            'Content-Length' => $file->getFilesize()
        ]);
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *