【运维心得】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 头
相关推荐
雪可问春风18 小时前
docker环境部署
运维·docker·容器
lwx91485218 小时前
Linux-Shell算术运算
linux·运维·服务器
翻斗包菜19 小时前
PostgreSQL 日常维护完全指南:从基础操作到高级运维
运维·数据库·postgresql
somi719 小时前
ARM-驱动-02-Linux 内核开发环境搭建与编译
linux·运维·arm开发
海的透彻19 小时前
nginx启动进程对文件的权限掌控
运维·chrome·nginx
路溪非溪19 小时前
Linux驱动开发中的常用接口总结(一)
linux·运维·驱动开发
此刻觐神19 小时前
IMX6ULL开发板学习-01(Linux文件目录和目录相关命令)
linux·服务器·学习
航Hang*20 小时前
第3章:Linux系统安全管理——第2节:部署代理服务
linux·运维·服务器·开发语言·笔记·系统安全
fengfuyao98520 小时前
VC++基于服务器的点对点文件传输实例
服务器·开发语言·c++
favour_you___20 小时前
epoll惊群问题与解决
服务器·网络·tcp/ip·epoll