Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

In nginx location block with regex match - the entire path including the location part is appended to the alias

I have a webapp with nginx (within a docker container: webserver_nginx_1) which I'm trying to configure.

The file admin_view_groups.html loads main.js.

I expect the following mapping, based on the location ~ /V1 block in nginx.conf:
/V1/js/mlj/main.js -> /usr/src/app/web/V1/js/mlj/main.js

# the file exists within the container  
docker  exec  -it webserver_nginx_1 bash
root@90c800c4bd28:/# ls -l /usr/src/app/web/V1/js/mlj/main.js
-rwxrwxrwx 1 1000 1000 1709 Jan  9 06:49 /usr/src/app/web/V1/js/mlj/main.js

But the file is not loaded, and I see the following errors:

# In chrome devtools
GET https://localhost/admin_edit_group/V1/js/mlj/main.js net::ERR_ABORTED 404
   
# the log for the "V1" location block shows the following debug messages
tail -f /var/log/nginx/V1-messages.log
2021/01/10 06:34:31 [debug] 9#9: *2 http script copy: "/usr/src/app/web/V1"
2021/01/10 06:34:31 [debug] 9#9: *2 http script var: "/V1/js/mlj/main.js"
2021/01/10 06:34:31 [debug] 9#9: *2 trying to use file: "/V1/js/mlj/main.js" "/usr/src/app/web/V1/V1/js/mlj/main.js"

Within the location matching block:

  • the string /V1/js/mlj/main.js is matched
  • a new path is constructed with 2 "V1" /usr/src/app/web/V1/V1/js/mlj/main.js

In nginx, within a location block, I'm using alias (and not e.g. root). According to here, in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.

In my case (option1):
the path: /V1/js/mlj/main.js
the location: /V1/
the portion of the path NOT including the location part: js/mlj/main.js
the alias: /usr/src/app/web/V1/

I expected the new path to be /usr/src/app/web/V1/ + js/mlj/main.js =
/usr/src/app/web/V1/js/mlj/main.js
But instead I'm getting:
/usr/src/app/web/V1/V1/js/mlj/main.js

The following work-arounds work ok, and the file loads ok:

  • If I change the alias to be /usr/src/app/web;
    p.s. within the location ~ /V1/ block, I need to have autoindex on; and try_files (if I place autoindex off; the file fails to load) (option2)

  • If I use root (instead of alias) (option3)

Still, I would like to understand why alias is not working as expected.
Can someone explain why the entire path including the location part is appended to the alias?


Related code:

# the html files:
cat admin_view_groups.html
...
<script type="module" src="/V1/js/mlj/main.js"></script>

# --------------------------------------------------------------

# the location regex block in the nginx conf file
cat nginx.conf
...

    location ~ /V1/ {
        error_log /var/log/nginx/V1-messages.log debug; 

        # option1 - 2 V1 are created in the path - NOT ok
        alias /usr/src/app/web/V1/;
        autoindex on;
        try_files $uri index.php;

        # option2 - the file loads ok
        # alias /usr/src/app/web;
        # autoindex off;

        # option3 - the file loads ok
        # root /usr/src/app/web;
    }

question from:https://stackoverflow.com/questions/65651059/in-nginx-location-block-with-regex-match-the-entire-path-including-the-locatio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...