一、Nginx简介与部署
(1)简介:
Nginx (engine x) 是一个高性能的HTTP(解决C10k的问题)和 反向代理服务器,也是一个IMAP/POP3/SMTP服务器。
(2)部署Nginx-Yum源
1.进入nginx官网找到下载,下载Stable version(最新稳定版,生产环境建议使用的版本)
2.下完之后回到主页面找到documentation(文档),点进去然后点击install nginx(安装nginx),点进去之后点击packas(包),往下拉看到这个界面:

3.进入到 cd /etc/yum.repores.d/ 里面,创建一个nginx.repo的文件,把上面官网第二个箭头指的内容加入到nginx,repo文件内保存退出。
4.使用yum安装上epel源:

安装完成epel源之后安装nginx:

安装完成之后:
systemctl stop firewalld #临时关闭防火墙
setenforce 0 #临时关闭selinux
systemctl start nginx #启动nginx
启动之后,就可以在浏览器里访问页面了。
二、Nginx配置文件
在命令终端执行rpm -ql nginx 就会显示所有配置文件:

三、Nginx编译参数
在命令终端执行nginx -V 就会显示所有模块:




四、Nginx基本配置文件信息
vim /etc/nginx/nginx.conf 进入到主配置文件中:

vim /etc/nginx/conf.d/defaulet.conf 进入到子配置文件:

五、Nginx日志log
(1)日志配置
相关指令:
ngx_http_log_module #日志模块名称
log_format #日志格式
access_log #访问日志
error_log #错误日志
open_log_file_cache #日志缓存
log_format :



access_log:

(2)日志分析
统计2017年九月五日八点到九点的pv量:

统计2017年9月5日 一天内访问最多的10个IP(ip top10):

统计2017年9月5日 每个URL访问内容总大小($body_bytes_sent):

统计2017年9月5日 IP访问状态码为404及出现次数($status):

统计前一分钟的PV量:

六、Nginx Web模块
1.连接状态模块 stub_status_module
目的:展示用户和nginx链接数量等信息
(1)查询模块是否已经安装
# nginx -V 2>&1 | grep stub_status
(2)启动状态模块
配置状态模块:
# vim /etc/nginx/conf.d/default.conf
在server框架内添加如下内容:
# location /nginx_status {
stub_status;
allow all;
}
保存退出,重启服务
#systemctl restart nginx
访问页面就会显示:


2.随机主页模块 random_index_module
目的:将主页设置成随机页面,是一种微调更新机制
启动随机主页:
创建主页目录:# mkdir /wangning
创建随即多个主页:# mkdir /wangning/{wannging.html ,wangning1.html ,wannging2.html,wangning3.html}
不同的页面里面都输入不同的内容
配置启动模块 # vim /etc/nginx/conf.d/default.conf
# server{
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
root /app;
random_index on;
}
}
完成之后 重启服务 刷新页面观察变化
3.替换模块 sub_module
目的:网页内容替换 如果我们用模板生成网站的时候,因为疏漏或者别的原因造成代码不如意,但是此时因为文件数量巨大,不方便全部重新生成,那么这个时候我们就可以用此模块来暂时实现纠错。另一方面,我们也可以利用这个实现服务器端文字过滤的效果。
启动替换1:
# vim /etc/nginx/conf.d/default.conf
# sub_filter nginx 'wangning';
# sub_filter_once off;
# location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
# }
注意是在server模块里面添加 刷新页面观察变化 注意sub_filter_once off; 这是全局替换 如果改成sub_filter_once on;就会只改变一处内容

启动替换模块2
首先准备替换的java脚本: #vim /usr/share/nginx/html/hellowangning.js
# function Hello() {alert("Hello WangNing")}
然后,在主页body标签里面插入按钮 #vim /usr/share/nginx/html/index.html
#<input type="button" onclick="Hello()" value="say hellp" />
启动替换:# vim /etc/nginx/nginx.conf
#sub_filter </head> '</head><script type="text/javascript" src="hellowangning.js"></script>';
#sub_filter_once off;
重启服务,观察页面

4.防盗链功能
1.首先搭建两个网站 wangning.com wangning1.com ,在wangning.com网站主页中添加一张图片:#<img src='1.jpg' />
2.在wangning1.com主页中添加主页一的地址<img src='http://wangning.com网站的域名或者地址/1.jpg' /> ,盗用wangning.com 的链接
3.访问两个网站的主页就会都显示图片

4.访问wangning1.com的日志发现日志正常,然而访问wangning.com的日志观察referer字段发现被盗链了
5.在wangning.com网站中添加防盗链功能:
#vi /etc/nginx/conf.d/wangning.conf
#location / {
# root /wangning.com;
# index index.html index.html
# valid_referers none blocked *.wangning.com;
# if ($invalid_referer) {
# return 403;
# }
#}
启用之后,重启服务
再次访问wangning1.com发现盗链失败

七.Nginx访问限制
目的:服务器经常会遇恶意的攻击使得服务器的流量异常负载过大带宽浪费等,从而影响业务,所以要限制用户的连接次数并发次数
模块:ngx_http_limit_conn_module ngx_http_limit_req_module
启动限制:vi /etc/nginx/nginx.conf
#http {
#limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
#}
#server {
#location / {
# ...
#limit_conn conn_zone 1;
#}
#}
重启服务并测试:yum -y install httpd-tools
#ab -n 100 -c 10 http://wangning.com/

模块二:ngx_http_limit_req_module
启动限制:vi/etc/nginx/nginx.conf
# http {
# limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; 定
# server {
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# limit_req zone=req_zone; 引用
#limit_req zone=req_zone burst=5;
#limit_req zone=req_zone burst=5 nodelay;
# }
# }
#}

八.Nginx访问控制
目的:就是控制某些恶意主机不让其访问,或者允许哪些主机让其访问
模块:ngx_http_access_module
启动访问控制:vim /etc/nginx/conf.d/default.conf
# server {
# allow 10.18.45.65;
# allow 10.18.45.181;
# deny all;
# }
重启服务并测试,发现除了允许的主机可以访问其余的主机都不能访问了:

若有问题咨询欢迎大家联系我:
邮箱:Wangmouren1997@163.com
QQ:1586787265
网友评论