User Tools

Site Tools


linux:nginx-basic-auth

Nginx Basic Auth

Enable the basic auth in Nginx config

I just enable it for the path “/upload”, so add the location segment

location /upload {
		try_files $uri $uri/ /index.php$is_args$args;
		autoindex on;
                auth_basic "nginx basic http auth";
                auth_basic_user_file conf/htpasswd;
	}

So the whole will be

snippet.file
server {
	listen 8000;
	server_name sap.pjq.me ddns.pjq.me;
	return 301 https://$server_name:8443$request_uri;
}
server {
	listen 8443;
	server_name sap.pjq.me ddns.pjq.me;
	ssl on;
	ssl_session_timeout 5m;
	root /home/pi/Music/;
	index index.html index.htm index.php;
	location / {
		try_files $uri $uri/ /index.php$is_args$args;
		autoindex on;
	}
	location /upload {
		try_files $uri $uri/ /index.php$is_args$args;
		autoindex on;
                auth_basic "nginx basic http auth";
                auth_basic_user_file conf/htpasswd;
	}
    location ~ \.ini$ {
          return 444;
     }
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
 
    }
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
    }
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}
}

Generate the username/password

echo "user:$(openssl passwd -crypt pwd)>>conf/htpasswd
/var/www/dokuwiki/wiki/data/pages/linux/nginx-basic-auth.txt · Last modified: 2019/10/21 12:30 by percy