# Basic-Auth for Shopware 6 with nginx

Unlike Apache2, where basic-auth is set in the local .htaccess file, in nginx it is set in the server's vhosts file. The downside is that it is difficult to integrate into the CI/CD pipeline. The upside is that it is defined per server by definition and thus doesn't require CI/CD procedures to take care of what the deployment's target (e.g. production, staging, ...) is.

First of all, create an .htpasswd file suiting your needs. A variety of online tools exist for this task. If you have access to a Unix/Ubuntu system, the clean way to go is to run the hdpasswd command:

<pre class="language-shell"><code class="lang-shell">htpasswd <a data-footnote-ref href="#user-content-fn-1">-c</a> <a data-footnote-ref href="#user-content-fn-2">.htpasswd</a> <a data-footnote-ref href="#user-content-fn-3">&#x3C;user></a>
</code></pre>

htpasswd will prompt you for the basic auth password.

Then locate and edit the nginx vhost file. At its head, preceding the server section, add the following lines:

<pre class="language-nginx"><code class="lang-nginx">map $request_uri $auth_type {
  default "off";
  ~/api/* "off";
  ~* "<a data-footnote-ref href="#user-content-fn-4">vanWittlaer Demo Site</a>";
}
...
</code></pre>

The above code sets a variable $auth\_type to either "off" or to "vanWittlaer Demo Site" (or any other text you chose). Further down in the vhost file, and inside the server section, add the following two lines:

```nginx
...
auth_basic $auth_type;
auth_basic_user_file <path-to-your-htpasswd-file>/.htpasswd;
...
```

[^1]: The -c option tells htpasswd to create a new file. Omit this option if you want to add further users to an existing file.

[^2]: This is the name of the htpasswd file. YOu may chose any name, .htpasswd is just a common practice

[^3]: This is the user to be used for basic auth

[^4]: Chose any text you like to be displayed on the basic auth login popup.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notebook.vanwittlaer.de/hosting/basic-auth-for-shopware-6-with-nginx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
