Serve static files with X-Accel-Redirect with PHP on nginx

2k views Asked by At

i have some files outside my root directory (/var/www/data) and would like to serve them over an URL e.g. https://example.com/data/?id=123456. I would use for this the X-Accel-Redirect header. My nginx configuration is the following:

server {

    server_name example.com;
    server_tokens off;
    root /var/www/html/;

    location / {
        index index.php index.html;
    }
    location ~ \.php(?:$|/) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
        fastcgi_param MOD_X_ACCEL_REDIRECT_PREFIX /data/;
    }

    location /data/ {
        internal;
        alias /var/www/data/;
    }

    listen 443 ssl; 
    ssl_certificate ...; 
    ssl_certificate_key ...;
    ssl_dhparam ...; 
}

Now I am trying to to serve the files on https://example.com/data/index.php like this:

$url = "/var/www/data/file.jpg;
header("X-Accel-Redirect: ".$url);

I get a 404 error and the nginx error log is the following:

[error] 8401#8401: *1 open() "/var/www/html/var/www/data/file.jpg" failed (2: No such file or directory), client: .....

Please help me to solve this issue!

0

There are 0 answers