环境准备
- 操作系统: CentOS 7.7
- Nginx 版本: 1.24.0
基础安装
-
安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel -
创建并进入文件夹
mkdir -p /data/software/nginx && cd /data/software/nginx -
下载并解压安装包
wget http://nginx.org/download/nginx-1.24.0.tar.gz && tar -zxvf nginx-1.24.0.tar.gz && rm -rf nginx-1.24.0.tar.gz -
编译并安装
cd /data/software/nginx/nginx-1.24.0 ./configure --prefix=/data/software/nginx --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_v2_module --with-http_stub_status_module --with-http_sub_module --with-http_auth_request_module make make install -
添加环境变量,实现全局配置
编辑
~/.bash_profile文件:vim ~/.bash_profile添加如下命令:
# nginx PATH=$PATH:/data/software/nginx/sbin export PATH然后执行以下命令使变更生效:
source ~/.bash_profile -
启动
nginx
升级改造
升级
设置运行用户
在 nginx.conf 文件中,将 user 设置为 root:
user root;
调整主进程PID路径
在 nginx.conf 文件中,重新指定进行PID路径
pid /data/software/nginx/sbin/nginx.pid;
调整工作线程
在 nginx.conf 文件中,将worker_processes设置为auto
worker_processes auto;
开启访问日志并设置日志格式与路径
在 http 块中添加:
log_format main escape=json '$remote_addr - $remote_user [$time_local.$msec] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time '
'$request_body ';
access_log /data/logs/nginx/access.log main;
开启zip压缩
在 http 块中添加:
gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types application/json text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
开启文件上传大小限制
在 http 块中添加:
client_max_body_size 20m;
改造
基于使用过程中的漏洞修复进行的nginx配置改造
去除接口响应中的Server响应头
如截图所示,响应头中的Server会暴露Nginx与版本,需要进行去除。使用过以下两个方法:
-
使用server_tokens参数。可以在http块中配置如下命令:
server_tokens off;但是此方案仅会隐藏Server响应头中的Nginx版本而已。
-
使用headers-more-nginx-module模块。此方案修复较为复杂,但能完全去除Server响应头:
-
创建插件目录
mkdir -p /data/software/nginx/plugins -
下载插件
cd /data/software/nginx/plugins wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz tar -zxvf v0.34.tar.gz rm -rf v0.34.tar.gz -
升级模块
- 进入nignx源码
mkdir -p /data/software/nginx/nginx-1.24.0 - 获取nginx已编译的内容
nginx -V - 复制【configure arguments】中的内容,并添加新的模块,最后进行编译
无需执行make install命令
./configure --prefix=/data/software/nginx --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_v2_module --with-http_stub_status_module --with-http_sub_module --with-http_auth_request_module --add-module=/data/software/nginx/plugins/headers-more-nginx-module-0.34 make - 将旧版本的nginx二进制文件进行备份
mv /data/software/nginx/sbin/nginx /data/software/nginx/sbin/nginx.old - 将新版本的nginx二进制文件进行拷贝
cp /data/software/nginx/nginx-1.24.0/objs/nginx /data/software/nginx/sbin - 执行更新安装命令
make upgrade - 执行命令,查看是否有新添加的模块
nginx -V - 重启nginx服务即生效
nginx -s reload
4.去除Server响应头
在
http块中添加:more_clear_headers 'Server'; - 进入nignx源码
-
配置HSTS请求头
HTTP Strict Transport Security(HSTS)是一种安全策略,用于强制客户端(如浏览器)只通过HTTPS与服务器建立连接。这种机制有助于保护网站免受SSL剥离攻击和会话劫持等威胁
一般在 http 块中添加:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
配置Content-Security-Policy请求头
用于提供额外的安全性层,通过制定有效的源列表来限制可以加载资源类型。这是 Web 安全策略的一部分,用于控制页面是否可以在 iframe、object 或 embed 等 HTML 元素中被嵌入。
一般在 location 块中添加:
add_header Content-Security-Policy "frame-ancestors none;";
页面使用Basic Auth
需要使用到http_auth_request_module模块,如果按照上文安装步骤,则无需在意,否则可以参考上文的【升级模块】的步骤来添加此模块
在 location 块中添加:
auth_basic "basic auth";
auth_basic_user_file /data/software/nginx/pw/htpasswd; # 指定密码文件
其中htpasswd文件可以使用服务器的httpd-tools软件包来生成账户与密码,可以简单参考一下命令:
-
下载安装httpd-tools
yum install httpd-tools -
生成文件
示例如下,账户为admin、密码为12345test
htpasswd -c -b /data/software/nginx/pw/htpasswd admin 12345test
版本升级
准备
- 旧版本的 nginx 为 1.26.2,且安装在/data/software/nginx-1.26.2 目录下
- 新版本的 nginx 为 1.27.0,且安装在/data/software/nginx-1.27.0 目录下
步骤
-
下载最新的 nginx 文件
wget -c http://nginx.org/download/nginx-1.27.0.tar.gz -
解压并进入解压后的目录
tar -zxvf nginx-1.27.0.tar.gz cd nginx-1.27.0/ -
查看 nginx 的配置信息
nginx -V复制 configure arguments 中的参数
-
编译与安装
./configure 后面跟着上一步复制的configure arguments中的参数 make -
关闭旧的 nginx 进程并备份 nginx 文件
nginx -s stop mv /data/software/nginx-1.26.2/sbin/nginx /data/software/nginx-1.26.2/sbin/nginx.old -
移动新的 nginx 文件,到旧的 nginx 目录下
cp /data/software/nginx-1.27.0/objs/nginx /data/software/nginx-1.26.2/sbin/ -
查看 nginx 的配置信息,检查是否升级成功
nginx -V可以查看到 nginx 的版本号
-
检查 nginx 配置是否正常
nginx -t -
启动新的 nginx
nginx