title: How do I create an auth_basic nginx config that excludes any URL path starting with a specific string?
body: <p>I'm trying to set up nginx basic authentication for the whole website, except for <strong>any URLs starting with "/ghost/"</strong>.</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>server {
      listen 443 ssl http2;
      listen [::]:443 ssl http2;
  
      server_name mysite.com;
      root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
  
      ssl_certificate /etc/letsencrypt/mysite.com/fullchain.cer;
      ssl_certificate_key /etc/letsencrypt/mysite.com/mysite.com.key;
      include /etc/nginx/snippets/ssl-params.conf;
  
      location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $http_host;
          proxy_pass http://127.0.0.1:2368;
  
          auth_basic "Restricted Content";
          auth_basic_user_file /etc/nginx/.htpasswd;
  
      }
  
      location /ghost {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $http_host;
          proxy_pass http://127.0.0.1:2368;
  
          auth_basic off;
  
      }
  
      location ~ /.well-known {
          allow all;
      }
  
      client_max_body_size 50m;
  }
  </code></pre>
  
  <p>(Note: I also tried with <code>/ghost</code> - so without using a trailing slash.)</p>
  
  <p>The config above works partially:</p>
  
  <ul>
  <li>I <strong>can't</strong> access <code>www.mysite.com</code> without authenticating ---> GOOD</li>
  <li>I <strong>can</strong> access <code>www.mysite.com/sub</code> without authenticating ---> GOOD</li>
  <li>I <strong>can't</strong> access any other path starting with <code>/sub/</code> without
  authenticating ---> BAD</li>
  </ul>
  
  <p>How should the config for the <code>/ghost/</code>-leading paths looks like?</p>