【运维心得】Ubuntu2404编译nginx隐藏Server信息

前言

因客户安全要求,需要去掉请求中的服务信息,故而根据资料做了nginx隐藏Server字段,这里记录下完整过程,方便其他同志使用。

操作步骤
1.安装编译环境
bash 复制代码
sudo apt update && sudo apt install -y build-essential devscripts debhelper dpkg-dev libpcre3-dev zlib1g-dev libssl-dev lintian
2. 确保下镜像源

这里使用清华源

使用新推荐方式处理Ubuntu镜像源头

bash 复制代码
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

sudo vim /etc/apt/sources.list.d/ubuntu.sources

内容:

bash 复制代码
# 清华大学 Ubuntu 24.04 (noble) 镜像 - deb822 格式

Types: deb

URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/

Suites: noble noble-security noble-updates noble-backports

Components: main restricted universe multiverse

Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg



# 源码

Types: deb-src

URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/

Suites: noble noble-security noble-updates noble-backports

Components: main restricted universe multiverse

Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
bash 复制代码
sudo apt clean

sudo apt update
3.创建编译目录

这里 /home/test为例

bash 复制代码
mkdir ~/nginx-build
cd ~/nginx-build
apt source nginx
4. 安装自定义插件
bash 复制代码
 wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz -O headers-more-nginx-module-0.34.tar.gz
tar -xzf headers-more-nginx-module-0.34.tar.gz
5. 安装必要依赖
bash 复制代码
cd nginx-*
sudo apt build-dep nginx
6.编辑deb包rule
bash 复制代码
vim debian/rules
找到 basic_configure_flags
增加
--add-module=/home/test/nginx-build/headers-more-nginx-module-0.34 \
7.自定义头信息
bash 复制代码
vim src/core/nginx.h
#define NGINX_VERSION      "httpServer"
#define NGINX_VER          "test/" NGINX_VERSION
8.免签名编译
bash 复制代码
dpkg-buildpackage -us -uc -b
9.获取安装包
bash 复制代码
编译好的数据 在上层目录
cd ..
ll nginx*.deb

核心只需要 
sudo dpkg -i nginx-common_1.24.0-2ubuntu7.5_all.deb
sudo dpkg -i nginx_1.24.0-2ubuntu7.5_amd64.deb
10.隐藏Server信息
bash 复制代码
vim /etc/nginx/nginx.conf
在http或者server域
增加
server_tokens off;
more_clear_headers 'Server';  # 完全移除 Server 头
相关推荐
小羊没烦恼!12 分钟前
jQuery1.5的改进细节
java·服务器·开发语言·前端·c#
北龙云海21 分钟前
AI驱动数据库运维变革:北龙云海智能巡检平台实践与展望
运维·数据库·人工智能
AI职业加油站30 分钟前
AI 校招现状:大模型应用工程师证书,助力简历能力证明
大数据·运维·人工智能·学习·职场发展
Lsetea2 小时前
curl报58 unable to set private key file:客户端证书与私钥加载排查
运维·https·ssl证书·openssl·curl
天远Date Lab2 小时前
零信任架构实战:基于天远学籍核验三要素构建自动化竞赛资格审查网关
运维·人工智能·架构·自动化
KING-WU5122 小时前
Linux 工具之Make 与 Makefile
java·linux·服务器
阿乔外贸日记2 小时前
全球电子产业高度集中:中、韩、日和中国台湾地区占全球数字硬件产能八成
大数据·服务器·人工智能·物联网·云计算
Lsetea3 小时前
OpenSSL报certificate is not yet valid:error 9与notBefore时间排查
linux·运维·https·ssl证书·openssl
魔法阵维护师3 小时前
Linux 命令行入门踩坑记录:从 VMware 装系统到文件管理
linux·运维·服务器
飞飞传输3 小时前
业务系统文件安全检测怎么做?生物医药企业选型与建设指南
大数据·运维·安全