potapuff
potapuff

Reputation: 1980

Nginx and relative path

On nginx root we have such files:

   - test\
       image.jpg
       index.html
   - ...

There is link to the image.jpg at index.html:

<img src='image.jpg'>

In standard Nginx config we have such lines (http://nginx.org/ru/docs/http/ngx_http_core_module.html#try_files)

   server {
    server_name example.com;
    ...
    try_files $uri $uri/index.html;
   }

For URL http://example.com/test/index.html (and for http://example.com/test/ ) all will work well, because base for relative path will be http://example.com/test/ and we have image.jpg in this folder.

For http://example.com/test page will be shown, but without image.

What is the best solution in this case?

Upvotes: 2

Views: 9365

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

I think you mean $uri with a I and not $url with an L.

The problem is that try_files will source the file at $uri/index.html but leave the URI as /test so any relative URIs will be relative to / and not /test/.

If you are going to use relative URIs within your HTML files, you must enforce the trailing / on directory references. For example:

index index.html;
try_files $uri $uri/ =404;

This will cause /test to redirect to /test/ and the index directive will automatically try the index.html file within the sub directory.

Upvotes: 6

Related Questions