Nginx 返回更改 http 响应头内容类型从 application/pdf 到 text/html

Nginx 返回更改 http 响应头内容类型从 application/pdf 到 text/html

我想从这个 URL 重定向:

localhost:80/files/1.pdf

到这个网址:

localhost:80/viewer.html?pdf=/files/1.pdf

我正在使用 nginx return 来执行此操作:

location ~* /files/(.+\.pdf)$ {
    return $scheme://$host:$server_port/viewer.html?pdf=/files/$1;
}

viewer.html 使用 PDF.js 并尝试打开 urls 参数中给出的 pdf,并记录其页数:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js">
    </script>
</head>
<body>
<script>
    const queryString = window.location.search; // has the params of the url
    const urlParams = new URLSearchParams(queryString); //parses the params
    const filePath = urlParams.get('pdf');
    var loadpdf = pdfjsLib.getDocument(filePath);

    loadpdf.promise.then(function(document) {
        console.log(document._pdfInfo.numPages);
    }, function (reason) { // PDF loading error
        console.error(reason);
    });
</script>
</body>
</html>

当发生重定向时,pdf 的 HTTP GET 响应标头具有 content-type: text/html 而不是 application/pdf,并且无法再读取 pdf。

nginx 返回有问题吗,有人可以指出是什么吗?

答案1

您缺少 viewer.html 页面的重要部分,并且如果请求来自查看器,nginx 配置应阻止重定向。 尝试以下操作:

nginx配置:

    location ~* ^/.*\.pdf$ {
            if ( $query_string !~ "noredir" ) {
                    rewrite /(.*\.pdf)$ viewer.html?pdf=$1 redirect;
            }
            try_files $uri =404;
    }

viewer.html(将?noredir添加到文件路径):

<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js">
    </script>
</head>
<body>

<canvas id="the-canvas"></canvas>

<script>
    const queryString = window.location.search; // has the params of the url
    const urlParams = new URLSearchParams(queryString); //parses the params
    const filePath = urlParams.get('pdf') + "?noredir";
    var loadpdf = pdfjsLib.getDocument(filePath);
    console.log(filePath);

    loadpdf.promise.then(function(pdf) {
          console.log('PDF loaded');

          // Fetch the first page
          var pageNumber = 1;
          pdf.getPage(pageNumber).then(function(page) {
            console.log('Page loaded');

            var scale = 1.5;
            var viewport = page.getViewport({scale: scale});

            // Prepare canvas using PDF page dimensions
            var canvas = document.getElementById('the-canvas');
            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
              canvasContext: context,
              viewport: viewport
            };
            var renderTask = page.render(renderContext);
            renderTask.promise.then(function () {
              console.log('Page rendered');
            });
          });
        }, function (reason) {
          // PDF loading error
          console.error(reason);
        });
</script>
</body>
</html>

这是基于github 上的 pdf.js 示例

答案2

问题是当我尝试获取 pdf 本身时也发生了重定向!

所以当我们想要获取pdf时我在url中添加了一个参数:

var loadpdf = pdfjsLib.getDocument(filePath+ "?getFile=true");

并且将 nginx.conf 更改为仅当 url 上没有其他参数时才重定向:

location ~* /files/(.+\.pdf)$ {
        if ($is_args = "") { # do the redirect only if you have no parameters in the url
                return $scheme://$host:$server_port/viewer.html?pdf=/files/$1;
        }
    }

谢谢@Gerard H. Pille

相关内容