影藏版本号,避免安全漏洞泄露
nginx隐藏版本号的方法
实验步骤:
1、手工编译Nginx
2、修改配置文件隐藏源码
[root@localhost nginx-1.12.2]# curl -I http://192.168.200.80 // 访问网站使用curl -I 命令检测,Curl -I 查看头部信息
HTTP/1.1 200 OK
Server: nginx/1.12.2 //可以看到版本号
Date: Mon, 10 Aug 2020 07:15:49 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:09:11 GMT
Connection: keep-alive
ETag: "5f30f297-264"
Accept-Ranges: bytes
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]# vim nginx.conf //修改配置文件参数
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; //加这句话,关闭版本号
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17476/nginx: master
[root@localhost conf]# curl -I http://192.168.200.80 // 访问网站使用curl -I 命令检测,Curl -I 查看头部信息
HTTP/1.1 200 OK
Server: nginx //版本号被隐藏
Date: Mon, 10 Aug 2020 07:20:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:09:11 GMT
Connection: keep-alive
ETag: "5f30f297-264"
Accept-Ranges: bytes
1、Nginx源码文件/usr/src/nginx-1.12.0/src/core/nginx.h
包含版本信息,可以随意设置置
2、重新编译安装,隐藏版本信息
示例
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt/
[root@localhost opt]# rz -E //通过xshell拖入文件
rz waiting to receive.
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel //安装编译环境
[root@localhost opt]# useradd -M -s /sbin/nologin nginx //创建管理用户
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.2]# cd src/
[root@localhost src]# ls
core event http mail misc os stream
[root@localhost src]# cd core/
[root@localhost core]# ls
nginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
nginx.h ngx_file.c ngx_palloc.c ngx_rwlock.h
ngx_array.c ngx_file.h ngx_palloc.h ngx_sha1.c
ngx_array.h ngx_hash.c ngx_parse.c ngx_sha1.h
ngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.c
ngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.h
ngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.c
ngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.h
ngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.c
ngx_connection.c ngx_log.c ngx_queue.c ngx_string.c
ngx_connection.h ngx_log.h ngx_queue.h ngx_string.h
ngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.c
ngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.h
ngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.c
ngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.h
ngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.c
ngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.h
ngx_crypt.h ngx_open_file_cache.c ngx_resolver.c
ngx_cycle.c ngx_open_file_cache.h ngx_resolver.h
[root@localhost core]# vim nginx.h
#define NGINX_VERSION "1.1.1" //将编译文件信息修改成1.1.1
[root@localhost core]# cd ../..
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# 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 nginx-1.12.2]# vim /etc/init.d/nginx //给service管理
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reloard)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0{start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24806/nginx: master
[root@localhost nginx-1.12.2]# curl -I 192.168.200.70
HTTP/1.1 200 OK
Server: nginx/1.1.1 //更改的版本信息
Date: Mon, 10 Aug 2020 07:37:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:29:42 GMT
Connection: keep-alive
ETag: "5f30f766-264"
Accept-Ranges: bytes
1、Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
2、Nginx默认使用nobody用户账号与组账号
3、修改的方法;
1、创建用户账号与组账号,如nginx
2、编译安装时-user与–group指定Nginx服务的运行用户与组账号
[root@www nginx-1.12.0]# ./configure \
接方法一实验
[root@localhost conf]# id nobody
uid=99(nobody) gid=99(nobody) 组=99(nobody)
[root@localhost conf]# vim nginx.conf
user nginx nginx; 设置用户和组
[root@localhost conf]# 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 conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# ps aux | grep nginx
root 73267 0.0 0.0 20544 612 ? Ss 15:51 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 73268 0.0 0.0 23072 1384 ? S 15:51 0:00 nginx: worker process
root 73283 0.0 0.0 112724 984 pts/1 S+ 15:52 0:00 grep --color=auto nginx
1、当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
2、一般针对静态网页设置,对动态网页不设置缓存时间
配置Nginx网页缓存时间2-2
●修改配置文件,在http段、 或者server段、 或者location段加入对特定内容的过期参数
■示例
●修改Nginx的配置文件,在location段加入expires 参数
location ~ \.(gifljpgljiepglpng|bmp lico)$ {
root html;
expires 1d;
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jpeg|png|ico)$ {
root html;
expires 1d;
}
[root@localhost conf]# cd ..
[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 index.html
[root@localhost html]# rz -E //xshell插入图片
rz waiting to receive.
[root@localhost html]# ls
50x.html game.jpg index.html
[root@localhost html]# vim index.html
"game.jpg"/>
[root@localhost conf]# service nginx start
[root@localhost conf]# 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
打开win10操作系统
登录192.168.200.80
1、随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注Nginx日志文件
2、太大的日志文件对监控是-一个大灾难
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d") //设置日期名称
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path //自动创建日志目录
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d //分割日志
kill -HUP $(cat $pid_path) //生成新日志
find $logs_path -mtime +30 | xargs rm -rf //删除30天前的日志
[root@localhost opt]# chmod +x fenge.sh
[root@localhost opt]# cd /var/log/
[root@localhost log]# ls //查看没有日志
anaconda firewalld ntpstats speech-dispatcher vmware-vgauthsvc.log.0
audit gdm pluto spooler vmware-vmsvc.log
boot.log glusterfs ppp sssd vmware-vmusr.log
btmp grubby_prune_debug qemu-ga swtpm wpa_supplicant.log
chrony lastlog rhsm tallylog wtmp
cron libvirt sa tuned Xorg.0.log
cups maillog samba vmware-network.1.log Xorg.9.log
dmesg messages secure vmware-network.log yum.log
[root@localhost log]# cd /opt/
[root@localhost opt]# ./fenge.sh //执行脚本
[root@localhost opt]# ls /var/log/nginx/ //自动创建了日志文件
test.com-access.log-20200809
[root@localhost opt]# crontab -e //创建计划性周期任务
0 1 * * * /opt/fenge.sh
1、为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
2、超时参数
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 100;
client_header_timeout 80; //等待客户端发送请求的超时时间 超时会发送408错误
client_body_timeout 80; //设置客户端发送请求体超时时间
[root@localhost opt]# service nginx stop
[root@localhost opt]# service nginx start
1、在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
2、更改进程数的配置方法
1、修改配置文件的worker_ processes参数
[root@localhost opt]# cat /proc/cpuinfo | grep -c "physical" //查看物理核心数
8
root@localhost opt]# ps aux | grep nginx
root 75577 0.0 0.0 20544 612 ? Ss 19:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 75578 0.0 0.0 23072 1388 ? S 19:10 0:00 nginx: worker process
root 75714 0.0 0.0 112724 988 pts/2 S+ 19:25 0:00 grep --color=auto nginx
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
[root@localhost opt]# service nginx stop
[root@localhost opt]# service nginx start
[root@localhost opt]# ps aux | grep nginx
root 75809 0.0 0.0 20544 616 ? Ss 19:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 75810 0.0 0.0 23072 1392 ? S 19:29 0:00 nginx: worker process //worker
nginx 75811 0.0 0.0 23072 1392 ? S 19:29 0:00 nginx: worker process //worker
nginx 75812 0.0 0.0 23072 1392 ? S 19:29 0:00 nginx: worker process //worker
nginx 75813 0.0 0.0 23072 1392 ? S 19:29 0:00 nginx: worker process //worker
root 75825 0.0 0.0 112724 984 pts/2 S+ 19:29 0:00 grep --color=auto nginx
vim /usr/local/nginx/conf/nginx.conf
gzip on;#开启gzip压缩功能
gzip_ min length 1k;#压缩阈值
gzip buffers 4 16k;#buffer大小为4个1 6k缓冲区大小
gzip http. version 1.1; #压缩版本
gzip_ comp level 6;#压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快
gzip_ disable “MSIE [1-6].”; #配置禁用gzip条件,支持正则,表示ie6以下不启用gzip
gzip. vary on; #选择支持very header可以让前端的缓存服务器缓存经过gzip压缩的页面
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png im
age/gif application/xml text/javascript application/x-httpd-php application/javascript applic
ation/json;
gzip_disable "MSIE[1-6]\.";
gzip_vary on;
[root@localhost opt]# 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 local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost local]# cd 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 game.jpg index.html
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start
进入win10打开浏览器,打开监控软件,输入192.168.200.80,打开监控软件,可以看到压缩开启
实验环境:192.168.200.70官网服务器,192.168.200.80盗链网站
1、配置192.168.200.70官网服务器
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# cd /etc/init.d/
[root@localhost init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reloard)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0{start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost init.d]# 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 init.d]# service nginx start
[root@localhost init.d]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24849/nginx: master
[root@localhost init.d]# 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 index.html
拖入图片
[root@localhost html]# ls
50x.html game.jpg index.html
[root@localhost html]# vim index.html
"game.jpg"/>
[root@localhost html]# yum -y install bind
[root@localhost html]# vim /etc/named.conf
options {
listen-on port 53 { any; };
allow-query { any; };
[root@localhost html]# vim /etc/named.rfc1912.zones
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
[root@localhost html]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
www IN A 192.168.200.70
[root@localhost named]# systemctl start named
打开win10计算机,然后把dns改成192.168.200.70,打开浏览器登录ip地址192.168.200.70,成功现实网页
2、制作192.168.200.80盗链网站
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.200.80:80
#Listen 80
ServerName www.test.com:80
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
this is test web</h1>
"http://www.kgc.com/game.jpg"/>
[root@localhost html]# echo "nameserver 192.168.200.70" > /etc/resolv.conf
[root@localhost html]# systemctl start httpd
用win10浏览访问192.168.200.80,能正常显示
3、在192.168.200.70服务器上操作
[root@promote ~]# cd /usr/local/nginx/
[root@promote nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@promote conf]# vim nginx.conf
location ~*\.(jpg|gif|swf)$ {
valid_referers none blocked *.kgc.com kgc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;
}
}
[root@promote conf]# 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@promote conf]# cd ..
[root@promote nginx]# cd html/
[root@promote html]# rz -E
rz waiting to receive.
[root@promote html]# ls
50x.html error.png game.jpg index.html
[root@promote html]# service nginx restart
WIN10浏览器访问192.168.200.80,出现的是error的图片
Nginx 的PHP解析功能实现是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整。
fpm参数优化
vi php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max children= 20#static模式下空闲进程数上限,大于下面的值
pm.start servers= 5 #动态方式下默认开启的进程数,在最小和最大之间
pm.min_ spare_ servers = 2 #动态方式下最少空闲进程数
pm.max_ spare_ servers = 8 #动态方式下最大空闲进程数
1、手工编译安装lnmp
2、fpm优化
[root@localhost conf]# cd /usr/local/php/etc
[root@localhost etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20
pm.start.servers=5
pm.min_ spare_ servers = 2
pm.max_ spare_ servers = 8