Nginx优化与防盗链

nginx优化:

nginx应用配置文件的优化:

 nginx的性能优化:
开启网页压缩gzip on;
页面缓存expires 缓存时间;
连接保持超时keepalive_timeout  服务端超时时间  客户端超时时间;
设置工作进程数work_processes  与服务器CPU数量相同或auto
设置工作进程连接数worker_connections    worker_rlimit_nofile
工作进程静态绑核worker_cpu_affinity
开启高效文件传输模式sendfile on;   tcp_nopush on;    tcp_nodelay on;
IO多路复用use epoll;
连接优化multi_accept on;(一个进程同时接受多个网络连接)    accept_mutex on;(以串行方式接入新连接,防止惊群现象发生)

  nginx的安全优化:       
隐藏版本号server_tokens off;      修改源代码 nginx.h
防盗链valid_referers   if ($invalid_referer) {rewrite ....}  rewrite地址重写
访问控制deny/allow
设置运行用户/组user 用户名 组名;
限制请求数limit_req_zone   limit_req
限制连接数limit_conn_zone  limit_conn
 日志分割

shell脚本 + crontab 


系统内核优化:

/etc/sysctl.conf         

内核参数配置文件

#用于解决系统存在大量TIME WAIT状态连接的问题
net.ipv4.tcp_syncookies=1表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击
net.ipv4.tcp_tw_reuse=1表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接
net.ipv4.tcp_tw_recycle=1表示开启TCP连接中TIME-WAIT sockets的快速回收
net.ipv4.tcp_fin_timeout=30修改MSL值,系统默认的TIMEOUT时间

#如果连接数本身就很多,可再优化TCP的可用端口范围,进一步提升服务器的并发能力       

net.ipv4.tcp_keepalive_time=1200#当keepalive启用时,TCP发送keepalive探测消息的频率,确认客户端是否断网
net.ipv4.ip_local_port_range=1024 65535#用于向外连接的端口范围。缺省情况下很小,为32768 60999
net.ipv4.tcp_max_syn_backlog=8192#SYN队列长度,默认为1024,加大队列长度为8192,可容纳更多等待连接的网络连接数
net.ipv4.tcp_max_tw_buckets=5000#表示系统同时保持TIME WAIT的最大数量
net.core.somaxconn=65535#一个端口能够监听的最大连接数

#如果需要IP路由转发 net.ipv4.ip_forward=1


/etc/security/limits.conf   内核资源限制文件

* softnoproc65535打开系统进程数
*hardnoproc65535
*soft  nofile 65535进程打开文件数
*hardnofile65535

                    

你用过哪些nginx模块?

http_stub_status_module访问状态统计模块
http_gzip_module  网页压缩模块
http_rewrite_moduleURL地址重写模块
http_ssl_modulehttps安全加密模块
http_auth_basic_module网页用户认证模块
http_fastcgi_modulefastcgi转发模块
http_image_filter_module图片处理模块
http_mp4/flv_modulemp4/flv视频格式模块
http_limit_req_module限制请求数模块
http_limit_conn_module限制连接数模块
http_proxy_module代理转发模块
http_upstream_*_module 负载均衡模块
stream   四层代理转发模块

   
           

一、配置Nginx隐藏版本号

  • 隐藏Nginx版本号,避免安全漏洞泄露
  • Nginx隐藏版本号的方法

#####################  快速配置一台nginx服务器   #######################


systemctl stop firewalld.service 
setenforce 0


df
mount /dev/sr0 /mnt
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

useradd -M -s /sbin/nologin nginx
cat /etc/passwd | grep nginx:


cd /opt/

[root@l1 opt]# ls
nginx-1.26.0.tar.gz  rh

tar xf nginx-1.26.0.tar.gz 


cd nginx-1.26.0/

_______________________________________________________________________________________

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

_______________________________________________________________________________________

make -j2 && make install



cd /usr/local/nginx/

ls sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx -t


cd /usr/lib/systemd/system

vim nginx.service


——————————————————————————————————————————————————————————————————————————————————

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


——————————————————————————————————————————————————————————————————————————————————


systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
netstat -lntp | grep nginx


方法一: 修改配置文件法   
                   

将nginx的配置文件中server_tokens选项的值设置为off

[root@l1 conf]# cd /usr/local/nginx/conf/
[root@l1 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@l1 conf]# vim nginx.conf
关闭版本号 

 

[root@l1 conf]# nginx -t
nginx: [emerg] unknown directive "server_token" in /usr/local/nginx/conf/nginx.conf:20
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@l1 conf]# systemctl reload nginx.service   //重新加载
[root@l1 conf]# 

重启服务,访问网站使用curl -l命令检测

systemctl restart ngnix
curl -l ...
[root@l1 conf]# systemctl reload nginx.service 
[root@l1 conf]# curl -I http://192.168.18.10
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 07 Jun 2024 06:00:45 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 04 Jun 2024 11:50:06 GMT
Connection: keep-alive
ETag: "665eff6e-267"
Accept-Ranges: bytes

 

方法二:修改源代码 

cd /opt/
ls

cd nginx-1.26.0/
ls

cd src/
ls

cd core/
ls

vim nginx.h
[root@l1 core]# pwd
/opt/nginx-1.26.0/src/core
[root@l1 core]# cp nginx.h nginx.h_bak
[root@l1 core]# vim nginx.h

重新编译安装 
cd /opt/nginx-1.26.0/

——————————————————————————————————————————————————————————————————————————————
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

——————————————————————————————————————————————————————————————————————————————
make -j2 && make install

vim /usr/local/nginx/conf/nginx.conf

 

systemctl restart nginx

curl -I http://192.168.18.10

方法三 模块法:

headers-more-nginx-module-0.34.tar.gz 插件包,解压到一个目录,编译安装 nginx, ./configure --add-module='模块路径' && make && make install
修改 nginx.conf 文件,在 http 配置块加   more_clear_headers '响应头字段'; 
即可去除 nginx 响应头任何想去除的字段

二、配置Nginx网页缓存时间

当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
一般针对静态网页设置,对动态网页不设置缓存时间
设置方法:修改配置文件,在http段、或者server段、或者location段加入对特定内容的过期参数

  • 17
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值