nginx:basic_authentication
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
nginx:basic_authentication [2016/10/13 23:38] – peter | nginx:basic_authentication [2019/12/01 10:19] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== NginX - Basic Authentication ====== | ||
- | This is the Nginx equivalent to basic HTTP authentication on Apache with .htaccess /.htpasswd. | ||
- | |||
- | ===== Creating the Password File ===== | ||
- | |||
- | We need a password file where users that should be able to log in are listed with their passwords (in encrypted form). | ||
- | |||
- | ===== Using Apache' | ||
- | |||
- | If you want to use Apache' | ||
- | |||
- | <code bash> | ||
- | which htpasswd | ||
- | </ | ||
- | |||
- | Will return something like this is the htpassed command exists on the system. | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | |||
- | If the command returns without any output, htpasswd does not exist on your system, and you must install it. On Debian/ | ||
- | |||
- | <code bash> | ||
- | apt-get -y install apache2-utils | ||
- | </ | ||
- | |||
- | |||
- | I want to create the password file / | ||
- | |||
- | <code bash> | ||
- | htpasswd -c / | ||
- | </ | ||
- | |||
- | You will be asked for a password for the user john. Please note that the -c switch makes that the file is created from scratch; if it didn't exist before, it will be created; if it existed before, it will be overwritten with a new one, and all users from the old file will be lost! Therefore, if you want to add another user without deleting all existing users, use the htpasswd command without the -c switch: | ||
- | |||
- | <code bash> | ||
- | htpasswd / | ||
- | </ | ||
- | |||
- | The last command adds the user jack to / | ||
- | |||
- | |||
- | |||
- | ===== Using the htpasswd.py Python Script ===== | ||
- | |||
- | If you don't want to or cannot use Apache' | ||
- | |||
- | We download it to / | ||
- | |||
- | <code bash> | ||
- | cd / | ||
- | wget http:// | ||
- | chmod 755 / | ||
- | </ | ||
- | |||
- | I want to create the password file / | ||
- | |||
- | htpasswd.py -c -b / | ||
- | |||
- | Please replace johnssecret with a password for the user john. Please note that the -c switch makes that the file is created from scratch; if it didn't exist before, it will be created; if it existed before, it will be overwritten with a new one, and all users from the old file will be lost! Therefore, if you want to add another user without deleting all existing users, use the htpasswd.py command without the -c switch: | ||
- | |||
- | htpasswd.py -b / | ||
- | |||
- | The last command adds the user jack to / | ||
- | |||
- | |||
- | ===== Configuring Nginx ===== | ||
- | |||
- | Now that we have our password file in place, we just need to add it to our Nginx vhost configuration in / | ||
- | |||
- | |||
- | <code bash> | ||
- | vi / | ||
- | </ | ||
- | |||
- | Because I want to password-protect the test directory in the document root, I use location /test {} here (to password-protect the whole website, you'd use location / {}): | ||
- | |||
- | <file bash / | ||
- | server { | ||
- | | ||
- | | ||
- | root / | ||
- | [...] | ||
- | | ||
- | auth_basic " | ||
- | auth_basic_user_file / | ||
- | } | ||
- | [...] | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | Reload Nginx afterwards: | ||
- | |||
- | <code bash> | ||
- | service nginx reload | ||
- | </ | ||
- | |||
- | That's it! You can now go to your test directory in a browser (http:// | ||
- | |||
- | If you enter the correct username and password, you'll be granted access. | ||
- | |||
- | Otherwise, you will see a 401 Authorization Required error message. |
nginx/basic_authentication.1476401891.txt.gz · Last modified: 2020/07/15 09:30 (external edit)