首页 > 解决方案 > NGINX 基本身份验证,代理内容的位置问题

问题描述

任务是阻止以下内容: /api/ldap-adapter/swagger-ui.html 在 NGINX default.conf 中,我们已经使用了以下位置:

location ~ /api(?<p_uri>.*) {
    #https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p
    set $gateway "http://tasks.gateway:8050";
    proxy_pass $gateway$p_uri?$args;
    proxy_intercept_errors on;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;

当我只是尝试使用通配符或完全匹配来阻止它时:通配符:

location ~* \.html$ {
    auth_basic           "Administrator’s Area";
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd;

完全符合:

location = /api/ldap-adapter/swagger-ui.html {
        auth_basic           "Administrator’s Area";
        auth_basic_user_file /etc/nginx/conf.d/.htpasswd;

我有以下错误:

2019/12/20 10:41:18 [error] 39#39: *39 open() "/usr/share/nginx/html/api/ldap-adapter/swagger-ui.html" failed (2: No such file or directory), client: 10.255.0.5, server: localhost, request: "GET /api/ldap-adapter/swagger-ui.html HTTP/1.1", 

Nginx 使用一些默认的位置前缀:/usr/share/nginx/html..

例如,如何使用 swagger-ui 阻止所有内容?

标签: regexnginxbasic-authenticationnginx-location

解决方案


推荐阅读