Home > front end >  How to cofigure nginx to serve a pdf file?
How to cofigure nginx to serve a pdf file?

Time:01-24

I´m trying to serve a pdf file by making a configuration in nginx but I get the following error in the page: 404 Not Found

The configuration is like this:

server {

    listen 3002;
    index index.html;
    server_name _;

    location / {

        root /var/www/html/pdf_carpet;
        try_files $uri /index.html = 404;
    }
}

pdf_carpet is where the pdf file is.

What could I do or change to be able to serve a pdf file in nginx?

P.S. It works with html files.

CodePudding user response:

Here is the full location block that should show the PDF file in the browser window under the http://<your_domain_or_IP>:3002/pdf_carpet URL:

location = /pdf_carpet {
    alias /var/www/html/pdf_carpet/file.pdf;
    default_type application/pdf;
    add_header Content-Disposition 'inline';
}

Update

If an URI for accessing the PDF file ends with the slash (or it is a root URI as a special case), the above config would not work since an index file name will be appended to such an URI by the nginx (making location = /path/ { ... } not match the $uri internal nginx variable). For such a case another technique can be used:

location = / {
    root /var/www/html/pdf_carpet;
    rewrite ^ /file.pdf break;
    add_header Content-Disposition 'inline';
}
  •  Tags:  
  • Related