目录
1、隐藏版本号
查看版本号
隐藏版本号
修改配置文件隐藏版本号
修改源码文件,重新编译安装
2、修改用户与组
3、缓存时间
4、日志切割
5、连接超时
6、更改进程数
7、配置网页压缩
8、配置防盗链
配置web源主机(192.168.52.120 www.my.com)域名映射关系
配置盗链网站主机(192.168.52.200 www.qyd.com)
在web源主机(192.168.52.120 www.my.com)中配置防盗链
使用盗链主机访问网页
1、隐藏版本号
以在 CentOS 中使用命令 curl -I http://192.168.52.120 显示响应报文首部信息。
查看版本号
- curl -I http://192.168.52.120
- 浏览器查看
输入IP地址后进入首页右击鼠标点击查看元素,点击网络
隐藏版本号
修改配置文件隐藏版本号
vim /usr/local/nginx/conf/nginx.conf
------------------------------------------------------
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #添加,关闭版本号
......
}
---------------------------------------------------
nginx -t #检查语法问题
systemctl restart nginx #重启服务
curl -I http://192.168.52.120 #查看版本号
vim /usr/local/nginx/conf/nginx.conf
------------------------------------------------------
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #添加,关闭版本号
......
}
---------------------------------------------------
nginx -t #检查语法问题
systemctl restart nginx #重启服务
curl -I http://192.168.52.120 #查看版本号
关闭版本号
重启并查看
修改源码文件,重新编译安装
方法二:修改源码文件,重新编译安装
vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1" #修改版本号 将1.12.0版本改为1.1.1
#define NGINX_VER "IIS" NGINX_VERSION #修改服务器类型 将nginx改为IIS
------------------------------------------------------------------------------
#重新配置安装
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
--------------------------------------------------------
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on; #将版本号打开
......
}
--------------------------------------------------------
systemctl restart nginx #重启服务
curl -I http://192.168.80.10 #查看服务名称
配置安装
打开版本号
重启服务并查看版本号
版本信息被更改
2、修改用户与组
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #取消注释,修改用户为 nginx ,组为 nginx
重启并查看
systemctl restart nginx
ps aux | grep nginx
主进程由root创建,子进程由nginx创建
nginx有两种进程有master进程 与worker进程
重启并查看
3、缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度,一般针对静态网页设置,对动态网页不设置缓存时间
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
......
location / {
root html;
index index.html index.htm;
}
location ~ .(gif|jpg|jepg|png|bmp|ico)$ { #加入新的 location,以图片作为缓存对象
root html;
expires 1d; #指定缓存时间,1天
}
......
}
}
设置主页图片
[root@localhost ~]# cd /usr/local/nginx/ [root@localhost nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost nginx]# cd html/ [root@localhost html]# ls 50x.html bbs index.html index.php [root@localhost html]# rz -E rz waiting to receive. [root@localhost html]# ls 50x.html bbs index.html index.php tu.jpg
编辑主页文件
[root@localhost html]# vim index.html -------------------------------------------------------------------------Welcome to nginx! Welcome to nginx!If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
重启并测验
#重启 [root@localhost html]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost html]# systemctl restart nginx
输入http://192.168.52.120并查看响应头
4、日志切割
将很多日志进行切割,可以增加工作效率,方便管理
vim /opt/fenge.sh
#!/bin/bash
# Filename: fenge.sh
#显示前一天的时间
DAY=$(date -d "-1 day" +%Y%m%d)
#定义日志存放目录的路径
LOG_PATH="/var/log/nginx"
#定义Nginx PID文件路径
PID_PATH="/usr/local/nginx/logs/nginx.pid"
#判断日志存放目录是否存在,如果不存在则创建日志文件目录
[ -d $LOG_PATH ] || mkdir -p $LOG_PATH
#移动并重命名日志文件
mv /usr/local/nginx/logs/access.log ${LOG_PATH}/my.com-access.log-$DAY
#在Nginx目录下重建新日志文件
kill -USR1 $(cat $PID_PATH)
#删除30天之前的日志文件
find $LOG_PATH -mtime +30 -exec rm -rf {} ;
#find $LOG_PATH -mtime +30 | xargs rm -rf
定时执行
[root@localhost ~]# crontab -e no crontab for root - using an empty one 00 00 * * * /opt/fenge.sh
[root@localhost ~]# bash /opt/fenge.sh [root@localhost ~]# ls /var/log/nginx/ my.com-access.log-20220511
5、连接超时
HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自同一客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。
keepalive_timeout
指定KeepAlive的超时时间(timeout)。指定每个TCP连接最多可以保持多长时间,服务器将会在这个时间后关闭连接。 Nginx的默认值是65秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为0,就禁止了keepalive 连接。
第二个参数(可选的)指定了在响应头Keep-Alive:timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,Nginx 不会发送 Keep-Alive 响应头。
client_header_timeout
客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。
client_body_timeout
指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)。
vim /usr/local/nginx/conf/nginx.conf
http {
......
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
......
}
#重启服务
systemctl restart nginx
重启并查看
输入网址192.168.52.120
6、更改进程数
在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
cat /proc/cpuinfo | grep -c "physical id" #查看cpu核数 ps aux | grep nginx #查看nginx主进程中包含几个子进程 ------------------------------------------------------------------------ vim /usr/local/nginx/conf/nginx.conf worker_processes 4; #修改为核数相同或者2倍 worker_cpu_affinity 0001 0010 0100 1000; #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000 #1所在的位置代表cpu的ID号 ----------------------------------------------------------- 重启服务 systemctl restart nginx
重启并查看
7、配置网页压缩
Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装,可在配置文件中加入相应的压缩功能参数对压缩性能进行优化。
查看是否有压缩功能
加入压缩参数
vim /usr/local/nginx/conf/nginx.conf
http {
......
gzip on; #取消注释,开启gzip压缩功能
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 64k; #压缩缓冲区,大小为4个64k缓冲区
gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 6; #压缩比率
gzip_vary on; #支持前端缓存服务器存储压缩页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; #压缩类型,表示哪些网页文档启用压缩功能
......
}
将图片文件传到/usr/local/nginx/html目录下,并在网页中添加图片
vim index.html ...... #网页中插入图片
重启服务并查看
在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络 ---> 选择 HTML、WS、其他
访问 http://192.168.52.120 ,双击200响应消息查看响应头中包含 Content-Encoding: gzip
8、配置防盗链
web源主机(192.168.52.120 www.my.com)
配置盗链主机 (192.168.52.200 www.qyd.com)
配置web源主机(192.168.52.120 www.my.com)域名映射关系
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.52.120 www.my.com
配置盗链网站主机(192.168.52.200 www.qyd.com)
安装apache服务并配置盗链主页
[root@localhost ~]# yum install httpd -y 安装apache服务 [root@localhost ~]# cd /var/www/html [root@localhost html]# ls [root@localhost html]# vim index.html --------------------------------------------- 是谁在盗链 #图片链接地址通过浏览器访问http://www.my.com 所得图片地址
设置域名映射
[root@localhost ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.52.120 www.my.com 192.168.52.200 www.qyd.com
重启服务并测试
[root@localhost ~]# systemctl restart httpd
在防盗链主机(192.168.52.200 )的浏览器中输入盗链主机的域名,显示如下图片
在web源主机(192.168.52.120 www.my.com)中配置防盗链
将盗链图片放入web源主机中
[root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# ls 50x.html bbs index.html index.php tu.jpg [root@localhost html]# rz -E rz waiting to receive. [root@localhost html]# ls 50x.html bbs index.html index.php tu.jpg tu2.png
配置防盗链
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
......
location ~* .(jpg|gif|swf)$ { #因为自己的盗链图片为png结尾所以此处不设置png
valid_referers none blocked *.my.com my.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.my.com/tu2.png;
}
}
......
}
}
~* .(jpg|gif|swf)$ :这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的文件;
valid_referers :设置信任的网站,可以正常使用图片;
none:允许没有http_refer的请求访问资源(根据Referer的定义,它的作用是指示一个请求是从哪里链接过来的,如果直接在浏览器的地址栏中输入一个资源的URL地址,那么这种请求是不会包含 Referer 字段的),如 http://www.my.com/tu.jpg
我们使用 http://www.my.com 访问显示的图片,可以理解成 http://www.my.com/tu.jpg 这个请求是从 http://www.my.com 这个链接过来的。
blocked:允许不是http://开头的,不带协议的请求访问资源;
*.my.com:只允许来自指定域名的请求访问资源,如 http://www.my.com
if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为true,则执行后面的操作
检查语法并重启
[root@localhost ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# systemctl restart nginx



