D
D
Dmitry2019-01-30 12:05:10
Nginx
Dmitry, 2019-01-30 12:05:10

How to apply nginx location rules for images only on the main page (root) of a site?

Hello!
There is a task: it is necessary to apply rules only to images on the main page of the site example.com/ using location NGINX
For the test, I created a directory inside the site /test/
What directives to write in location so that example.com/1.jpg is processed , but not example is processed .com/test/1.jpg ?
At the moment the config is:

server {
    ...
    location / {
    ...
    }

    location ~* ^/.*\.jpg$ {
    ...
    }

In this case, all images on the server are processed...
If we do the opposite:
server {
    ...
    location / {
    ...
    }

    location ~* ^/test/.*\.jpg$ {
    ...
    }

Then everything works correctly, i.e. example.com/test/1.jpg falls under the rule, but example.com/1.jpg does not.
Can you please tell me how to make a similar rule, but only for the root?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-01-30
@7in

location ~* ^/[^/]+\.jpg$ {
    ...
}

K
ky0, 2019-01-30
@ky0

Use nested locations - top level on /, inside it for pictures.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question