2

How can I serve a PDF file at the below address:

127.0.0.1/getMeThatFile/willYou?name=jane

which is stored at a location:

/usr/share/nginx/thatFile.pdf

I tried to follow Serve pdf file by location in nginx, but couldn't get it to work:

server {
        location /getMeThatFile/willYou/ {
                alias /usr/share/nginx/;
                return 302 thatFile.pdf;
        }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pushp Vashisht
  • 874
  • 3
  • 9
  • 17

1 Answers1

1

/getMeThatFile/willYou and /getMeThatFile/willYou/ are different URIs. Your question suggests that the request will use the first, but your solution matches the second.

Use location /getMeThatFile/willYou to match both, or location = /getMeThatFile/willYou to match only the first. See this document for details.

To return a single file, use root and try_files. For example:

location = /getMeThatFile/willYou {
    root /usr/share/nginx/;
    try_files /thatFile.pdf =404;
}

Assuming thatFile.pdf always exists, the =404 is never reached, but is necessary as try_files requires at least two parameters. See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81