2017-06-16 4 views
1

Der Autorisierungsheader wird nicht mit einer HTTP OPTIONS-Anforderung gesendet. Ich möchte diese Authentifizierung nur deaktivieren, wenn die Anforderung OPTIONS ist, und sie für andere Anforderungen aktiviert lassen. Hier ist der relevante Teil des Config-Codes, den ich gerade habe. kann nicht sehen, warum es nicht funktioniert. Ich bekomme immer einen 401 Nicht autorisierten Fehler bei der OPTIONS-Anfrage.nginx: Erfordert keine Standardauthentifizierung, wenn http-Anforderung OPTIONS ist

location ~ /foo/bar 
    { 

     if ($request_method = OPTIONS) { 
     set $auth_basic "off"; 
     } 
     if ($request_method != OPTIONS) 
     { 
     set $auth_basic "Resctricted"; 
     set $auth_basic_user_file /var/www/.htpasswd; 
     } 
     auth_basic $auth_basic; 
     auth_basic_user_file $auth_basic_user_file; 
    } 

Antwort

0

Der einfachste Weg, um damit umzugehen ist nginx erlauben die OPTIONS Anfrage zu bearbeiten:

server { 
    listen 80; 
    server_name example.com; 
    root /var/www; 

    auth_basic "Resctricted"; 
    auth_basic_user_file /var/www/.htpasswd; 

    location/{ 
     if ($request_method = OPTIONS) { 
      add_header Access-Control-Allow-Origin "http://example.com"; 
      add_header Access-Control-Allow-Methods "GET, OPTIONS"; 
      add_header Access-Control-Allow-Headers "Authorization"; 
      add_header Access-Control-Allow-Credentials "true"; 
      add_header Content-Length 0; 
      add_header Content-Type text/plain; 
      return 200; 
     } 
    } 
} 

Diese OPTIONS erlauben eine Antwort, ohne Authentifizierung zu erhalten:

[email protected] www $ curl -i -X OPTIONS http://example.com 
HTTP/1.1 200 OK 
Server: nginx 
Date: Sat, 17 Jun 2017 00:09:52 GMT 
Content-Type: application/octet-stream 
Content-Length: 0 
Connection: keep-alive 
Access-Control-Allow-Origin: http://example.com 
Access-Control-Allow-Methods: GET, OPTIONS 
Access-Control-Allow-Headers: Authorization 
Access-Control-Allow-Credentials: true 
Content-Length: 0 
Content-Type: text/plain 

[email protected] www $ curl -i http://example.com 
HTTP/1.1 401 Unauthorized 
Server: nginx 
Date: Sat, 17 Jun 2017 00:09:59 GMT 
Content-Type: text/html 
Content-Length: 188 
Connection: keep-alive 
WWW-Authenticate: Basic realm="Resctricted" 

<html> 
<head><title>401 Authorization Required</title></head> 
<body bgcolor="white"> 
<center><h1>401 Authorization Required</h1></center> 
<hr><center>nginx</center> 
</body> 
</html> 
+0

Vielen Dank für Ihre Antwort. Ich habe das mit Ihrer Hilfe gefunden. Wäre das nicht richtig? Der Authentifizierungscode muss da sein, da dies der einzige Ort ist, der dies erfordert. Ich bekomme immer noch einen 401 Unauthorized. 'Lage ~/foo/bar/ { if ($ REQUEST_METHOD = OPTIONS) { ... return 200; } auth_basic "Eingeschränkt"; auth_basic_user_file /var/www/.htpasswd; } ' –

0

Es sieht aus wie es ist ein alter Post, aber diese Lösung gefunden:

Setzen Sie die folgende Konfiguration in "location" und entfernen Sie auth_basic vom Server. Dies funktioniert

location/{ 
    # Your node proxy configuration for example # 

    # Make options requests work # 
    limit_except OPTIONS { 
     auth_basic "Restricted access zone"; 
     auth_basic_user_file /etc/nginx/pass/protected; 
    } 
    } 
Verwandte Themen