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

别来无恙 提交于 2021-01-29 17:25:51

问题


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;
    }

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!