2016-12-14 12 views
1

nginxサーバの下にsymfonyアプリケーションがあります。私は基本的なhttpの認証を有効にしたいが、/ api/urlリクエスト内のすべてを除外したい。Nginx + Symfony。サブフォルダ以外の基本認証

これは、nginxのための私の現在の設定です:/etc/nginx/php-mysite.conf

server { 
    listen 80; 
    listen [::]:80; 

    root /home/mysite/www/web; 

    index app.php index.php index.html; 

    server_name mysite.com; 

    error_log /home/mysite/logs/error.log warn; 
    access_log /home/mysite/logs/access.log; 


    location/{ 
     # try to serve file directly, fallback to app.php 
     try_files $uri /app.php$is_args$args; 
    } 

    # PROD 
    location ~ ^/app\.php(/|$) { 
     include /etc/nginx/php-mysite.conf; 

     # Protect access 
     auth_basic "Restricted"; 
     auth_basic_user_file /etc/nginx/.htpasswd; 
    } 

    location ~ ^/app\.php/api/(.*) { 
     include /etc/nginx/php-mysite.conf; 
     auth_basic "off"; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 
} 

は、PHP-FPM構成です。それはうまくいく。

問題はすべてのリクエストが^app.php(/|$)ロケーションディレクティブによって処理されているようです。 /api/...のURLにアクセスするときに、認証要求を無効にするように設定できません。

私は成功しなくても数時間を過ごしました。

答えて

1

まあ、私はこの解決策が嫌いですが、うまくいきます。私が行ったのは、locationディレクティブのrequest_uriを調べることです。/ apiで始まっていれば、auth basicを有効にします。私はこの目的のために2つの別々の場所を使用したいと思います。

これは、要求が^/APIと一致した場合に、それがその後、認証がオフに設定されている/.*$有効とされ、デフォルトでは、場所のディレクティブです:

# PROD 
location ~ ^/app\.php(/|$) { 
    include /etc/nginx/php-mysite.conf; 

    # Protect access 
    set $auth "Restricted"; 
    if ($request_uri ~ ^/api/.*$){ 
     set $auth "off"; 
    } 

    auth_basic $auth; 
    auth_basic_user_file /etc/nginx/.htpasswd; 
}