news 2026/8/11 1:03:25

从零到一:Nginx 1.28.1 源码编译与Systemd服务配置全攻略

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
从零到一:Nginx 1.28.1 源码编译与Systemd服务配置全攻略

Nginx的源码编译

1.下载软件

[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz

2.解压

[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz[root@Nginx ~]# cd nginx-1.28.1/[root@Nginx nginx-1.28.1]# lsauto CHANGES.ru conf contrib htmlmanSECURITY.md CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src
  1. auto/
    编译辅助脚本,./configure执行时会调用这里面脚本,检测系统环境、生成 Makefile,一般不用手动改。
  2. conf/
    源码自带的 nginx 示例配置文件。
    注意:编译安装完成后,真正使用的配置文件在安装目录(默认/usr/local/nginx/conf),这里只是源码里的模板。
  3. contrib/
    附加工具,提供 vim 语法高亮脚本、nginx 脚本示例等。
  4. html/
    默认网页页面模板(欢迎页、50x 错误页面),编译后复制到安装目录 html。
  5. man/
    nginx 帮助手册。
  6. src/
    最重要:nginx 全部 C 语言源代码

3.检测环境

#安装依赖性[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
  1. –prefix=/usr/local/nginx:安装根目录,后续所有程序、配置、日志都放在这个目录。卸载直接删这个文件夹即可。
  2. –user=nginx --group=nginx:nginx 工作进程以 nginx 用户运行,必须提前创建 nginx 用户组和用户,不能用 root 身份跑业务进程,安全规范
  3. –with-http_ssl_module:开启 HTTPS ssl 模块,实现 443 端口加密访问。
  4. –with-http_v2_module:开启 HTTP/2 协议,网页加载性能优化,HTTPS 下生效。
  5. –with-http_realip_module:获取真实客户端 IP;Nginx 做反向代理时,拿到原始用户 IP,而不是代理服务器 IP。
  6. –with-http_stub_status_module:状态监控模块,可以访问/status页面查看连接数、请求数等运行指标。
  7. –with-http_gzip_static_module:预压缩 gzip 模块,直接返回已经压缩好的.gz文件,比运行时 gzip 效率更高。
  8. –with-pcre:编译内置 pcre 正则库,rewrite、location 正则匹配。
  9. –with-stream:四层 TCP/UDP 反向代理模块,做端口转发、数据库代理、tcp 负载均衡。
  10. –with-stream_ssl_module:四层 stream 的 SSL 解密,TCP 流量的 SSL 终止。
  11. –with-stream_realip_module:四层代理下获取真实客户端 IP。

4.编译

[root@Nginx nginx-1.28.1]# make [root@Nginx nginx-1.28.1]# make install

5.nginx启动

#设定环境变量 [root@Nginx sbin]# vim ~/.bash_profile export PATH=$PATH:/usr/local/nginx/sbin [root@Nginx sbin]# source ~/.bash_profile [root@Nginx logs]# useradd -s /sbin/nologin -M nginx [root@Nginx logs]# nginx [root@Nginx logs]# ps aux | grep nginx root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process root 44015 0.0 0.1 6636 2176 pts/0 S+ 17:01 0:00 grep --color=auto nginx #测试 [root@Nginx logs]# echo timinglee > /usr/local/nginx/html/index.html [root@Nginx logs]# curl 172.25.254.100 timinglee

6.编写启动文件

[root@Nginx ~]# vim /lib/systemd/system/nginx.service[Unit]Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingExecStartPre=/usr/local/nginx/sbin/nginx-tExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx-sreloadExecStop=/bin/kill-sQUIT$MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target[root@Nginx ~]# systemctl daemon-reload#验证[root@Nginx ~]# systemctl status nginx.service○ nginx.service - The NGINX HTTP and reverse proxy server Loaded: loaded(/usr/lib/systemd/system/nginx.service;disabled;preset: disabled)Active: inactive(dead)[root@Nginx ~]# systemctl enable --now nginx[root@Nginx ~]# ps aux | grep nginxroot18390.00.1146882356? Ss 09:530:00 nginx: master process /usr/local/nginx/sbin/nginx nginx18400.00.2148883828? S 09:530:00 nginx: worker process[root@Nginx ~]# systemctl status nginx.service
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/11 0:54:40

Agent数字员工不是聊天机器人,它怎么真正进车间干活

## 引言不少企业管理者初次接触 Agent 数字员工,会把它等同于一个能聊天的机器人——回答几个问题、生成一段文字、转写一份会议纪要。真正把它放到车间里、放进采购流程里、嵌进排产环节里,会发现大多数所谓"智能助手"根本接不动真实业务。本…

作者头像 李华
网站建设 2026/8/11 0:33:55

洛谷P1784 数独 题解

题目概述 给定一个数独,求最终填好的数独。(保证有唯一解)。 思路拆分 数独的要求有三个:行,列,九宫格无重复数字。 我们只用dfs搜索每一个点就行了。 由于整个数独可以划分为9个九宫格, 在处理九宫格时我们…

作者头像 李华
网站建设 2026/8/11 0:28:00

2026年上海企业资讯投稿渠道哪家好?深度剖析及优选指南

导语2026年上海企业品牌传播进入精细化、合规化、AI可视化新阶段,本地企业资讯投稿不再是简单的稿件发布公示,而是沉淀区域品牌公信力、搭建AI数字资产、夯实沪上行业口碑的核心途径。上海作为国内媒体资源高度集中、传播监管体系规范的核心城市&#xf…

作者头像 李华
网站建设 2026/8/11 0:24:22

快消SFA系统怎么选?外勤访销管理工具的评估要点

中小快消经销商和品牌商做外勤管理,普遍被三件事卡住:拜访靠自觉、终端数据靠手写、营销费用核销凭运气。选一套 快消SFA系统,核心不是比谁功能列表长,而是比谁能让一线业务员真正用起来,帮老板看清终端、管住费用。本…

作者头像 李华