Nginx 优化

一、Nginx 隐藏版本号

当Nginx的版本号不隐藏时,访问网站会显示 Nginx的版本号,从而会增加一定的风险

1. 访问网站查看版本号

image-20230625145205221

2. 隐藏方法

(1)备份配置文件

cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak

image-20230625150134701

(2)修改配置文件

vim /usr/local/nginx/nginx.conf 
 
 http {
     include       mime.types;
     default_type  application/octet-stream;
	
	 # 隐藏版本号
	 server_tokens off;

:wq!

# 重启 Nginx 服务
systemctl restart nginx

image-20230625150819226

3. 刷新网页

版本号被隐藏

image-20230625151223070

二、Nginx 更改版本号

1. 修改 Nginx 源码文件

cd /opt/nginx-1.22.0/src/core/
vim nginx.h

# 更改为自定义版本号
#define NGINX_VERSION      "abc"

:wq!

image-20230625152205337

2. 重新编译安装

# 指定重新安装路径及模块
cd /opt/nginx-1.22.0/
[root@www nginx-1.22.0]# 
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module

# 编译与安装
make -j 6 && make install

3. 将隐藏版本号设置打开

vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    # 打开版本号设置
    server_tokens on; 

:wq!

# 重启 Nginx 服务
systemctl restart nginx

image-20230625154424572

4. 刷新网页

版本号隐藏成功

image-20230625154846670

三、Nginx 日志分割

Nginx本身不带日志分割工具,所以在工作中,所有的nginx的日志分割都是以shell脚本的形式来实现的

1. 编写shell脚本

cd /opt
[root@www opt]# 
vim nginxlog.sh

#!/bin/bash

# 获取当前日期
d=$(date +%Y-%m-%d)

# 定义存储目录
dir="/usr/local/nginx/logs"

# 定义需要分割的日志源文件
logs_file="/usr/local/nginx/logs/access.log"
logs_error="/usr/local/nginx/logs/error.log"

# 定义nginx服务的pid文件
pid_file="/usr/local/nginx/logs/nginx.pid"


if [ ! -d $dir ]
then
        mkdir $dir
fi


# 移动日志文件access error,重命名
mv ${logs_file}  ${dir}/access_${d}.log
mv ${logs_error}  ${dir}/error_${d}.log


# 发送信号,给nginx主程序,让nginx生成新的日志文件
kill -USR1 $(cat ${pid_file})


# 日志文件清理,将30天前的日志文件直接清除
find ${dir} -mtime +30 -exec rm -rf {} \;


image-20230625172206918

image-20230625165536229

2. 运行脚本

./nginxlog.sh

image-20230625172353094

3. 创建定时任务

crontab -e -u root

0 0 1 * * /opt/nginxlog.sh

:wq!

image-20230627015755523

四、Nginx 压缩页面

压缩目的时为了节约宽带,提高访问速度

ngx_http_gzip_module 压缩模块所提供的功能,默认是注释掉的,也就是不压缩,需要人工指定

gzip_min_length 1k; 最小的压缩文件,小与1K就不再压缩了

gzip buffers 4 64k; 压缩的缓冲区,4个64K缓冲区

gzip _http_version 1.1; 压缩版本,默认1.1

gzip_comp_level 6; 压缩级别1-9,6(正好)

gzip_vary on; 支持前端缓存服务器的压缩功能打开

gzip_types textplain text/javascript application/x-javascript text/css text/xml application/xml applicationt/xmI+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
支持压缩的类型

1. 更改配置文件

    gzip  on; 
        gzip_min_length 1k; 
        gzip_buffers 4 64k;
        gzip_http_version 1.1;
        gzip_comp_level 6;
        gzip_vary on; 
        gzip_types textplain text/javascript application/x-javascript text/css text/xml application/xml applicationt/xmI+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; 
        
:wq!

# 检验配置文件是否有错误
nginx -t

# 重启Nginx服务
systemctl restart nginx

image-20230625190310305

image-20230625192346348

2. 清空浏览器缓存重新访问

image-20230625194842753

五、Nginx 图片缓存

避免重复访问,导致访问速度变慢,加快访问的时间

针对静态页面,动态不设置缓存时间的

image-20230625202247664

1. 编辑Nginx配置文件

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

        location ~* \.(gif|jpg|jepg|bmp|ico)$ {
                        root html;
                        expires 1d;
        }
 
:wq!

# 重启 nginx 服务
systemctl restart nginx

image-20230625202937064

2. 浏览器访问图片

第一次访问

image-20230625203217559

第二次访问

image-20230625204556400

六、Nginx 连接超时

http:keepalive模式,keepalive 缓存记录时间

web服务器处理完一个请求之后,保持tcp连接,接受到同样的客户端的其他请求时,web服务器就会利用这个未关闭的连接,继续提供相应服务,不需要再新建连接了

keepalive:在一段时间之内保持打开状态,它会占用资源,占用过多资源,影响整体性能

keepalive_timeout 65 180;

第一个参数(60)表示客户端与服务器之间的 keep-alive(TCP) 连接的超时时间(以秒为单位)。在这个示例中,如果在 60 秒内没有数据传输,连接将被关闭。

第二个参数(180)是可选的,表示服务器向客户端发送 keep-alive 包的超时时间(以秒为单位)。在这个示例中,如果在 180 秒内没有收到客户端的请求,服务器将关闭连接。

nginx默认是65秒,一般浏览器都是60秒

设为0,nginx就不会再发送包含keepalive的相应头

client_header_timeout 60; 请求头的超时时间

没有60秒内发送完整的请求头,nginx返回408 请求超时

client_body_timeout 60; 请求体超时

没有在60秒,向web服务器请求,发送任何内容,nginx 408 请求超时

1. 编辑Nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
    keepalive_timeout  60 180;
    client_header_timeout 60;
    client_body_timeout 60;

image-20230625204238257

image-20230626151141283

2. 浏览器访问

image-20230626152743008

七、Nginx 更改进程数

在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞

vim /usr/lcoal/nginx/conf/nginx.conf

# 进程数为核数或是核数的2倍
worker_processes  2;

# 指定每个进程使用的cpu
worker_cpu_affinity 01 10;

image-20230626155022021

八、Nginx 配置防盗链

1. 添加访问的图片

# 被盗链服务器 www.TServer.com

echo "192.168.23.10 www.TServer.com" >> /etc/hosts
echo "192.168.23.15 www.FServer.com" >> /etc/hosts

vim /usr/local/nginx/html/index.html

<html>
<body>
<img src="test.jpg"/>
</body>
</html>

:wq!


# 盗链服务器 www.FServer.com

echo "192.168.23.10 www.TServer.com" >> /etc/hosts
echo "192.168.23.15 www.FServer.com" >> /etc/hosts

vim /usr/local/nginx/html/index.html
<html>
<body>
<img src="http://www.TServer.com/test.jpg"/>
</body>
</html>

:wq!

用盗链服务器的浏览器访问两台服务器

都可以访问

image-20230626183452407

image-20230626183536557

2. 配置防盗链

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

http {
......
	server {
	......
		location ~* \.(jpg|gif|swf)$ {
			valid_referers none blocked *.TServer.com TServer.com;
			if ( $invalid_referer ) {
				rewrite ^/ http://www.TServer.com/error.png;
				#return 403;
            }
        } 
	......
	}
}

盗链服务器浏览器访问两台服务器

image-20230627013339621

image-20230627013421909

九、总结

当配置Nginx服务时,可以用上面几种优化方法,来使Nginx服务稳定安全的运行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值