nginx常用模块

[root@web01 ~]# nginx -V	#显示nginx还可以安装的模块(不显示已经安装过的)

syntax		:语法
default		:默认
context		:内容
location	:位置
autoindex	:自动索引

#模仿阿里云镜像站,做自己的nginx镜像站(server标签里面的/ ,代表站点目录的意思)
/centos/7/os/x86_64/Packages/
[root@web01 /dir]# mkdir /dir/centos/7/os/x86_64/Packages/ -p

#查看nginx某一个模块有没有安装,过滤不到的话就是安装过的
[root@web01 ~]# nginx -V 2>1.txt
[root@web01 ~]# grep autoindex 1.txt

autoindex和index必须要指定一个,一个都不指定的话就报错(403),nginx默认无论如何都先找index模块
如果只设置一个autoindex的话,相要autoindex模块生效,那么站点目录下面不能有index.html或者index.htm
只要站点目录下有index.html或者index.htm,那么就显示html页面

IP访问nginx默认显示/usr/share/nginx/html/index.html,配置的对应IP的index.html才显示别的html
当配置了域名之后,如果浏览器访问返回的是默认页面,那么说明自定义html没有生效
default.conf控制nginx默认反馈

防火墙只是防,只会拦截外来的请求,不限制怎么出去

域名解析,谁用谁解析,哪个系统用,就在哪个系统解析,域名的作用就是方便人们的记忆

mv 1.txt{,.conf}
cp 1.txt{,.bak}
cp *.txt{,.conf}
cp *{,.conf}

模块就是写在配置文件中,让网站实现不同的功能的配置,常用的模块有,index,autoindex,size,locatime

index 可以配置在http层,server层,location层,一般配置在location层

wget 下载的时候小心,有时候要做域名解析

一个网站可以设置多个字符集,用‘,’隔开
gbk是windows的字符集
charset utf-8,gbk;

nginx错误日志才会显示绝对路径,正常的访问日志只会显示 '斜杠'

autoindex模块,autoindex指令

也可以叫ngx_http_autoindex_module

1.手写nginx配置文件,#开关自如
vim /etc/nginx/conf.d/auto_index.html 
server {
        listen 80;
        server_name www.abc.com;

        location / {
                root /dir;
                autoindex on;
        }
}

2.配置域名解析
ping www.abc.com
或者再次打开本地hosts文件查看是否解析

3.创建站点目录
mkdir /dir

4.创建指定后缀的文件,并写入内容
echo 哈哈 > /dir/base/1.css

vim /etc/nginx/mime.types	#查看nginx默认允许直接查看的文件扩展名

autoindex_exact_size指令

on :指定是在目录列表中输出确切的文件大小,还是四舍五入为千字节,兆字节和千兆字节

off :显示出文件的大小(单位是b/kb/mb,自动转化为适应单位)

只显示文件,不显示目录?

vim /etc/nginx/conf.d/auto_index.html 
server {
        listen 80;
        server_name www.abc.com;
        location / {
        charset utf-8,gbk;
        autoindex_exact_size off;
                root /dir;
                autoindex on;
        }
}

autoindex_localtime on 指令

默认显示格林时间(on)(本地时间-11),指定显示当地时区时间(off),这个时间最终显示文件的状态时间

vim /etc/nginx/conf.d/auto_index.conf
server {
        listen 80;
        server_name www.abc.com;
        location / {
                charset utf-8,gbk;
                autoindex_exact_size off;
                autoindex_localtime on;
                root /dir;
                autoindex on;
        }
}

index模块(优先级高)

vim /etc/nginx/conf.d/auto_index.html 	#测试指令优先级
server {
        listen 80;
        server_name www.abc.com;

        location / {
                root /dir;
                autoindex on;
                index index.html;
        }
}

#编辑HTML文件,加入自定义元素
vim index.html
<html>
    <meta charset="utf-8"
    <head>
        <title>哈哈哈</title>
    </head>
    <body>
        <h1>美丽</h1>
        <a href="http://www.baidu.com">百度
        </a>
    </body>
</html>

访问一个网站里面的反馈,防火墙,状态码,目录结构,文件或者HTML页面

配置ngx_http_stub_status_module模块

这个连接是一个长连接,同区域网浏览器输入可以直接查看(速度快)

使用alias指定站点目录的话,location的作用就像是标签,浏览器显示的就是站点目录下的内容,location的作用也只是跳转

使用root指定站点目录的话,浏览器显示的是站点目录下的 标签下的 内容

location = /basic_status 这个location被占用,访问该location将会显示指定的页面

长连接的特点就是,建立一次连接,发起多次请求的时候使用缓存

1.配置status模块
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.syy1.com;
        location / {
                root /code/syy1;
                index index.html;
        }

        location /o {
        alias /code;
        autoindex on;
        }


        location = /basic_status {
            stub_status;
		}
}

2.浏览器输入 http://www.syy1.com/basic_status
Active connections: 2 
server accepts handled requests
 196 196 283 
Reading: 0 Writing: 1 Waiting: 1 

活动连接:2
服务器  接受  处理  请求
        196  196   283
正在读取:0正在写入:1正在等待:1
#长连接中,客户端请求,服务器响应,反馈较上一次变化的数据,如果数据内容不变,客户端直接使用浏览器缓存
较短的长连接可导致等待数量增多


#修改长连接的超时时间(可以理解为长连接的存活时间,超时重新连接)
vim /etc/nginx/nginx.conf 
#长链接的超时时间
    keepalive_timeout  0;
    
[root@web01 ~]# curl http://www.syy1.com/basic_status
Active connections: 1 
server accepts handled requests
 282 282 461 
Reading: 0 Writing: 1 Waiting: 0
[root@web01 ~]# curl -s http://www.syy1.com/basic_status|awk 'NR==3{print $3}'
463
curl 一次也是请求一次

ngx_http_auth_basic_module模块

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.syy1.com;
        location / {
                root /code/syy1;
                index index.html;
                auth_basic           "aaa";
                auth_basic_user_file /code/htpasswd;
        }
}

2.本地域名解析

3.创建站点目录

4.创建index.html文件,并且写入内容

5.在指定的密码文件中 写入用户名和密码,然后加密(不能手写密码文件)
[root@web01 /code]# htpasswd -b -c htpasswd syy 1
Adding password for user syy
[root@web01 /code]# cat htpasswd 
syy:$apr1$Syj2XZuM$Hzijmo/8HkJdKJiROOiX/0

6.浏览器登录(输入用户名和密码)

#设置指定的网段免密登录,别的网段密码登录,指定IP不能登录(或者说剩下的所有用户)
vim /etc/nginx/conf.d/syy1.conf +17
server {
        listen 80;
        server_name www.syy1.com;
        location / {
                root /code/syy1;
                index index.html;
                auth_basic           "aaa";
                auth_basic_user_file /code/htpasswd;
        }

        location /status {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
}
}

原文地址:https://www.cnblogs.com/syy1757528181/p/12909797.html