centos7如何安装nginx并配置密码访问?

安装

wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
yum install nginx-1.20.2-1.el7.ngx.x86_64.rpm
systemctl enable nginx
systemctl start nginx

配置密码访问

yum  -y install httpd-tools
htpasswd -c /etc/nginx/conf.d/password username

要对整个站点开启验证,需在配置文件中的server加上认证配置
auth_basic 和 auth_basic_user_file

server {
  listen 80;
  server_name  localhost;
  # ...
  
  auth_basic "请输入用户和密码"; # 验证时的提示信息
  auth_basic_user_file /etc/nginx/conf.d/password; # 认证文件

  location / {
      root   /var/www;
      index  index.html index.htm;
  }
  # ...
}

回复

我来回复
  • 暂无回复内容