How to cofigure nginx to serve a pdf file?

Solution 1:

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