【03】Nginx的配置文件介绍

NGINX的配置二

1.1 Nginx的四层访问控制

该功能需要基于模块ngx_http_access_moudle实现

可以通过匹配客户端源IP进行访问的限制。

location /abc {
    
    
	alias	/data/nginx/html/pc;
	index	index.html;
	#deny	192.168.33.1;
	allow	192.168.33.0/24;
	deny	all;
	# 允许小部分一般放前面
}
# 一般在防火墙上实现这个功能。

1.2 Nginx账户认证功能

htpasswd

htpasswd指令用来创建和更新用于基本认证的用户认证密码文件。htpasswd指令必须对密码文件有读写权限,否则会返回错误码。

语法

htpasswd [ -c ] [ -m ] [ -D ] passwdfile username

htpasswd -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password

htpasswd -n [ -m | -d | -s | -p ] username

htpasswd -nb [ -m | -d | -s | -p ] username password

参数列表

选项 说明
-b 使用批处理方式,直接从命令行获取密码,不提示用户输入
-c 创建密码文件,如果文件存在,那么内容被清空重写
-n 将结果送到标准输出
-m 使用MD5加密
-s 使用crypt()加密
-p 使用文本密码
-D 从认证文件中删除用户记录
[root@localhost@~]->yum install httpd-tools -y
# htpasswd 命令在此包中
#
# 创建密码文件及创建用户密码
[root@localhost@~]->htpasswd -cbm /apps/nginx/conf/.htpasswd user1 123123
Adding password for user user1
[root@localhost@~]->htpasswd -bm /apps/nginx/conf/.htpasswd user2 123123
Adding password for user user2
#
[root@localhost@~]->cat /apps/nginx/conf/.htpasswd 
user1:$apr1$aHa5x2we$Z8F.j6ikPQAt7KiK5AHZ10
user2:$apr1$7tMlfpt6$ewI3e7hkjHQA8nJVZMu37/
#
# 在pc.conf新增location
#
 location /login {
    
    
                root    /data/nginx/html;
                index   index.html;
                auth_basic      "login password";
                auth_basic_user_file    /apps/nginx/conf/.htpasswd;
 }
# 
# 建立文件目录/data/nginx/html/login
#
[root@localhost@~]->mkdir /data/nginx/html/login
[root@localhost@~]->echo "YOU ARE HERE" > /data/nginx/html/login/index.html
#
# 测试

在这里插入图片描述

在输入user1 123123后进入该界面

在这里插入图片描述

同样的这个功能适用内部访问

1.3 自定义错误界面

# 在目录 /apps/nginx/conf/nginx.conf中有错误界面的相关配置
 error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }

# 尝试自定义
 error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
# 编辑界面
#
[root@localhost@conf]->vi ../html/50x.html 
[root@localhost@conf]->nginx -s reload
#

在这里插入图片描述

在www.flamenca.net中

在这里插入图片描述

1.4 自定义访问日志

访问日志分为:

  • access.log
  • error.log
# 在pc.conf中定义
access_log      /data/nginx/logs/www.flamenca.net_access.log;
error_log       /data/nginx/logs/www.flamenca.net_error.log;
#
# 新建文件夹
[root@localhost@conf]->mkdir /data/nginx/logs
[root@localhost@conf]->nginx -s reload
# 开始测试
# access.log
[root@localhost@~]->tail -fv /data/nginx/logs/www.flamenca.net_access.log 
==> /data/nginx/logs/www.flamenca.net_access.log <==
192.168.33.1 - user1 [12/Nov/2020:14:20:13 +0800] "GET /login/ HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36"


# error.log
[root@localhost@~]->tail -fv /data/nginx/logs/www.flamenca.net_error.log 
==> /data/nginx/logs/www.flamenca.net_error.log <==
2020/11/12 14:21:32 [error] 6034#0: *20 "/data/nginx/html/flamenca/l1213n/index.html" is not found (2: No such file or directory), client: 192.168.33.1, server: www.flamenca.net, request: "GET /l1213n/ HTTP/1.1", host: "www.flamenca.net"
2020/11/12 14:21:36 [error] 6034#0: *20 "/data/nginx/html/flamenca/l12231n/index.html" is not found (2: No such file or directory), client: 192.168.33.1, server: www.flamenca.net, request: "GET /l12231n/ HTTP/1.1", host: "www.flamenca.net"

1.5 检测文件是否存在

try_flies 会按顺序检测文件是否存在,如果找不到,会进行一个内部重定向,通过URI来指向,最后的URI必须存在,否则会返回500错误;

location /hello {
    
    
                root    /data/nginx/html;
                index   index.html;
                try_files       $uri $uri/index.html $uri.html =666;
                #try_files       $uri $uri/index.html $uri.html /hello/index.html;
        }

# try
# 新建文件夹/data/nginx/html/hello
# 
[root@localhost@conf]->mkdir /data/nginx/html/hello
#
[root@localhost@conf]->cat /data/nginx/html/hello/index.html
<h1> Defult Index Page </h1>
# 测试访问 www.flamenca.net/hello/xxx.html


####
# 测试2
location /hello {
    
    
                root    /data/nginx/html;
                index   index.html;
                #try_files       $uri $uri/index.html $uri.html =666;
                try_files       $uri $uri/index.html $uri.html /hello/index.html;
        }

在这里插入图片描述

测试2

在这里插入图片描述

在这里插入图片描述

关键点1:按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理

关键点2:查找路径是按照给定的root或alias为根路径来查找的

关键点3:如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配

关键点4:如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

猜你喜欢

转载自blog.csdn.net/FlamencaH/article/details/109644310