nginx 配置登陆密码


基于rocky 8.4

安装依赖环境

  1. 安装nginx
dnf install nginx -y

验证是否安装成功
在这里插入图片描述

  1. 安装密码生成工具
dnf install httpd-tools -y

在这里插入图片描述

  1. 生成nginx登陆所需用户,密码文件
htpasswd -c /etc/nginx/passwd admin
#-c 创建一个加密文件
#/etc/nginx/passwd 加密文件所在位置
# admin 指定的用户

在这里插入图片描述
补充
删除用户和密码

$ htpasswd -D /usr/local/nginx/password username
 -D 删除指定的用户

修改用户和密码

$ htpasswd -D /usr/local/nginx/password username
$ htpasswd -b /usr/local/nginx/password username pass
#-D 删除指定的用户
#-b htpassswd命令行中一并输入用户名和密码而不是根据提示输入密码
#-p htpassswd命令不对密码进行进行加密,即明文密码

配置Nginx认证

nginx 配置文件,在/etc/nginx/conf/nginx.conf,要对整个站点开启验证,需在配置文件中的server加上认证配置

server {
    
    
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        auth_basic "请输入用户和密码"; # 验证时的提示信息
        auth_basic_user_file /etc/nginx/password; # 认证文件
        location / {
    
    
        }

        error_page 404 /404.html;
            location = /40x.html {
    
    
        }

启动nginx并访问nginx

[root@localhost nginx]#
[root@localhost nginx]# systemctl restart nginx
[root@localhost nginx]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-08-24 13:58:02 CST; 6s ago
  Process: 7639 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 7637 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 7636 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 7641 (nginx)
    Tasks: 2 (limit: 23498)
   Memory: 3.7M
   CGroup: /system.slice/nginx.service
           ├─7641 nginx: master process /usr/sbin/nginx
           └─7642 nginx: worker process

824 13:58:02 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
824 13:58:02 localhost.localdomain nginx[7637]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
824 13:58:02 localhost.localdomain nginx[7637]: nginx: configuration file /etc/nginx/nginx.conf test is successful
824 13:58:02 localhost.localdomain systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
824 13:58:02 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@localhost nginx]#

访问nginx(有防火墙的记得关闭)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44157851/article/details/119887900