【运维心得】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 头
相关推荐
Solar20259 分钟前
机械制造业TOB企业获客软件选型指南:从挑战到解决方案的深度解析
java·大数据·服务器·架构·云计算
橘子真甜~11 分钟前
Reids命令原理与应用5 - Redis 主从同步与高可用集群
运维·网络·数据库·redis·缓存·redis集群·redis高可用
陈让然24 分钟前
WSL2 ubuntu18.04扩容
linux·运维·ubuntu
HIT_Weston43 分钟前
94、【Ubuntu】【Hugo】搭建私人博客:面包屑(二)
linux·运维·ubuntu
hui2593031 小时前
RH134 第八章 管理存储堆栈
linux·运维·服务器
每日出拳老爷子1 小时前
【客户端方案】Electron / 原生客户端在内网会议中的取舍与坑点
服务器·electron·远程工作·流媒体·视音频
乾元1 小时前
10 个可复制的企业级项目:从需求到交付的 AI 网络工程模板(深度实战版)
运维·网络·人工智能·网络协议·安全
遇见火星2 小时前
Linux 命令篇:df、du、fdisk 磁盘管理
linux·运维·服务器·df·du·fdisk
噎住佩奇2 小时前
正则表达式(Regex)入门
运维·正则表达式