Nginx伪装

dsysama

关注

阅读 105

2023-02-10

1)修改源代码程序

[root@centos01 ~]# vim /usr/src/nginx-1.16.1/src/core/nginx.h

13 #define NGINX_VERSION      "7.0"           //修改版本
14 #define NGINX_VER "IIS/" NGINX_VERSION //修改名字
22 #define NGINX_VAR "IIS" //修改名字

2)修改Nginx数据包请求头部

[root@centos01 ~]# vim /usr/src/nginx-1.16.1/src/http/ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: IIS" CRLF; //响应头部信息为IIS

3)配置Nginx

[root@centos01 ~]# cd /usr/src/nginx-1.16.1/

[root@centos01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx 
--group=nginx --with-http_stub_status_module

4)编译安装Nginx

[root@centos01 nginx-1.16.1]# make && make install

2、隐藏版本和优化管理Nginx进程用户

1)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf

user  nginx;          //管理进程用户
worker_processes 1; //启动一个进程
pid logs/nginx.pid;
events {
worker_connections 1024; //一个进程最大并发1024个访问
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens Off; //不显示版本号
server {
listen 192.168.100.10:80;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.bdqn.com.access.log;

location / {
root /www/;
index index.html index.htm;
}
}
}

2)查看Nginx进程

[root@centos01 ~]# ps -ef | grep nginx

3、设置网页缓存时间

1)网页缓存时间应用范围

server:虚拟主机配置中
http:对所有虚拟主机生效
location:对指定网站根目录生效
2)修改Nginx主配置文件支持图片缓存
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens Off;
server {
listen 192.168.100.10:80;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.bdqn.com.access.log;
location ~ \.(gif|jpg|png|jpeg|bmp|swf)$ {
root /www;
expires 1d;
}
location / {
root /www;
index index.html index.html;
}
}
}

3)设置网页加载图片

[root@centos01 ~]# cd /www/

[root@centos01 www]# rz

[root@centos01 www]# ls

index.html  logo.jpg

4)重启Nginx服务

[

root@centos01 www]# killall nginx
nginx: no process found

[root@centos01 www]# nginx

[root@centos01 www]# cd

二、日志切割和防盗

1、日志切割

1)创建存储日志切割目录

[root@centos01 ~]# mkdir /nginx_log

2)编写日志切割脚本

[root@centos01 ~]# vim /opt/nginx_log_.sh

#!/bin/bash
d=$(date -d "-1 day" "+%Y%m%d")
logs_path="/nginx_log/"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/www.bdqn.com.access.log ${logs_path}/www.bdqn.com-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 |xargs rm -rf

3)添加执行权限

[root@centos01 ~]# chmod +x /opt/nginx_log_.sh

4)执行脚本

[root@centos01 ~]# sh /opt/nginx_log_.sh

5)验证切割

[root@centos01 ~]# ls /nginx_log/

www.bdqn.com-access.log-20230209

6)编辑计划任务,两分钟切割一次

[root@centos01 ~]# crontab -e

*/2     *       *       *       *       /opt/nginx_log_.sh

2、优化Nginx保持连接

1)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 5;
server_tokens Off;
client_header_timeout 5;
client_body_timeout 5;
server {
listen 192.168.100.10:80;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.bdqn.com.access.log;
location ~ \.(gif|jpg|png|jpeg|bmp|swf)$ {
root /www;
expires 1d;
}
location / {
root /www;
index index.html index.html;
}
}
}

2)重新启动Nginx

[root@centos01 ~]# killall nginx
nginx: no process found

[root@centos01 ~]# nginx

3、配置Nginx压缩数据

1)修改Nginx主配置文件

user  nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server_tokens Off;
gzip on; //开启gzip压缩
gzip_min_length 10k; //低于10k不压缩
gzip_buffers 4 16k; //压缩数据占用内存
gzip_http_version 1.0; //压缩版本
gzip_comp_level 9; //压缩比率
gzip_types text/planin application/x-javascript text/css application/xml; //压缩文件类型
gzip_vary on; //压缩后数据支持保存到squid缓存
server {
listen 192.168.100.10:80;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.bdqn.com.access.log;
location ~ \.(gif|jpg|png|jpeg|bmp|swf)$ {
root /www;
expires 1d;
}
location / {
root /www;
index index.html index.html;
}
}
}
2)重新启动Nginx
[root@centos01 ~]# killall nginx
nginx: no process found
[root@centos01 ~]# nginx

4、配置Nginx防盗链

1)上传错误图片

[root@centos01 www]# ls
error.png index.html logo.jpg
[root@centos01 www]# cd
2)修改Nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 192.168.100.10:80;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.bdqn.com.access.log;
location ~* \.(gif|jpg|swf)$ { //匹配图片类型
valid_referers none blocked *.bdqn.com bdqn.com; //允许当前域名连接
if ($invalid_referer) {
rewrite ^/ http://www.bdqn.com/error.png; //跳转到错误图片上
}
}
location / {
root /www;
index index.html index.html;
}
}
}

3)重新启动服务

[root@centos01 ~]# killall nginx
nginx: noprocess found

[root@centos01 ~]# nginx

精彩评论(0)

0 0 举报