nginx 位置匹配问题 auth_basic

nginx 位置匹配问题 auth_basic

我有这样的配置:

location / {
    root /etc/nginx/euchet;
}

location /app/ {
    proxy_pass http://172.16.184.2:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE_ADDR $remote_addr;
}

我需要添加另一个auth_basic仅为请求启用的位置:/app/f?p=3000

我尝试使用 rexexp:

   location ~ \/appt\/f\?p=3000 {
            auth_basic "Hello! Enter password";
            auth_basic_user_file /etc/nginx/euchet.htpasswd;

            proxy_pass http://172.16.184.2:8080;
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE_ADDR $remote_addr;
    }

Nginx 使用以前的位置/app/

我尝试了另一种方法:

location ~* /euchet/f {

    if ($arg_p = "4550") {
        return 403;
    }

    error_page 403 = @hidden;
}


location @hidden {
            auth_basic "Hello! Enter password";
            auth_basic_user_file /etc/nginx/euchet.htpasswd;

            proxy_pass http://172.16.184.2:8080;
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE_ADDR $remote_addr;
}

我在 上收到 403 错误f?p=3000,在 上收到 404 错误f?p=<another_number>

我该如何解决这个问题?

答案1

你最好使用地图作为auth_basic指令可以包含变量。

map $request_uri $auth {
    default         "off";
    "/app/f?p=3000" "MyRealm";
}


server {

    location / {
        root /etc/nginx/euchet;
    }

    location /app/ {
        auth_basic $auth;
        auth_basic_user_file /etc/nginx/euchet.htpasswd;
        proxy_pass http://172.16.184.2:8080;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE_ADDR $remote_addr;
    }   

}

相关内容