1.创建密码认证文件并进行授权
1)创建密码文件
yum install httpd-tools -y
htpasswd -bc /etc/nginx/htpasswd yiheng 123456
2)授权密码文件(属主属组与nginx启动用户一致即可)
chmod 400 /etc/nginx/htpasswd
chown -R nginx.nginx /etc/nginx/htpasswd
2.htpasswd用法介绍
[root@centos7 nginx]# htpasswd --help
Usage:
htpasswd [-cimB25dpsDv] [-C cost] [-r rounds] passwordfile username
htpasswd -b[cmB25dpsDv] [-C cost] [-r rounds] passwordfile username password
htpasswd -n[imB25dps] [-C cost] [-r rounds] username
htpasswd -nb[mB25dps] [-C cost] [-r rounds] username password
-c Create a new file.
创建一个新文件
-n Don't update file; display results on stdout.
不更新文件,显示双输出结果
-b Use the password from the command line rather than prompting for it.
使用密码来自命令行,相当于交互方式
-i Read password from stdin without verification (for script usage).
从标准输入读取密码而无需验证(用于脚本)。
-m Force MD5 encryption of the password (default).
强制采用 MD5 加密密码(默认)
-d Force CRYPT encryption of the password (8 chars max, insecure).
强制采用 CAYPT 加密密码(默认)
-s Force SHA-1 encryption of the password (insecure).
强制采用 SHA 加密密码
-p Do not encrypt the password (plaintext, insecure).
不加密密码(明文)
-D Delete the specified user.
删除指定用户
-v Verify password for the specified user.
验证指定用户的密码
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA-1 algorithm does not use a salt and is less secure than the MD5 algorithm.
3.nginx配置认证信息
[root@nginx ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=512m;
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
root /usr/share/nginx/html ;
proxy_cache_key $host$uri;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
auth_basic "admin auth"; #说明(内容任意写)
auth_basic_user_file /etc/nginx/htpasswd; #配置账户密码文件存放路径
}
}
}
#重启nginx即可
4.nginx安装位置
[root@nginx ~]# ll /etc/nginx/
total 80
-rw-r--r-- 1 root root 899 Sep 22 15:21 @
drwx------ 2 nginx root 6 Sep 22 17:07 cache
drwx------ 3 nginx root 15 Sep 22 17:07 cache_dir
drwxr-xr-x 2 root root 23 Sep 24 15:43 conf.d
drwxr-xr-x 2 root root 6 Oct 3 2019 default.d
-rw-r--r-- 1 root root 1077 Oct 3 2019 fastcgi.conf
-rw-r--r-- 1 root root 1077 Oct 3 2019 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Oct 3 2019 fastcgi_params
-rw-r--r-- 1 root root 1007 Oct 3 2019 fastcgi_params.default
-r-------- 1 nginx nginx 45 Sep 24 15:39 htpasswd
-rw-r--r-- 1 root root 2837 Oct 3 2019 koi-utf
-rw-r--r-- 1 root root 2223 Oct 3 2019 koi-win
-rw-r--r-- 1 root root 5231 Oct 3 2019 mime.types
-rw-r--r-- 1 root root 5231 Oct 3 2019 mime.types.default
-rw-r--r-- 1 root root 1220 Sep 24 15:51 nginx.conf
-rw-r--r-- 1 root root 3378 Sep 24 15:15 nginx.conf_bak20200924
-rw-r--r-- 1 root root 2656 Oct 3 2019 nginx.conf.default
-rw-r--r-- 1 root root 636 Oct 3 2019 scgi_params
-rw-r--r-- 1 root root 636 Oct 3 2019 scgi_params.default
drwx------ 2 nginx root 6 Sep 22 17:07 temp_dir
-rw-r--r-- 1 root root 664 Oct 3 2019 uwsgi_params
-rw-r--r-- 1 root root 664 Oct 3 2019 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Oct 3 2019 win-utf
5.nginx访问图片存放位置
[root@nginx ~]# ls -l /usr/share/nginx/html/
total 336
-rw-r--r-- 1 root root 13392 Sep 11 13:23 1.gif-rw-r--r-- 1 root root 3650 Oct 3 2019 404.html
-rw-r--r-- 1 root root 3693 Oct 3 2019 50x.html
lrwxrwxrwx 1 root root 20 Sep 22 14:40 en-US -> ../../doc/HTML/en-US
drwxr-xr-x 2 root root 27 Sep 22 14:40 icons
lrwxrwxrwx 1 root root 18 Sep 22 14:40 img -> ../../doc/HTML/img
lrwxrwxrwx 1 root root 25 Sep 22 14:40 index.html -> ../../doc/HTML/index.html
-rw-r--r-- 1 root root 368 Oct 3 2019 nginx-logo.png
lrwxrwxrwx 1 root root 14 Sep 22 14:40 poweredby.png -> nginx-logo.png
6.访问效果