1 つの場所で nginx basic_auth を無効にし、サイトの残りの部分では有効にする方法

1 つの場所で nginx basic_auth を無効にし、サイトの残りの部分では有効にする方法

特定のディレクトリ ( /api/) に対して basic_auth を無効にしようとしていますが、作業しているサイトの残りの部分に対しては basic 認証がまだ有効になっています。以下は私の ですnginx.conf:

server {
    # base settings
    listen 80;
    server_name somesite-somewhere-anywhere.com;
    root /var/www/wordpress;
    index index.php index.html index.htm;

    if (!-e $request_filename) {

        rewrite ^(.+)$ /index.php?q=$1 last;
    }

    # setup logs
    access_log /var/log/nginx/somesite-somewhere-anywhere.com.access.log;
    error_log /var/log/nginx/somesite-somewhere-anywhere.com.error.log;

    # setup 404
    error_page 404 /404.html;
    location  /404.html {
        internal;
    }

    # map 403 to 404
    error_page 403 = 404;

    # hide wordpress details
    location ~ /(\.|wp-config.php|readme.html|licence.txt) {
        return 404;
    }

    # add trailing slash to wp-admin requests
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;


    # ignore robots in logging
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # ssl redirect

    # setup location
    location / {
        # setup basic auth 
        auth_basic dk; 
        auth_basic_user_file /var/www/htpasswd;

        # fastcgi setup
        location ~* (^(?!(?:(?!(php|inc)).)*/uploads/).*?(php)) {
            try_files $uri = 404;
            fastcgi_split_path_info ^(.+.php)(.*)$;
            fastcgi_pass unix:/var/run/php-fpm.socket;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_ignore_client_abort off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

        # prevent access to hidden files
        location ~ /\. {
            deny all;
            access_log off;
            log_not_found off;
        }
    }
    # allow access to api without auth
    location  /api/ { 
        auth_basic "off" ;
    }
}

答え1

ご覧になれますnginx 認証の基本ドキュメント必要なのは「auth_basic off;」です。したがって、ここでは何か他のことが起こっていると思われます。URL にアクセスする方法を教えてください。

おそらく、末尾のスラッシュなしで /api を入力しているのではないでしょうか。それでも問題が解決しない場合は、次のようにして回避できるかもしれません。

location /api/ {
    satisfy any;
    allow all;
    auth_basic           "dk";
    auth_basic_user_file "/var/www/htpasswd";
}

これは間違った方法ですが、環境内で正しいことを行うことの重要性に応じて、迅速になる可能性があります(場所/継承が正しく指定されている場合)。

関連情報