2016-12-14 7 views
1

nginx 서버 아래에 심포니 앱이 있습니다. 기본 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을 사용 가능하게 만든다. 나는이 목적을 위해 두 개의 분리 된 위치를 대신 사용하고자한다.

은 기본적으로 활성화되어, 위치 지시자 인 요청은 정식 오프로 설정 ^/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; 
} 
관련 문제