在Nginx上使用PHP通过X-Accel-Redirect服务静态文件

时间:2018-09-28 16:06:34

标签: php nginx backend x-accel-redirect

我的根目录(/ var / www / data)之外有一些文件,并希望通过网址提供这些文件,例如https://example.com/data/?id=123456。为此,我将使用X-Accel-Redirect标头。我的nginx配置如下:

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

现在,我正在尝试像这样在https://example.com/data/index.php上提供文件:

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

我收到404错误,nginx错误日志如下:

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

请帮助我解决此问题!

0 个答案:

没有答案