Nginx优化与防盗链

目录

引言

一、Nginx 服务优化

1. 隐藏版本号

1.1 查看 Nginx 版本

1.2 隐藏 Nginx 版本号

2. 修改用户与组

2.1 编译安装时指定用户和组

 2.2 修改配置文件

3. 配置网页缓存时间

4.日志分割 

5. 设置连接超时

二、Nginx 优化深入

1. 更改进程数

2. 配置网页压缩

① 压缩功能参数

② 上传图片到 /usr/local/nginx/html 目录下

③ 在网页中插入图片 

④ 浏览器中访问网址

3. 盗链与防盗链

3.1 盗链

3.2 防盗链

4. FPM 参数优化

总结


引言

在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况来进行相应的参数配置,来达到最优的用户体验。
但是默认的 Nginx 安装参数只能提供最基础的服务,还需调整页面缓存时间、连接超时、网页压缩等相应参数,才能让 Nginx 发挥最大的性能,接受更高的并发。

一、Nginx 服务优化

1. 隐藏版本号

在生产环境中,需要隐藏 Nginx 版本号,以避免泄露 Nginx 的版本,使攻击者不能针对特定版本进行攻击。

1.1 查看 Nginx 版本

 (1)网页查看

(2)本地查看

1.2 隐藏 Nginx 版本号

隐藏 Nginx 版本号有两种方式:第一种是修改 Nginx 源码文件,指定不显示版本号;第二种是修改 Nginx 的主配置文件。

(1)修改配置文件

[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
......
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;                         #添加,关闭版本号
......

[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.service   #重启服务

(2)修改源码文件

[root@localhost /opt/nginx-1.20.2/src/core]#vim /opt/nginx-1.12.2/src/core/nginx.h
......
#define nginx_version      1012002
#define NGINX_VERSION      "6.6.6"                             #修改版本号
#define NGINX_VER          "nginx/" NGINX_VERSION            #修改服务器类型
......

[root@localhost /]#cd /opt/nginx-1.20.2/
#重新编译安装
[root@localhost /opt/nginx-1.20.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost /opt/nginx-1.20.2]#make && make install

[root@localhost /opt/nginx-1.20.2]#vim /usr/local/nginx/conf/nginx.conf
.......
http {
    include       mime.types;
    default_type  application/octet-stream;
   # server_tokens off;
    server_tokens on;                                        #开启版本号
......

[root@localhost /opt/nginx-1.20.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 /opt/nginx-1.20.2]#systemctl restart nginx.service
[root@localhost /opt/nginx-1.20.2]#curl -I http://192.168.8.135
HTTP/1.1 200 OK
Server: mysql/8.8.8                                          #查看设置的版本信息
......

2. 修改用户与组

Nginx 运行时进程需要有用户与组的支持,用以实现对网站文件读取时进行访问控制。
主进程由 root 创建,子进程由指定的用户与组创建。
Nginx 默认使用 nobody 用户账号与组账号,一般需要进行修改。
修改 Nginx 用户和组有两种方法,一种是编译安装时指定用户和组,另一种是修改配置文件指定用户和组。

2.1 编译安装时指定用户和组

  • 配置 Nginx 时,在 ./configure 后面指定用户和组的参数
[root@localhost /opt/nginx-1.20.2]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \   									#指定用户名是 nginx
--group=nginx \										#指定组名是 nginx
--with-http_stub_status_module

 2.2 修改配置文件

[root@localhost /opt/nginx-1.20.2]#vim /usr/local/nginx/conf/nginx.conf

user nginx nginx;									#修改用户为 nginx,组为 nginx
worker_processes  1;
......

[root@localhost /opt/nginx-1.20.2]#systemctl restart nginx.service 
[root@localhost /opt/nginx-1.20.2]#ps aux |grep nginx
root       5451  0.0  0.0  20496   604 ?        Ss   20:04   0:00 nginx: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值