Serving pdf configuration issue

Maxim Dounin mdounin at mdounin.ru
Fri Feb 9 02:52:18 UTC 2024


Hello!

On Wed, Feb 07, 2024 at 02:21:09PM -0500, Victor Oppenheimer wrote:

> I am running nginx 1.24.0 on a computer with
> Windows Server 2016 Server standard OS.
> 
> I am truing to use nginx to function as a reverse proxy
> server when it received requests for some URLs and
> to directly serve web pages for others.  I was successful s
> configuing nginx to do the reverse proxy serving.
> 
> However, when I tried to directly serve some .pdf pages I kept
> getting errors.  To isolate the problem I edited nginx.config
> to only function as a direct server for a .pdf file.
> 
> I have a file named camprental.pdf in the directory
>     C:\Users\victor\My
> Drive\Properties\rental_manuals_documents_and_email_templates\Clearwater
> 
> I want users to be able to browse to:
>     www.clearwaterescapes.com/camp/camprental.pdf to see the file.
> 
> The nginx.conf file I am using to try to accomplish this is copied below.
> 
> When I start nginx with this config file and browse to:
>     http://clearwaterescapes.com/camp/camprrental.pdf
> 
> I receive the following error message:
>     This page isn’t working right nowclearwaterescapes.com redirected you
> too many times.
> 
> I assume that I am probably making a simple error.  But I do not see it.
> 
> Hopefully somebody will point it out.

[...]

>  server {
>     listen 80;
>     server_name clearwaterescapes.com;
> 
>        # Make incoming URLs lowercase
>     rewrite ^(.*)$ /$1 permanent;

This is going to be a infinite redirection loop, as you redirect 
any requests to the same URI.  Likely this is what causes the 
"redirected too many times" error you are seeing.

> 
>   # Rewrite rule for PDF files
>   rewrite ^clearwaterescapes.com/camp/camprental.pdf$
> http://www.clearwaterescapes.com/documentation/camprental.pdf break;

This is not going to work, as rewrite works with URI as seen in 
the HTTP request, that is, URI path, and it doesn't contain domain.

(And it does not seem to be needed, given the following 
locations.)

> 
>     # avoid errors when favicon.ico file is missing
>     location = /favicon.ico {
>         access_log off;
>         log_not_found off;
>         return 204;
>     }
> 
>     location ~* ^/documentation/ {
>         # Convert URL to lowercase and capture filename
>         rewrite ^/documentation/(.*)$ /documentation/$1 break;
> 
>         # Serve documentation files directly
>         root "C:/Users/victor/My
> Drive/Properties/rental_manuals_documents_and_email_templates/clearwater";
>         try_files /$1 =404;

Just

    location /documentation/ {
        alias "C:/.../clearwater/";
    }

should do the trick.

>     }
> 
>     # Add location block for /camp/ requests
>   location ~* ^/camp/ {
>     root "C:/Users/victor/My
> Drive/Properties/rental_manuals_documents_and_email_templates/clearwater";
>     try_files $uri =404;
> }

There is no need to use regex location here, just a prefix one 
will be equivalent on Windows (since on Windows location matching 
is caseless).  Similarly, in most cases there is no need to use 
try_files, as simply serving static files is equivalent (unless 
you specifically want to return 404 for directories).

That is, just

     location /camp/ {
         root "C:/.../clearwater";
     }

would be (mostly) equivalent.

But, given that you want "/camp/camprental.pdf" to access the file 
in the "C:/.../Clearwater" directory, correct approach would be to 
use "alias" instead of "root", similarly to the above.

     location /camp/ {
         alias "C:/.../clearwater/";
     }

Hope this helps.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list