目录
② 上传图片到 /usr/local/nginx/html 目录下
引言
在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况来进行相应的参数配置,来达到最优的用户体验。
但是默认的 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 ngin