Skip to content

Commit

Permalink
Merge pull request #2389 from nginx-proxy/2300
Browse files Browse the repository at this point in the history
feat: define basic auth for virtual path
  • Loading branch information
buchdag committed Feb 10, 2024
2 parents df890de + fa23c11 commit e50429e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ docker run -d -p 80:80 -p 443:443 \

You'll need apache2-utils on the machine where you plan to create the htpasswd file. Follow these [instructions](http://httpd.apache.org/docs/2.2/programs/htpasswd.html)

If you want to define basic authentication for a `VIRTUAL_PATH`, you have to create a file named as /etc/ngingx/htpasswd/${VIRTUAL_HOST}_${VIRTUAL_PATH_SHA1}
(where $VIRTUAL_PATH_SHA1 is the SHA1 hash for the virtual path, you can use any SHA1 online generator to calculate it).

### Upstream (Backend) Server HTTP Load Balancing Support

> **Warning**
Expand Down
5 changes: 4 additions & 1 deletion nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@
set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
{{- end }}

{{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
{{- if (exists (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path) )) }}
auth_basic "Restricted {{ .Host }}/{{ .Path }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path)) }};
{{- else if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
auth_basic "Restricted {{ .Host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
{{- end }}
Expand Down

0 comments on commit e50429e

Please sign in to comment.