在Linux系统中配置Nginx网页优化(一)
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,配置Nginx隐藏版本号在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄漏查看方法使用fiddler工具在Windows客户端查看Nginx版本号在CentOS系统中使用"curl -I
千家信息网最后更新 2025年02月23日在Linux系统中配置Nginx网页优化(一)
配置Nginx隐藏版本号
在生产环境中,需要隐藏
Nginx
的版本号,以避免安全漏洞的泄漏查看方法
- 使用
fiddler
工具在Windows
客户端查看Nginx
版本号 - 在
CentOS
系统中使用"curl -I 网址"
命令查看
- 使用
- Nginx隐藏版本号的方法
- 修改配置文件法
- 修改源码法
修改配置文件法
Nginx
的配置文件中的server_ tokens
选项的值设置为off
[root@www conf]# vi nginx.conf.....server_ tokens off; //关闭版本号.....[root@www conf]# nginx -t
- 重启服务,访问网站使用
curl -I
命令检测
[root@www conf]# service nginx restart[root@www conf]# curl -| http://192.168.9.209/HTTP/1.1 200 OKServer: nginx
- 若
php
配置文件中配置了fastcgi_param SERVER_ SOFTWARE
选项. - 则编辑
php-fpm
配置文件,将fastcgi_param SERVER_ SOFTWARE
对应的值修改为fastcgi_param SERVER_ SOFTWARE nginx
;
配置实例
[root@localhost nginx]# curl -I http://192.168.144.133/ //使用命令查看版本号HTTP/1.1 200 OKServer: nginx/1.12.2 //显示版本号Date: Thu, 14 Nov 2019 06:52:14 GMTContent-Type: text/htmlContent-Length: 634Last-Modified: Thu, 14 Nov 2019 06:24:32 GMTConnection: keep-aliveETag: "5dccf320-27a"Accept-Ranges: bytes[root@localhost nginx]# vim conf/nginx.conf //进入编辑配置文件...//省略部分内容...http { include mime.types; default_type application/octet-stream; server_tokens off; //添加条目关闭版本号...//省略部分内容...:wq[root@localhost nginx]# systemctl restart nginx.service[root@localhost nginx]# curl -I http://192.168.144.133HTTP/1.1 200 OKServer: nginx //版本号隐藏Date: Thu, 14 Nov 2019 06:56:51 GMTContent-Type: text/htmlContent-Length: 634Last-Modified: Thu, 14 Nov 2019 06:24:32 GMTConnection: keep-aliveETag: "5dccf320-27a"Accept-Ranges: bytes
修改源码法
Nginx
源码文件/usr/src/nginx-1.12.0/src/core/nginx.h
包含了版本信息,可以随意设置重新编译安装,隐藏版本信息
示例:
#define NGINX_ VERSION"1.1.1" 修改版本号为1.1.1#define NGINX VER "IIS/" 修改软件类型为IIS
- 重启服务,访问网站使用
curl -I
命令检测
配置实例
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //编辑nginx配置文件...//省略部分内容...http { include mime.types; default_type application/octet-stream; server_tokens on; //打开上面设置的隐藏版本号条目...//省略部分内容...:wq[root@localhost ~]# cd /opt/nginx-1.12.2/src/core/ //到解压的源码包中更改版本号信息[root@localhost core]# vim nginx.h#define nginx_version 1012002#define NGINX_VERSION "1.1.1" //更改版本号#define NGINX_VER "nginx/" NGINX_VERSION:wq[root@localhost core]# cd /optnginx-1.12.2/[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module //重新配置nginxchecking for OS + Linux 3.10.0-693.el7.x86_64 x86_64checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ...//省略部分内容... nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"[root@localhost nginx-1.12.2]# make && make install //重新制作安装nginx[root@localhost nginx-1.12.2]# systemctl restart nginx.service //重新启动nginx服务[root@localhost nginx-1.12.2]# curl -I http://192.168.144.133 //查看版本号HTTP/1.1 200 OKServer: nginx/1.1.1 //版本号变更Date: Thu, 14 Nov 2019 07:11:08 GMTContent-Type: text/htmlContent-Length: 634Last-Modified: Thu, 14 Nov 2019 06:24:32 GMTConnection: keep-aliveETag: "5dccf320-27a"Accept-Ranges: bytes
修改Nginx用户与组
Nginx
运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制Nginx
默认使用nobody
用户账号与组账号,一般也要进行修改- 修改的方法
- 编译安装时指定用户与组
- 修改配置文件指定用户与组
编译安装时指定
创建用户账号与组账号,如
nginx
- 在编译安装时
--user
与--group
指定Nginx
服务的运行用户与组账号
修改配置文件法指定
- 新建用户账号,如
nginx
- 修改主配置文件
user
选项,指定用户账号 - 重启
nginx
服务, 使配置生效 - 使用
ps aux
命令查看nginx
的进程信息,验证运行用户账号改变效果
[root@www conf]# vi nginx.confuser nginx nginx;[root@www conf]# service nginx restart[root@www conf]# ps aux | grep nginxroot 130034 0.0 0.0 20220 620 ? Ss 19:41 0:00 nginx: master process/usr/local/sbin/nginxnginx 130035 0.0 0.0 20664 1512 ? S 19:41 0:00 nginx: worker process
配置Nginx网页缓存时间
- 当
Nginx
将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度 - 般针对静态网页设置,对动态网页不设置缓存时间
- 可在
Windows
客户端中使用fiddler
查看网页缓存时间
设置方法
- 可修改配置文件,在
http
段、 或者server
段、 或者location
段加入对特定内容的过期参数
示例
- 修改Nginx的配置文件,在location段加入expires参数
location ~\.(gif|ipg|jepg|png|bmp|ico)$ { root html; expires 1d; }
配置实例
[root@localhost ~]# systemctl stop firewalld.service //关闭防火墙[root@localhost ~]# setenforce 0 //关闭增强性安全功能[root@localhost ~]# systemctl start nginx.service //启动nginx服务[root@localhost ~]# netstat -ntap | grep 80 //查看服务端口是否开启tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1684/nginx: master [root@localhost ~]# mkdir abc[root@localhost ~]# mount.cifs //192.168.100.10/lamp-c7 abc/ //将宿主机图片文件夹挂载到abc目录Password for root@//192.168.100.10/lamp-c7: [root@localhost ~]# cd abc/ //进入abc目录 [root@localhost abc]# lsapr-1.6.2.tar.gz Discuz_X2.5_SC_UTF8.zip miao.jpgapr-util-1.6.0.tar.gz error.png mysql-5.6.26.tar.gzawstats-7.6.tar.gz httpd-2.4.29.tar.bz2 nginx-1.12.0.tar.gzcronolog-1.6.2-14.el7.x86_64.rpm LAMP-php5.6.txt php-5.6.11.tar.bz2[root@localhost abc]# cp miao.jpg /usr/local/nginx/html/ //将图片复制到nginx服务站点[root@localhost abc]# cd /usr/local/nginx/html/ //进入站点目录[root@localhost html]# ls50x.html index.html miao.jpg[root@localhost html]# vim index.html //编辑网页内容Welcome to nginx! Welcome to nginx!
//添加图片If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.
:wq[root@localhost nginx]# vim conf/nginx.conf //编辑配置..//省略部分内容...events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream;..//省略部分内容... # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~\.(gif|jepg|jpg|ico|bmp|png)$ { //编辑缓存条目 root html; expires 1d; } }..//省略部分内容...:wq[root@localhost nginx]# systemctl restart nginx.service //重启nginx服务
- 在客户机中访问网页,并使用抓包工具查看缓存时间是否开启
配置Nginx实现连接超时
- 在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
- 使用Fiddler工具查看connection参数
超时参数讲解
Keepalive_ timeout
- 设置连接保持超时时间,-般可只设置该参数,默认为75秒,可根据网站的情况设置,或者关闭,可在http段、 server段、 或者location段设置
Client header_ timeout
- 指定等待客户端发送请求头的超时时间
- Client body _timeout
- 设置请求体读超时时间
配置实例
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/ //进入nginx配置文件目录[root@localhost conf]# vim nginx.conf //编辑配置文件...//省略部分内容...http { include mime.types; default_type application/octet-stream; server_tokens on; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65 180; //添加客户端超时时间180秒 client_header_timeout 80; //设置客户端头部超时时间 client_body_timeout 80; //设置客户端主题内容超时时间 #gzip on; server { listen 80; server_name localhost;...//省略部分内容...:wq[root@localhost conf]# systemctl restart nginx.service //重启服务
配置
文件
版本
内容
时间
用户
部分
服务
客户
账号
网页
客户端
参数
缓存
命令
网站
信息
实例
方法
源码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
我国 网络安全
第一部网络安全基础法律
网络安全主要面临的危险有哪些
网络安全敏捷性
智能语音服务器订做价格
万方数据库2.0查重
学计算机软件开发看什么书
手机登录邮箱的服务器怎么填
招标进行软件开发属于自行研发吗
360网络安全医疗
计算机网络技术论文写手
NDR网络安全
服务器搭建与管理xp怎么装
网络安全的危害性的作文开头
数据库技术及应用B卷
我国网络技术以及相关技术的发展
速达运行软件服务器怎样进入
java 多数据库支持
华为服务器业务公司名称
无纸化会议文件管理服务器
sql数据库语句默认值
p2p系统软件开发商
互联网时代科技社会生态协调
全国网络安全宣传周的讲话
关系型数据库读写分离
为什么数据库有时连接失败
服务器存在多个jdk
网络安全班团活动ppt
proe怎么和数据库连接
系统网络安全运维服务