Skip to content

Instantly share code, notes, and snippets.

@qifei
Created December 6, 2024 08:08
Show Gist options
  • Save qifei/247c90f10cfa7f88ee8eb07f0b039076 to your computer and use it in GitHub Desktop.
Save qifei/247c90f10cfa7f88ee8eb07f0b039076 to your computer and use it in GitHub Desktop.
Nginx Basic Authentication on OpenWrt

Install Nginx and password generator

opkg update
opkg install nginx openssl-util

Create a file to store password

touch /etc/nginx/.htpasswd

Generate password for user

echo -n 'USERNAME:' >> /etc/nginx/.htpasswd
openssl passwd -apr1 >> /etc/nginx/.htpasswd

cat /etc/nginx/.htpasswd

Edit server configuration

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

Make it effective

service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment