Linux系统架构----Nginx的服务优化

Linux系统架构----Nginx的服务优化

一.隐藏版本号

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

查看Nginx的版本有两种方法

  • 使用fiddler工具抓取数据包,查看Nginx版本

  • 在Centos7上使用使用命令 curl -I 查看

隐藏Nginx版本号也有两种方法

  • 修改Nginx的源码文件,指定不显示版本号

  • 修改Nginx的主配置文件

隐藏版本号操作

  • 修改源码文件

    [root@server1 ~]# curl -I 10.1.1.172
    HTTP/1.1 200 OK
    Server: nginx/1.14.1 ##版本号
    Date: Sat, 09 Mar 2024 11:27:40 GMT
    Content-Type: text/html
    Content-Length: 4057
    Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
    Connection: keep-alive
    ETag: "5d9bab28-fd9"
    Accept-Ranges: bytes

    ##修改配置
    [root@server2 ~]# vim /etc/nginx/nginx.conf
    ...
    http {
    include mime.types;
    default_type application/octet-stream;
    server_tokens off;
    ...
    [root@server2 ~]# systemctl restart nginx.service

  • 验证是否隐藏版本号

    [root@server1 ~]# curl -I 10.1.1.172
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 09 Mar 2024 11:31:19 GMT
    Content-Type: text/html
    Content-Length: 4057
    Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
    Connection: keep-alive
    ETag: "5d9bab28-fd9"
    Accept-Ranges: bytes

二.修改用户和组

  • Nginx在运行时进程需要有用户和组的支持,用于实现对网站读取时的进行访问控制
  • 主进程由root创建,子进程有指定的用户与组创建
  • Nginx默认使用nobody用户账户和组账号

修改Nginx用户和组有两种方法

  • 在编译安装时指定的用户与组

  • 修改配置文件

修改用户和组操作

  • 编译时指定用户和组

    #创建用户,不建立宿主文件,且不能再shell上登录
    useradd -M -s /sbin/nologin nginx
    #配置,安装且编译
    cd /opt/nginx-1.12.2/
    ./configure
    --prefix=/usr/local/nginx
    --user=nginx \ //指定用户名为nginx
    --group=nginx \ //指定组名为nginx
    --with-http_stub_status_module

  • 修改Nginx的配置文件nginx.conf指定用户和组

    [root@server2 ~]# vim /etc/nginx/nginx.conf
    ...
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    ...

  • 查看Nginx进程运行情况

    [root@server2 ~]# ps aux |grep nginx
    root 25282 0.0 0.0 119160 2176 ? Ss 19:30 0:00 nginx: master process /usr/sbin/nginx
    nginx 25283 0.0 0.2 151852 8140 ? S 19:30 0:00 nginx: worker process
    nginx 25284 0.0 0.2 151852 8140 ? S 19:30 0:00 nginx: worker process
    nginx 25285 0.0 0.2 151852 8140 ? S 19:30 0:00 nginx: worker process
    nginx 25286 0.0 0.2 151852 8140 ? S 19:30 0:00 nginx: worker process
    root 25291 0.0 0.0 12348 1036 pts/2 S+ 19:35 0:00 grep --color=auto nginx

三.配置网页缓存时间

  • 当Nginx将网页数据返回给给客户端之后,可以设置缓存的时间,方便下次再浏览相同的内容时直接返回,避免重复请求,加快访问速度,一般只针对静态资源设置,对于动态网页不用设置缓存时间

  • 在Nginx服务中,expires参数指定缓存时间

  • 当没有设置expires参数时,使用Fiddler进行抓包

    [root@server2 ~]# vim /etc/nginx/nginx.conf
    location ~ .php$ {
    proxy_pass http://10.1.1.171;
    expires 1d; ##添加此处代码
    }

四.日志分割

  • Nginx没有类似于Apache的cronlog日志分割处理功能,但是可以通过Nginx的信号控制功能脚本来实现日志的自动分割,并且将脚本加入到Linux的计划任务中去,让脚本在每天固定的时间执行,便可以实现切割功能

  • 编写脚本进行日志切割的思路

  • 设置时间变量

  • 设置保存日志路径将目前的日志文件进行重命名

  • 删除时间过长的日志文件

  • 设置cron任务,定期执行脚本自动进行日志分割

    [root@server2 ~]# vim /opt/fenge.sh
    #!/bin/bash
    d=(date -d "-1 day" "+%Y%m%d") logs_path="/var/log/nginx" pid_path="/run/nginx.pid" [ -d logs_path ] || mkdir -p logs_path //创建日志文件目录 mv /var/log/nginx/access.log {logs_path}/test.com-access.log-d ##移动且重命名日志文件 kill -USR1 (cat pid_path) ##重建新的日志文件 find logs_path -mtime +30 |xargs rm -rf ##删除30天之前的日志文件

执行分割脚本

复制代码
[root@server2 opt]# bash fenge.sh 
[root@server2 opt]# ls /var/log/nginx/  ##查看日志文件
access.log  error.log  test.com-access.log-20240308
##修改日期
[root@server2 opt]# date
2024年 03月 09日 星期六 20:10:00 CST
[root@server2 opt]# date -s 2024-03-12
2024年 03月 12日 星期二 00:00:00 CST
[root@server2 opt]# ls /var/log/nginx/
access.log  error.log  test.com-access.log-20240308  test.com-access.log-20240311
###设置crontab任务,每天零点执行脚本
[root@server2 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@server2 ~]# crontab -l
0 0 * * * /opt/fenge.sh

五.设置连接超时

  • 在企业网站中为了避免同一个客户长时间占用连接,造成资源浪费,可以设置相应的连接超时参数,实现对连接访问时间的控制,可以修改配置文件nginx.conf,设置keepalive_timeout超时时间

  • 具体操作如下

    keepalive_timeout 65 180;
    //第一个参数指定了与客户端的keep-alive连接超时时间,服务器将会在这个时间后关闭连接
    //第二个参数指定了在响应头Keep-Alive_timeout中的time值,这个头能够让一些浏览器主动关闭连接,这样服务器就不必关闭连接。如果没有这个参数,Nginx将不会发送Keep_Alive响应头
    client_header_timeout 80;
    //指定等待客户端发送请求头的超时时间
    client_body_timeout 80;
    //指定请求体读的超时时间

相关推荐
存储服务专家StorageExpert15 分钟前
手搓一个 DELL EMC Unity存储系统健康检查清单
linux·运维·服务器·存储维护·emc存储
笑口常开xpr1 小时前
Linux 库开发入门:静态库与动态库的 2 种构建方式 + 5 个编译差异 + 3 个加载技巧,新手速看
linux·c语言·动态库·静态库
SonOfWind03111 小时前
CentOS搭建本地源
linux·运维·centos
IT成长日记1 小时前
【Nginx开荒攻略】Nginx主配置文件结构与核心模块详解:从0到1掌握nginx.conf:
linux·运维·nginx·配置文件
Nimsolax1 小时前
Linux线程控制
linux
Light601 小时前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)
linux·spring boot·pdf·gpcl6/ghostpcl·s3/oss·权限与审计·异步与进度
alibli2 小时前
一文学会CMakeLists.txt: CMake现代C++跨平台工程化实战
开发语言·c++·系统架构
YuTaoShao2 小时前
【LeetCode 每日一题】36. 有效的数独
linux·算法·leetcode
NiKo_W2 小时前
Linux 开发工具(1)
linux·运维·服务器
笑口常开xpr3 小时前
Linux动静态库开发基础:静态库与动态库的编译构建、链接使用及问题排查
linux·c语言·动态库·静态库