1

I'm returning a pdf file at a specific location in nginx. When someone goes to mysite.com/resume, I want my resume.pdf to be returned but at the redirected url of mysite.com/resume.pdf (so that the browser will show download/print/etc buttons related to pdfs).

What I have below returns the pdf since resume.pdf is in /dir/to/contents/, but as mysite.com/resume, not mysite.com/resume.pdf. How can I achieve this kind of redirect?

server {
            listen       80;
            listen       [::]:80;
            server_name  mysite.com;
            root /dir/to/contents;

            location / {
                index site.html;
            }

}

- Edit: address first comment

3
  • From the configuration you show in your question, it's not obvious how you are mapping /resume to /resume.pdf. Nov 16, 2019 at 19:07
  • @RichardSmith - just addressed above, resume.pdf is in the dir specified by the root directive
    – lsimmons
    Nov 16, 2019 at 19:14
  • So you just renamed the file from resume.pdf to resume? Remark the your browser doesn't care if the URL of the page ends in .pdf, what matters is the media type sent by nginx. On the other hand nginx guesses the media type based on the file extension. So what URL do you want to have and what do you want to call the file on the disk? I assume you want the content served as application/pdf. Nov 16, 2019 at 20:55

3 Answers 3

1

Add .pdf to /resume (invisible to the client):

    rewrite ^(/resume)$ $1.pdf last;

It is good idea to provide correct file name to the browser, e.g.

    location ~* ([^/]*\.pdf)$ {
        add_header Content-Disposition 'attachment; filename="$1"';
    }
2
  • thanks, please see comment on Danila Vershinin's post - tried your code in the same way and couldn't get it to work
    – lsimmons
    Nov 17, 2019 at 2:45
  • rewrite rule should be in location / block or in server block.
    – edo1
    Nov 17, 2019 at 10:36
0

The following should be sufficient (I did test this):

rewrite ^/resume$ /resume.pdf last;

As pointed out by @Piotr P. Karwasz the MIME type detection will be handled by NGINX. Because the rewrite ends up with a .pdf file, NGINX will correctly serve it as application/pdf.

The extra bits by @edo1 are only needed in case you prefer to have the file to be prompted for downloaded, as opposed to default behavior of your browser (many - displays in browser).

2
  • thanks for your resp. What version/environment did you test it out in? I'm trying it on Ubuntu nginx 1.14.0 with localhost as server_name and can't get it to work. Tried putting the above directive in the server block, in the location / block, and in a location /resume / block, and none work for me.
    – lsimmons
    Nov 17, 2019 at 2:42
  • The environment where I got this to work is CentOS 7 / NGINX 1.16.x stable. However, it's not environment specific. Make sure it's put in server { } context directly (outside of any location) Nov 17, 2019 at 12:28
0

Both edo1 and Danila Vershinin's answers will return the pdf, but both return it at the url without the .pdf ending - for my picky use case of wanting the file returned with the .pdf extension I found the directive below to work.

server {
    ...
    rewrite ^/resume$ resume.pdf redirect;
    ...
}

Not sure if this offers worse performance than other answers or if it's not as idiomatic, but it does what I was looking for. The redirect at the end returns a 302.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .