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;
    //指定请求体读的超时时间

相关推荐
CoolTiger、1 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
学习3人组2 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神2 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0662 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen012 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦3 小时前
linux 解压缩
linux·运维·服务器
牧小七4 小时前
Linux命令---查看端口是否被占用
linux
鸡鸭扣5 小时前
虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
linux·运维·ubuntu
傻傻虎虎5 小时前
【系统架构设计】信息系统基础知识
系统架构
友友马6 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http