网站服务器的维护方法,英文网站提交,刚注册公司怎么做网站,优秀的网页模板网站Linux系统架构----Nginx的服务优化
一.隐藏版本号
在生产环境中#xff0c;需要隐藏Nginx的版本号#xff0c;以免泄露Nginx的版本#xff0c;使得攻击者不能针对特定版本进行攻击
查看Nginx的版本有两种方法 使用fiddler工具抓取数据包#xff0c;查看Nginx版本 在Cen…Linux系统架构----Nginx的服务优化
一.隐藏版本号
在生产环境中需要隐藏Nginx的版本号以免泄露Nginx的版本使得攻击者不能针对特定版本进行攻击
查看Nginx的版本有两种方法 使用fiddler工具抓取数据包查看Nginx版本 在Centos7上使用使用命令 curl -I 查看
隐藏Nginx版本号也有两种方法 修改Nginx的源码文件指定不显示版本号 修改Nginx的主配置文件
隐藏版本号操作
修改源码文件
[rootserver1 ~]# 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##修改配置
[rootserver2 ~]# vim /etc/nginx/nginx.conf
...
http {include mime.types;default_type application/octet-stream;server_tokens off;
...
[rootserver2 ~]# systemctl restart nginx.service
验证是否隐藏版本号
[rootserver1 ~]# 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 \
--usernginx \ //指定用户名为nginx
--groupnginx \ //指定组名为nginx
--with-http_stub_status_module修改Nginx的配置文件nginx.conf指定用户和组
[rootserver2 ~]# vim /etc/nginx/nginx.conf
...
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
...查看Nginx进程运行情况
[rootserver2 ~]# 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 --colorauto nginx
三.配置网页缓存时间 当Nginx将网页数据返回给给客户端之后可以设置缓存的时间方便下次再浏览相同的内容时直接返回避免重复请求加快访问速度一般只针对静态资源设置对于动态网页不用设置缓存时间 在Nginx服务中expires参数指定缓存时间 当没有设置expires参数时使用Fiddler进行抓包
[rootserver2 ~]# vim /etc/nginx/nginx.conflocation ~ \.php$ {proxy_pass http://10.1.1.171;expires 1d; ##添加此处代码}四.日志分割
Nginx没有类似于Apache的cronlog日志分割处理功能但是可以通过Nginx的信号控制功能脚本来实现日志的自动分割并且将脚本加入到Linux的计划任务中去让脚本在每天固定的时间执行便可以实现切割功能编写脚本进行日志切割的思路设置时间变量设置保存日志路径将目前的日志文件进行重命名删除时间过长的日志文件设置cron任务定期执行脚本自动进行日志分割
[rootserver2 ~]# 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天之前的日志文件执行分割脚本
[rootserver2 opt]# bash fenge.sh
[rootserver2 opt]# ls /var/log/nginx/ ##查看日志文件
access.log error.log test.com-access.log-20240308
##修改日期
[rootserver2 opt]# date
2024年 03月 09日 星期六 20:10:00 CST
[rootserver2 opt]# date -s 2024-03-12
2024年 03月 12日 星期二 00:00:00 CST
[rootserver2 opt]# ls /var/log/nginx/
access.log error.log test.com-access.log-20240308 test.com-access.log-20240311
###设置crontab任务每天零点执行脚本
[rootserver2 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[rootserver2 ~]# 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;
//指定请求体读的超时时间