W
W
Win32Sector2019-04-08 15:16:56
Nginx
Win32Sector, 2019-04-08 15:16:56

How to write dynamic location and try_files in NGINX config?

Good afternoon!
There are a bunch of static projects of the same type that are added to the nginx root directory into directories with random names.
For example,
site.ru/ffdsf2425ff
site.ru/2435werw2f
site.ru/ffdsertwet3f
site.ru/ertw3ergeb The
Nginx config now looks like this:

server {
    listen       80;
    server_name  localhost;
    location / {
        root /usr/share/nginx/html;
    }

Each directory of the site contains index.html, which is displayed when you enter at site.ru/project_id
But there is a problem: lines to display also index.html from the directory site.ru/project_id
I think that you need to do something like this:
location ~ /(.*)/(.*) {
      try_files $uri $uri/ $1/index.html;
}

But that doesn't work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Mezhuev, 2019-04-08
@Win32Sector

You almost found the solution, only you missed the "/" at the beginning:

location ~ ^/([a-zA-Z0-9]*)/ {
    root /usr/share/nginx/html;
    try_files $uri $uri/ /$1/index.html;
}

V
Viktor Taran, 2019-04-09
@shambler81

a tray file does not need to be used at all,
since all codes must be processed by the CMS.
If you have apache + nginx

error_page 401 403 404 405 500 502 503 = @fallback;
        location @fallback {
                proxy_pass              http://127.0.0.1:82;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                }

If it's purely nginx, then it's even easier by analogy.
The site itself should be responsible for how to give an error, at least all cms have a 404 owl.
You have given a special case, but there can be a million subtleties on the site, and in any case, cms should try to process them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question