Nginx使用404状态代码提供静态文件吗? [英] Nginx serving static files with 404 status code?

查看:207
本文介绍了Nginx使用404状态代码提供静态文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让Nginx使用PHP为我的经过身份验证的管理员用户提供一些静态文件.效果很好,我取回了文件.但是它带有404状态代码.

I'm trying to let Nginx serve my authenticated admin users some static files with PHP. This works great, I get back the file. But it's served with a 404 status code..

我正在使用以下(Symfony/Silex)php代码:

I'm using the following (Symfony / Silex) php code:

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

$filePath = '/usr/share/nginx/project/src/path/to/my/protected/static/dir/' . $file;
if (empty($file) || !is_readable($filePath)) {
    $app->abort(404);
}

$response = new BinaryFileResponse($filePath);
$response->trustXSendfileTypeHeader();
$response->setPrivate();
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_INLINE,
    $file,
    iconv('UTF-8', 'ASCII//TRANSLIT', $file)
);
$response->headers->addCacheControlDirective('must-revalidate', true);
return $response;

这是我的Nginx配置:

And here's my nginx config:

server {
    listen          443;
    listen          [::]:443;

    root            /usr/share/nginx/project/web;
    index           index.php;
    error_page      401 403 404 /404.html;

    server_name     example.com;

    rewrite ^/(.*)/$ /$1 permanent;

    location / {
        # First attempt to serve request as file, then
        # as directory, then let php handle the file
        try_files $uri $uri/ /index.php$is_args$args;
        index index.php;
        autoindex off;

        location ~*  \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
            expires 150d;
        }
    }

    location ~ \.php$ {
        set $path_info $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        try_files $uri $uri/ /index.php$is_args$args;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

        include fastcgi_params;
        fastcgi_param APP_ENV production;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

受保护的目录位于Nginx配置(/usr/share/nginx/project/web)中的根目录之外.

The protected dir lies outside of the root in the nginx config (/usr/share/nginx/project/web).

我在日志中发现了这类错误消息:

I've found these kind of error messages in the logs:

open() "/usr/share/nginx/project/web/admin/static/admin.js" failed 
(2: No such file or directory), 
request: "GET /admin/static/admin.js HTTP/1.1"

/admin/static/admin.js确实是请求的网址.

更新1

nginx似乎总是尝试打开url并在错误日志中添加一个条目,即使php能够很好地处理响应.

It looks like nginx always tries to open the url and adds an entry to the error log even php handles the response just fine.

即使我只替换了所有的php代码:return new Response('test', 200);正文"test"的响应代码仍然是404 ...

Even if I replace all the php code for just: return new Response('test', 200); the response code for the body 'test' is still 404...

我还尝试在我的nginx配置中添加一个额外的位置块:

I also tried adding an extra location block in my nginx config:

location /protected_admin_files {
    internal;
    alias /usr/share/nginx/project/src/path/to/my/protected/static/dir;
}

然后尝试像这样重定向到文件:

And then try to redirect to the file like this:

return new Response('', 200, [
    'X-Accel-Redirect' => '/protected_admin_files/' . $file
]);

但也没有运气.相同的404和正确的响应正文...

But also without luck. Same 404 with the right response body...

推荐答案

我自己找到了原因.

将错误级别设置为debug(

显然,在我的第一个location /块中嵌套的location是导致404的原因.

Apparently, the nested location in my first first location / block was causing the 404.

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

    location ~*  \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
        expires 150d;
    }
}

由于扩展名而匹配,使Nginx可以查找文件.由于找不到它,因此404被设置,并且显然不会在以后的过程中被覆盖,因为php返回X-Accel-Redirect标头:(.

It got matched because of the extension, making Nginx go looking for the file. Since it's then not found, the 404 is set and apparently not overwritten later in the process when php returns the X-Accel-Redirect header :(.

这篇关于Nginx使用404状态代码提供静态文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