title: Serve static files with X-Accel-Redirect with PHP on nginx
body: <p>i have some files outside my root directory (/var/www/data) and would like to serve them over an URL e.g. <a href="https://example.com/data/?id=123456" rel="nofollow noreferrer">https://example.com/data/?id=123456</a>. I would use for this the X-Accel-Redirect header. My nginx configuration is the following: </p>
  
  <pre><code>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 ...; 
  }
  </code></pre>
  
  <p>Now I am trying to to serve the files on <a href="https://example.com/data/index.php" rel="nofollow noreferrer">https://example.com/data/index.php</a> like this:</p>
  
  <pre><code>$url = "/var/www/data/file.jpg;
  header("X-Accel-Redirect: ".$url);
  </code></pre>
  
  <p>I get a 404 error and the nginx error log is the following:</p>
  
  <pre><code>[error] 8401#8401: *1 open() "/var/www/html/var/www/data/file.jpg" failed (2: No such file or directory), client: .....
  </code></pre>
  
  <p>Please help me to solve this issue!</p>