快轉到主要內容

Set nginx access password

·353 字
Computer-Science Nginx
目錄

How to make nginx website require password to access ?


Make authentication file
#

For example, I make the file in path /etc/nginx/htpasswd/, and name it my_auth.
Edit it, fill in the username you want to use to login (e.g. my_user), followed by the : symbol. (p.s. Dont break the new line)
Then save the file, type the following command to generate the password (change to your own path):

1openssl passwd -apr1 >> /etc/nginx/htpasswd/my_auth

Check the file, you should see the content looks like this:

1my_user:$apr1$l3IuPvhA$/KzcRqpMNC7dUBG9XwQ370

For setting mutiple users:

1my_user:$apr1$l3IuPvhA$/KzcRqpMNC7dUBG9XwQ370
2my_user2:$apr1$UePZ9SIn$HVpEhDfM5dgk2fgbGE81T/
3my_user3:$apr1$4t8dv/wc$9Gy5Hb5hnaLO5Zwey1WbJ1

Configure nginx
#

Edit config file of nginx.
For example, my config file is /etc/nginx/conf.d/default.conf, and I want to make the users require password when accessing path of website under /admin/, add the following settings in file (server block):

1location ~ /admin/ {
2  auth_basic  "Login to access files.";
3  auth_basic_user_file    /etc/nginx/htpasswd/my_auth;
4}

p.s. Dont forget to change above content to your own path of auth file.


Make nginx reload the config
#

(Optional) Check it first: nginx -t

You can simply use command nginx -s reload to reload config, or restart the service service nginx restart.
And finished.

Now when you access the path of website, it will show such the input box:

Access with password
Access with password


References
#

Alpaca
作者
Alpaca
No one can stop my feet.

相關文章

Linux 常見壓縮指令整理
·279 字
Computer-Science

How to use compress file commands in linux ?

《Pwn - 0x03》Tools - GDB, Pwntools
·2054 字
Computer-Science Pwn
Ubuntu Server Auto Login TTY when Start up
·443 字
Computer-Science Ubuntu
Ubuntu "Crontab" 工作排程
·1115 字
Computer-Science Ubuntu
《Pwn - 0x02》What did the "Compilation" do? And what is ELF?
·6890 字
Computer-Science Pwn
《Pwn - 0x01》Segments? Stack, Heap? .text, .data, .bss?
·2877 字
Computer-Science Pwn