2

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.

5
  • You want to serve a single file? Check this answer. Change the location to location = / { ... }. Commented Jan 23, 2022 at 16:18
  • Thanks. I tried doing that but didn´t work :( Still show error. I put it like this: server { listen 3002; index index.html; server_name _; location = /pdf_carpet { alias /var/www/html/pdf_carpet/file.pdf; add_header Content-Disposition 'attachment; filename="file.pdf"'; try_files $uri /index.html = 404; } }
    – Coder20
    Commented Jan 23, 2022 at 17:07
  • Do you see a try_files directive in the answer I'm referring to? Remove it. And how are you trying to access the file? With the config you show in your last comment (without the try_files directive) your PDF file should be accessible with the http://<your_domain_or_IP>:3002/pdf_carpet URL. Commented Jan 23, 2022 at 17:31
  • And since you are serving a PDF file, add the default_type application/pdf; directive to that location. Commented Jan 23, 2022 at 17:40
  • Now works but it downloads the file. I want to display the file in the page, not to download it. How could I do that?
    – Coder20
    Commented Jan 23, 2022 at 18:05

1 Answer 1

4

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';
}
5
  • Now works fine. Thank you :) One last question, is there a way I shouldn´t need to put the direction for the carpet? For example, to put just the following as the URL: http://<your_domain_or_IP>:3002
    – Coder20
    Commented Jan 23, 2022 at 18:27
  • Sure, using location = / { ... } instead of location = /pdf_carpet { ... } as I suggested in the very first comment. Commented Jan 23, 2022 at 18:30
  • If I put it like that it gives me this error: 500 Internal Server Error
    – Coder20
    Commented Jan 23, 2022 at 18:50
  • And it have location like this: location = / { alias /var/www/html/pdf_carpet/file.pdf; default_type application/pdf; add_header Content-Disposition 'inline'; }
    – Coder20
    Commented Jan 23, 2022 at 18:56
  • You are right, using an URI ended with slash will break given configuration (ngx_http_index_module will add an index file name to the URI, and in error log you'll see /var/www/html/pdf_carpet/file.pdfindex.html" is not a directory message). See an update to the answer for the solution. Commented Jan 23, 2022 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.