NGINX 与位置指令和 proxy_pass 的意外行为

NGINX unexpected behaviour with location directive and proxy_pass

我有一个 NGINX 配置文件,可以通过开发服务器为带有静态文件的网站提供服务。

静态 -> http://localhost:8080

开发网络服务器 -> http://localhost:8080/dev

我将其他几个服务绑定到不同的位置指令。

这是配置文件的片段。

...
upstream qgis {
   server qgis-spcluster_server:80;
}
...    
server {
    listen       8080;
    server_name  localhost;

location / {
    root   /usr/share/nginx/html/build;
    index  index.html index.htm;

    auth_basic "Zugangskontrolle";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location /dev/ {
    proxy_pass http://web_app/;

    auth_basic "Zugangskontrolle";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location /static/ {
    proxy_pass http://web_app/static/;
}

location /qgis/ {
    proxy_pass http://qgis/;
}

location /apex/ {
    proxy_pass http://apex/apex/;
    auth_basic "off";
}

...

一切正常,直到我打开 URL 获取静态文件。之后所有其他 URLs 都指向静态文件。

对我来说一切看起来都很好,但确实有些地方不对。

Basic_Auth 产生了另一种意外行为。

所以现在我有点不知道如何解决这个问题。

请从您的位置指令中删除结尾的 / 或在访问它们时提供 /

Nginx 寻找最长的前缀匹配位置。当您访问 http://localhost:8080/apex 时,它被路由到 / 因为 /apex/ 不是 /apex

的前缀

location 的文档是 here

我现在尝试了几种方法,但没有一种效果很好。所以我决定为我当前设置有问题的所有位置指令创建第二个服务器块。

这可能不是最好的解决方案,因为我仍然不知道为什么会遇到这些问题。但它现在有效,这对我来说很重要。