ngin 模块及模板

nginx碎碎念

server {
        listen 80;
        server_name www.abc.com;
        access_log /var/log/nginx/www.abc.com.log;

        location / {
        root    /dir;
        index /a.txt;
        access_log /var/log/nginx/www.abc.com.dir.log;
        }
}

nginx的源:有官网上的源,有阿里云上的源,github上面的源
下载nginx的方式,wget,yum,windows下载,不一样的下载方式的安装方法不同,相关文件的位置不同,相关文件的内容不同

nginx配置文件能够简单归纳为:
	核心层,事件层,http层
	http层包括:server层,location层
yum下载的官网nginx能够include,阿里云nginx不能够include

location / 能够认为是隐藏的
root和index在某些模块中能够省略
通常来讲root和alias必需要指定一个,不知道就会报错(403),index和autoindex也必须指定一个,不指定就报错(403)
root可使用alias代替(使用alias将少一层目录)
index可使用autoindex代替(区别是直接显示一个文件内容,显示文件或目录的层级结构)
root里面的 / 是根,别的地方的根是站点目录
index后面的文件名能够是任意的,html文件可使用绝对路径,也可使用相对路径

nginx日志老是做用于最小的做用域
access.log	该文件记录着nginx正常的访问日志和报错日志
error.log	该文件只记录着nginx报错日志


/etc/nginx/conf.d/default.conf
	该文件记录着nginx默认的信息,不过优先级低(默认的站点目录,HTML页面,error_page,location)
/usr/share/nginx/html
	这个目录是安装nginx自动生成目录,这个目录和使用root相对路径(/50.html,html/50html)有关
/usr/share/nginx/html/50x.html 
	nginx默认的50x 的反馈页面	
/usr/share/nginx/html/index.html
	下载好的nginx默认的html页面,和IP的访问好玩localhost的访问有关
/etc/logrotate.d/
	centos7中默认的日志切割文件,能够自动切割nginx,mysql,yum的日志,天周年默认不等
	
一个server表明一个网站,一个location表明一个页面的跳转
一个nginx搭载的网站里面有目录,各类后缀的文件,html页面
经过nginx不一样的模块能够给nginx增长不一样的功能,好比:显示目录的层级结构,文件的大小,状态时间,设置网站的密码,限制访问,限制并发访问

401	:认证失败

nginx模块

index模块

可使用html的语法编写html,显示美丽的html页面css

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

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

2.检查和检测,重载
nginx -t
nginx -s reload

3.建立站点目录
mkdir /dir

4.建立html文件,并写入内容

5.windows上域名解析

6.浏览器访问
F5	alt+F5	F12+network+cache	换别的浏览器

autoindex模块

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

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

2.检查和检测
nginx -t
nginx -s reload

3.建立站点目录
mkdir /dir

4.移除站点目录下的html,htm文件

5.windows上域名解析

6.浏览器访问
F5	alt+F5	F12+network+cache	换别的浏览器

添加字符集

charset utf-8,gbk;

autoindex_exact_size模块

on :显示文件的字节html

off :显示文件的经常使用单位node

只显示文件的大小,不显示目录的大小,过小的文件不会显示经常使用的单位mysql

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;
        charset utf-8,gbk;
        autoindex_exact_size off;
        
        location / {
        root    /dir;   
        autoindex on;
        }
}

2.检查和检测
nginx -t
nginx -s reload

3.建立站点目录
mkdir /dir

4.移除站点目录下的html文件

5.windows上域名解析

6.浏览器访问
F5	alt+F5	F12+network+cache	换别的浏览器

autoindex_localtime on

on :显示格林时间nginx

off :显示当地时区时间git

这个时间最终显示文件的状态时间github

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

2.检查和检测
nginx -t
nginx -s reload

3.建立站点目录
mkdir /dir

4.移除站点目录下的html文件

5.windows上域名解析

6.浏览器访问
F5	alt+F5	F12+network+cache	换别的浏览器

ngx_http_stub_status_module模块

最简单的模块,站点目录都不用配...web

这个网站可使用allow和denysql

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

        location = /basic_status {
        stub_status;
        }
}

2.检查和检测
nginx -t
nginx -s reload

3.浏览器访问
http://www.abc.com/basic_status

