当前位置:   article > 正文

nginx 配置登陆密码_nginx账号密码登录

nginx账号密码登录


基于rocky 8.4

安装依赖环境

  1. 安装nginx
dnf install nginx -y
  • 1

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

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

在这里插入图片描述

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

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

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

修改用户和密码

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

配置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 {
        }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

启动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]#

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/732432
推荐阅读
相关标签
  

闽ICP备14008679号