banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

Add user authentication configuration to the website with Nginx (Basic HTTP authentication).

Note: The ngx_http_auth_basic_module module allows visitors to access web content only if they enter the correct username and password. Some content on the web is not intended to be known by others, but you may want certain people to be able to see it. The http auth module in Nginx and Apache http auth are both good solutions for this.

image

Here, we will use Junge's LNMP as an example. By default, Nginx already has the ngx_http_auth_basic_module module installed.

Example of Nginx Authentication Configuration#

1. Generate the authentication file

# printf "test:$(openssl passwd -crypt 123456)\n" >>/home/htpasswd
# cat /home/htpasswd 
test:xyJkVhXGAZ8tM

Note: The account here is test and the password is 123456. Remember the path to the authentication file.

2. Configure the website conf file

server{
       listen 80;
       server_name  www.moeyy.cn moeyy.cn;
       index index.html index.php;
       root /home/wwwroot/www.moeyy.cn;       
       location /
       {
                auth_basic "Please enter your username and password";
                auth_basic_user_file /home/htpasswd; 
                autoindex on;
       }
}

Note: Pay attention to the auth_basic_user_file path, otherwise you will constantly encounter 403 errors.

3. Restart Nginx

/etc/init.d/nginx restart

Setting Username and Password for Nginx Directory Authentication in LNMP#

Sometimes, you need to add authentication to a specific directory, similar to what Apache does. In Apache, you usually use htpasswd to add authentication, but htpasswd is included in apache2-utils, which is generally not installed in LNMP one-click installation packages or self-compiled LNMP installations.

1. Create a htpasswd-like file
Execute the following command:

wget -c https://www.moeyy.cn/usr/down/htpasswd.sh;bash htpasswd.sh

Follow the prompts to enter the username, password, and authentication file name. The script will automatically generate the authentication file. Take note of the file path returned by the script, for example: /usr/local/nginx/conf/vpser.net.auth.

2. Add auth authentication configuration to Nginx
Below is an example of adding the following code to the server section of a domain, using the soft directory as an example:

location ^~ /soft/
{
auth_basic "Authorized users only";
auth_basic_user_file Insert the file path returned by the previous script here;
}

Authorized users only is the prompt message, which can be modified to any desired message. The auth_basic_user_file needs to be filled with the file path returned by the htpasswd.sh script. After modifying the configuration as instructed above, restart Nginx. When accessing http://yourdomainname/soft/, you will be prompted to enter a username and password.

Note: After adding authentication, PHP files in that directory will not be parsed and a download prompt will appear. If you want to parse PHP files, you can modify the configuration as follows:

location ^~ /soft/ {
    location ~ .*\.(phpphp5)?$ {
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    auth_basic "Authorized users only";
    auth_basic_user_file Insert the file path returned by the previous script here;
}

This tutorial is suitable for LNMP one-click installation packages or self-installed LNMP setups, although the directory and configuration file locations may vary.

After configuring, execute: /usr/local/nginx/sbin/nginx -t to test if there are any errors in the configuration.
Then execute: /usr/local/nginx/sbin/nginx -s reload to load the configuration file.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.