F5	alt+F5	F12+network+cache	换别的浏览器

#能够修改location
        location = /zt {
        stub_status;
        }

#可使用allow和deny(先写allow再写deny)
        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }

ngx_http_auth_basic_module模块

该模块放在server里或者location均可以,可是不能单独放在一个location里面vim

能够给整个网站设置密码,也能够给指定的location设置密码

该模块放在哪就是给哪一个模块设置密码,重复设置密码无心义

这个模块不能设置免密登陆

1.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;

	   location / {
        root    /dir;   
        autoindex on;
        
        auth_basic           "closed site";
        auth_basic_user_file /dir/htpasswd;
        }

        location = /zt {
        stub_status;
        }
}

2.检查和检测(#只是检查server语句语法而已,站点目录不存在的话都不会被检测出来)
nginx -t
nginx -s reload

3.建立站点目录
mkdir /dir

写在哪就是给谁设置密码,能够给网站,location,指定的html设置密码
htpasswd -b -c /dir/htpasswd syy 123

4.移除站点目录下的html文件

5.windows上域名解析

6.浏览器访问
F5	alt+F5	F12+network+cache	换别的浏览器

ngx_http_access_module

该模块放在server里或者location均可以,可是不能单独放在一个location里面

容许某一网段的的IP访问,拒绝剩下的全部用户的访问

basic和access 是两个模块,互不相关

当心allow和deny的顺序

vim /etc/nginx/conf.d/syy1.conf +13
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;
        autoindex on;

        #auth_basic           "closed site";
        #auth_basic_user_file /dir/htpasswd;

        allow 10.0.0.0/24;
        deny  all;
}

        location = /zt {
        stub_status;
        }
}
vim /etc/nginx/conf.d/syy1.conf +13
server {
        listen 80;
        server_name www.abc.com;

        location / {
        root    /dir;
        autoindex on;

        #auth_basic           "closed site";
        #auth_basic_user_file /dir/htpasswd;
}

        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }
}

#使用curl命令取出指定内容
curl http://syy:123@www.syy1.com/zt

不能这样分开写

Y5XxMQ.md.png

ngx_http_limit_conn_module模块

链接限制,限制同时最高500个链接(只对公网IP限制)

超过500个链接后,后续所有显示403

1.主配置文件
vim /etc/nginx/nginx.conf  
limit_conn_zone $binary_remote_addr zone=perip:10m; 
limit_conn_zone $server_name zone=perserver:10m;

2.从配置文件
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.syy1.com;

        limit_conn perip 3;
        limit_conn perserver 3;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;

                }
        }

3.nginx -s reload

4.浏览器 访问该server中的任意一个location

使用ab命令对某一个网站( / 或者某一个页面)进行压测

-c	:并发用户数
-n	:发送请求数(总数)

#域名必须加/

ab -c100 -n 1000 http://www.syy1.com/

ngx_http_limit_req_module 模块

请求限制,限制并发请求

并发请求返回状态码503

1.主配置文件设置限制
vim /etc/nginx/nginx.conf 
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;

2.从配置文件调用
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    
        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;
        }
}

3.浏览器测试手速
503 Service Temporarily Unavailable (503服务暂时不可用)(服务器过载)

自定义反馈状态码

自定义状态码范围(400-599),只有某些模块能够自定义状态码

1.自定义网站 单个IP'并发'请求形成的服务器过载 反馈的'状态码'
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    limit_req_status 412;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;
        }
}

2.浏览器测试手速
412 Precondition Failed

3.查看日志记录的状态码
[root@web01 ~]# tailf /var/log/nginx/access.log
10.0.0.1 - syy [19/May/2020:01:02:32 +0800] "GET / HTTP/1.1" 412 575 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

自定义状态码的 HTML页面

1.自定义网站 单个IP'并发'请求形成的服务器过载 反馈的'状态码'
vim /etc/nginx/conf.d/syy1.conf +5
server {
        listen 80;
        server_name www.syy1.com;

    limit_req zone=perip burst=1 nodelay;
    limit_req zone=perserver burst=1;
    limit_req_status 412;
    error_page 412 /syy1.html;

        location / {
                root /code/syy1;
                #index index.html;
                autoindex on;

                auth_basic           "aaa";
                auth_basic_user_file /code/htpasswd;
        }
}

2.编辑412 HTML页面
vim /code/syy1/syy1.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="=utf-8" />
        <title>标题</title>
    </head>
    <body>
        <center>
            <p>
                412 Precondition Failed
            </p>
        </center>
        <img src="https://images.cnblogs.com/cnblogs_com/syy1757528181/1767642/t_20051810211013300809_980x1200_0.jpg" alt="看什么看" width="1200" height="1000">

    </body>
</html>

3.重载nginx

4.浏览器测试手速

nginx初步优化

0.0>配置官方yum源,安装官方nginx
0.1> 启动,加入开机自启动

1.主配置文件
vim /etc/nginx/nginx.conf  
limit_conn_zone $binary_remote_addr zone=perip:10m; 
limit_conn_zone $server_name zone=perserver:10m;

limit_req_zone $binary_remote_addr zone=perip2:10m rate=1r/s;
limit_req_zone $server_name zone=perserver2:10m rate=10r/s;

2.手写server语句
vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.abc.com;
        
        access_log /var/log/nginx/www.abc.com.access.log main;
    	error_log /var/log/nginx/www.abc.com.error.log;
        
        #字符集
        charset utf-8,gbk;
        #显示经常使用单位
        autoindex_exact_size off;
        #显示当地时区时间
        autoindex_localtime off;
        #设置网站全站密码
        auth_basic           "closed site";
        auth_basic_user_file /dir/htpasswd;
        #访问限制
        deny 10.0.0.1;
        allow 10.0.0.0/24;
        deny  all;
        
        #设置状态(zt)location,加入访问控制
        location = /zt {
        stub_status;
        allow 10.0.0.0/24;
        deny all;
        }  
        
        #限制公网IP链接数
        limit_conn perip 3;
        limit_conn perserver 3;
        #限制并发请求
        limit_req zone=perip2 burst=1 nodelay;
        limit_req zone=perserver2 burst=1;
        
        #默认访问域名返回的html页面
        location / {
        root	/dir;
        index index.html;
        
        #自定义并发请求的反馈的状态码
        limit_req_status 412;
        error_page 412 /412.html;
             
        }
        #自定义站点目录
        location /abc {
        root    /dir;   
        autoindex on;
        }
        
        #匹配大小写  .*\.(svn|git|cvs)  通通拒绝  好比 tt.svn  .git .cvs
		location ~ .*\.(svn|git|cvs) {
		deny all;
        }

		# 忽略大小写匹配   .*\.htm|html|xml|shtml   ->缓存 expires
		location ~* \.(htm|html|xml|shtml)$ {
		expires 600;    #秒
         }
		location  ~* \.(js|css)$ {
	    expires 30d;      #天
        }
		location ~* \.(mp3|htc|gif|ico|png|swf|jpg|jpeg|bmp)$ {
         etag off;         #关闭校验
        }
        
        #只要出现错误则跳转对应的错误页面/dir/*.html 下面的文件
		error_page  400 = /dir/400.html;
		error_page  404 = /dir/404.html;
		error_page  500 = /dir/500.html;
		error_page  502 = /dir/502.html;
}

3.检查
nginx -t
nginx -s reload

4.建立站点目录
mkdir /dir/abc -p

5.建立html页面,加入HTML元素
vim /dir/abc/index.html

6.编辑412 HTML页面,400,404,500,502(#不作这些html页面的话,默认反馈)
vim /dir/412.html 

7.basic模块写在哪就是给谁设置密码,能够给网站,location,指定的html设置密码
htpasswd -b -c /dir/htpasswd syy 123

8.移除 想要显示下载目录的location中的 站点目录下的html文件
rm -rf /dir/abc/*.htm*

9.windows上域名解析
10.0.0.7 www.abc.com

10.浏览器访问,#测试网站(密码,访问限制,状态模块,文件单位,显示时间,并发请求,还有他的html页面,还有默认的index.html页面,相关的html页面,查看目录结构,验证location的模糊匹配)
F5	alt+F5	F12+network+cache	换别的浏览器

11.查看日志记录的状态码
[root@web01 ~]# tailf /var/log/nginx/www.abc.com.access.log
相关文章
相关标签/搜索