Java 项目实战: 外卖平台优化-Nginx概述与源码编译安装

接入 Nginx: 轻量级 Web 服务器定位与源码编译安装

纲要

本篇对应课程 day02 的「Nginx 概述」与「下载和安装」,覆盖:

  • Nginx 是什么 :轻量级 Web 服务器 / 反向代理服务器 / 邮件代理服务器,与 Tomcat 的定位差异
  • 为什么并发强 :master-worker 多进程模型 + epoll 事件驱动,与 Tomcat 的线程模型对比
  • 版本选择 :Mainline 与 Stable 的区别,为什么生产用稳定版
  • 源码编译安装五步 :装依赖 → 下载 → 解压 → ./configure → make && make install
  • 依赖包逐个解释 :gcc、pcre-devel、zlib-devel、openssl-devel 各自负责什么
  • --prefix 的意义:为什么必须显式指定,不指定会装到哪里

#mermaid-svg-lpmYx3d3IG2hpW1E{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-lpmYx3d3IG2hpW1E .error-icon{fill:#552222;}#mermaid-svg-lpmYx3d3IG2hpW1E .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-lpmYx3d3IG2hpW1E .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-lpmYx3d3IG2hpW1E .marker{fill:#333333;stroke:#333333;}#mermaid-svg-lpmYx3d3IG2hpW1E .marker.cross{stroke:#333333;}#mermaid-svg-lpmYx3d3IG2hpW1E svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-lpmYx3d3IG2hpW1E p{margin:0;}#mermaid-svg-lpmYx3d3IG2hpW1E .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster-label text{fill:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster-label span{color:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster-label span p{background-color:transparent;}#mermaid-svg-lpmYx3d3IG2hpW1E .label text,#mermaid-svg-lpmYx3d3IG2hpW1E span{fill:#333;color:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E .node rect,#mermaid-svg-lpmYx3d3IG2hpW1E .node circle,#mermaid-svg-lpmYx3d3IG2hpW1E .node ellipse,#mermaid-svg-lpmYx3d3IG2hpW1E .node polygon,#mermaid-svg-lpmYx3d3IG2hpW1E .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-lpmYx3d3IG2hpW1E .rough-node .label text,#mermaid-svg-lpmYx3d3IG2hpW1E .node .label text,#mermaid-svg-lpmYx3d3IG2hpW1E .image-shape .label,#mermaid-svg-lpmYx3d3IG2hpW1E .icon-shape .label{text-anchor:middle;}#mermaid-svg-lpmYx3d3IG2hpW1E .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-lpmYx3d3IG2hpW1E .rough-node .label,#mermaid-svg-lpmYx3d3IG2hpW1E .node .label,#mermaid-svg-lpmYx3d3IG2hpW1E .image-shape .label,#mermaid-svg-lpmYx3d3IG2hpW1E .icon-shape .label{text-align:center;}#mermaid-svg-lpmYx3d3IG2hpW1E .node.clickable{cursor:pointer;}#mermaid-svg-lpmYx3d3IG2hpW1E .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-lpmYx3d3IG2hpW1E .arrowheadPath{fill:#333333;}#mermaid-svg-lpmYx3d3IG2hpW1E .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-lpmYx3d3IG2hpW1E .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-lpmYx3d3IG2hpW1E .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lpmYx3d3IG2hpW1E .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-lpmYx3d3IG2hpW1E .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lpmYx3d3IG2hpW1E .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster text{fill:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E .cluster span{color:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-lpmYx3d3IG2hpW1E .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-lpmYx3d3IG2hpW1E rect.text{fill:none;stroke-width:0;}#mermaid-svg-lpmYx3d3IG2hpW1E .icon-shape,#mermaid-svg-lpmYx3d3IG2hpW1E .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lpmYx3d3IG2hpW1E .icon-shape p,#mermaid-svg-lpmYx3d3IG2hpW1E .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-lpmYx3d3IG2hpW1E .icon-shape .label rect,#mermaid-svg-lpmYx3d3IG2hpW1E .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lpmYx3d3IG2hpW1E .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-lpmYx3d3IG2hpW1E .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-lpmYx3d3IG2hpW1E :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 缺少依赖
通过
yum 安装依赖包

gcc / pcre-devel / zlib-devel / openssl-devel
wget 下载 nginx-1.16.1.tar.gz
tar -zxvf 解压
./configure --prefix=/usr/local/nginx

检查环境 + 生成 Makefile
检查是否通过
make 编译
make install 安装
/usr/local/nginx

conf html logs sbin

一、Nginx 是什么

官方定义

Nginx 是一款轻量级的 Web 服务器、反向代理服务器以及电子邮件(IMAP/POP3/SMTP)代理服务器。

拆开看这三重身份:

身份 作用 在外卖平台中的用途
Web 服务器 直接对外提供静态资源(HTML/CSS/JS/图片) 部署移动端与后台的前端页面
反向代理服务器 接收客户端请求,转发给后端真实服务器,再把响应回给客户端 把 /api/ 请求转发到 8080 端口的 Spring Boot
邮件代理服务器 代理 IMAP/POP3/SMTP 本项目中不使用

与 Tomcat 的区别

这是最容易混淆的一组概念。二者都能"部署 Web 项目",但设计目标完全不同:

维度 Nginx Tomcat
核心职责 HTTP 服务、反向代理、负载均衡、静态资源 Servlet 容器,运行 Java Web 应用
处理静态资源 极强,直接 sendfile 零拷贝 较弱,DefaultServlet 处理效率低
处理动态请求 不支持,必须转发 原生支持(执行 Servlet/JSP)
并发模型 多进程 + epoll 事件驱动,C10K 轻松 线程池,BIO/NIO,线程数受限
内存占用 极低,静态服务场景数 MB 较高,JVM 堆内存起步
典型部署位置 最外层,面向公网 Nginx 之后,内网

生产上的标准架构是 二者配合而非二选一 :Nginx 在最外层扛住海量连接、处理静态资源、HTTPS 卸载、限流;动态请求反向代理给后面的 Tomcat/Spring Boot。

为什么并发能力强

Nginx 启动后有两类进程:

  • master 进程 :只有一个,负责读取配置、管理 worker 进程、接收信号、平滑升级
  • worker 进程:默认一个,可配置多个,真正处理请求

关键在于 worker 采用 epoll 多路复用 + 异步非阻塞 模型:一个 worker 进程就能同时维持数万连接,连接数与进程数无关。

对比 Tomcat 的线程模型:默认一个请求占一个线程,maxThreads 通常配 200~500,线程上下文切换开销随并发上升而急剧增加。
#mermaid-svg-hXte79mnQjs0nBvy{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hXte79mnQjs0nBvy .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hXte79mnQjs0nBvy .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hXte79mnQjs0nBvy .error-icon{fill:#552222;}#mermaid-svg-hXte79mnQjs0nBvy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hXte79mnQjs0nBvy .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hXte79mnQjs0nBvy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hXte79mnQjs0nBvy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hXte79mnQjs0nBvy .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hXte79mnQjs0nBvy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hXte79mnQjs0nBvy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hXte79mnQjs0nBvy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hXte79mnQjs0nBvy .marker.cross{stroke:#333333;}#mermaid-svg-hXte79mnQjs0nBvy svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hXte79mnQjs0nBvy p{margin:0;}#mermaid-svg-hXte79mnQjs0nBvy .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-hXte79mnQjs0nBvy .cluster-label text{fill:#333;}#mermaid-svg-hXte79mnQjs0nBvy .cluster-label span{color:#333;}#mermaid-svg-hXte79mnQjs0nBvy .cluster-label span p{background-color:transparent;}#mermaid-svg-hXte79mnQjs0nBvy .label text,#mermaid-svg-hXte79mnQjs0nBvy span{fill:#333;color:#333;}#mermaid-svg-hXte79mnQjs0nBvy .node rect,#mermaid-svg-hXte79mnQjs0nBvy .node circle,#mermaid-svg-hXte79mnQjs0nBvy .node ellipse,#mermaid-svg-hXte79mnQjs0nBvy .node polygon,#mermaid-svg-hXte79mnQjs0nBvy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hXte79mnQjs0nBvy .rough-node .label text,#mermaid-svg-hXte79mnQjs0nBvy .node .label text,#mermaid-svg-hXte79mnQjs0nBvy .image-shape .label,#mermaid-svg-hXte79mnQjs0nBvy .icon-shape .label{text-anchor:middle;}#mermaid-svg-hXte79mnQjs0nBvy .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hXte79mnQjs0nBvy .rough-node .label,#mermaid-svg-hXte79mnQjs0nBvy .node .label,#mermaid-svg-hXte79mnQjs0nBvy .image-shape .label,#mermaid-svg-hXte79mnQjs0nBvy .icon-shape .label{text-align:center;}#mermaid-svg-hXte79mnQjs0nBvy .node.clickable{cursor:pointer;}#mermaid-svg-hXte79mnQjs0nBvy .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hXte79mnQjs0nBvy .arrowheadPath{fill:#333333;}#mermaid-svg-hXte79mnQjs0nBvy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hXte79mnQjs0nBvy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hXte79mnQjs0nBvy .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hXte79mnQjs0nBvy .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hXte79mnQjs0nBvy .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hXte79mnQjs0nBvy .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hXte79mnQjs0nBvy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hXte79mnQjs0nBvy .cluster text{fill:#333;}#mermaid-svg-hXte79mnQjs0nBvy .cluster span{color:#333;}#mermaid-svg-hXte79mnQjs0nBvy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-hXte79mnQjs0nBvy .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hXte79mnQjs0nBvy rect.text{fill:none;stroke-width:0;}#mermaid-svg-hXte79mnQjs0nBvy .icon-shape,#mermaid-svg-hXte79mnQjs0nBvy .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hXte79mnQjs0nBvy .icon-shape p,#mermaid-svg-hXte79mnQjs0nBvy .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hXte79mnQjs0nBvy .icon-shape .label rect,#mermaid-svg-hXte79mnQjs0nBvy .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hXte79mnQjs0nBvy .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hXte79mnQjs0nBvy .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hXte79mnQjs0nBvy :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Tomcat模型
Tomcat
线程 1 → 连接 1
线程 2 → 连接 2
线程 N → 连接 N
Nginx模型
master 进程
worker 进程 1

epoll 管理数万连接
worker 进程 2

epoll 管理数万连接

实际项目中的常识:把 worker_processes 设为 CPU 核心数 (或 auto),让每个 worker 绑定一个核,避免进程在核之间迁移带来的 CPU 缓存失效。

版本与历史

Nginx 由俄罗斯工程师 Igor Sysoev 开发,最初为 Rambler.ru 站点服务,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

官网提供三类版本:

版本类型 说明 适用场景
Mainline version 主线版,包含最新特性与修复,更新频繁 追求新特性、能接受风险
Stable version 稳定版,只合入严重 Bug 修复 生产推荐
Legacy versions 历史版本 兼容遗留环境

课程选用 1.16.1(Stable)。不要在生产上追最新版本------新版本意味着新代码路径尚未被大规模验证,而出问题的代价是整站不可用。

二、安装前的准备

环境说明

项目 值
操作系统 CentOS 7
Nginx 版本 1.16.1
安装目录 /usr/local/nginx
远程工具 FinalShell

课程资料中已经提供了安装包 nginx-1.16.1.tar.gz(位于 项目优化/资料/day02/),可以直接用 FinalShell 上传,也可以在 Linux 上直接 wget 下载。

为什么是源码编译而不是 yum install

Linux 章节讲过四种安装方式,这里用的是二进制发布包安装的变体------源码编译安装。原因是:

  • yum 源的 Nginx 版本往往很旧,且默认编译参数不含某些模块
  • 源码编译可以自定义模块 :--with-http_ssl_module、--with-http_v2_module、第三方模块(如 nginx-upsync-module)只能在编译时加
  • --prefix 可以指定安装位置,卸载时直接删目录即可,不污染系统目录

代价是需要手动解决依赖、编译耗时。对 Nginx 这种依赖较少的 C 项目来说,代价可接受。

三、安装 Nginx

第一步:安装依赖包

Nginx 使用 C 语言开发,编译前必须有编译环境,同时它依赖几个第三方库:

bash 复制代码
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

每个包的作用:

包名 作用 缺失后果
gcc C 语言编译器 ./configure 报 C compiler cc is not found
pcre-devel Perl 兼容正则表达式库 无法使用 location 正则匹配、rewrite
zlib-devel 压缩库 无法开启 gzip 压缩
openssl SSL 运行库 无法提供 HTTPS
openssl-devel SSL 开发头文件 无法编译 http_ssl_module

注意带 -devel 后缀的包是开发包 ,包含头文件(.h)和链接库,gcc 编译时需要。只装运行时包(pcre、zlib)编译会失败。

第二步:下载安装包

先确认 wget 是否可用:

bash 复制代码
wget
# -bash: wget: command not found

若提示 command not found,先安装:

bash 复制代码
yum install wget

然后下载:

bash 复制代码
wget https://nginx.org/download/nginx-1.16.1.tar.gz

wget 的特点值得记一下:它在弱网环境下会自动重试,且支持断点续传 ------下载中断后重新执行会自动从断点继续,这对大文件很关键(curl -O 默认不支持)。

下载完成后当前目录会出现 nginx-1.16.1.tar.gz。

第三步:解压

bash 复制代码
tar -zxvf nginx-1.16.1.tar.gz

参数含义:-z 用 gzip 解压,-x 解包,-v 显示过程,-f 指定文件名。解压后得到 nginx-1.16.1/ 目录。

第四步:配置编译环境

bash 复制代码
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx

这一步不是安装,而是做两件事:

  • 环境检查 :检测编译器、依赖库、OS 特性是否满足,把结果写入 objs/ngx_auto_config.h
  • 生成 Makefile :根据 --prefix 与各种 --with-xxx / --without-xxx 参数,决定编译哪些模块、最终装到哪

输出大致如下:

txt 复制代码
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
checking for PCRE library ... found
checking for OpenSSL library ... found
checking for zlib library ... found
...
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"

最后这段 Configuration summary 是重要信息------它明确告诉你各文件的最终落点。

关于 --prefix :若不指定,默认装到 /usr/local/nginx(Nginx 自身的默认值),但显式指定是良好习惯。更重要的是,后续要加模块时必须用相同的 --prefix 重新编译,否则会把旧安装覆盖掉。

如果需要 HTTPS(现代站点基本必需),应加上:

bash 复制代码
./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module

编译后加模块不能重新 ./configure 覆盖安装 ,正确做法是重新编译出一个新二进制,替换 sbin/nginx,再 nginx -s reload。

第五步:编译并安装

bash 复制代码
make && make install
  • make:按 Makefile 编译源代码,产物在 objs/ 目录
  • make install:把编译产物、配置文件、HTML 页面复制到 --prefix 指定的目录

课程讲义用的是 make & make install(& 表示两条命令用 && 语义同时执行)。建议用 &&------make 失败时不会继续执行 make install。

验证安装

bash 复制代码
cd /usr/local/nginx
ls
# conf  html  logs  sbin

四个目录出现即安装成功。

四、完整安装脚本

把上述步骤整理为可重复执行的脚本:

bash 复制代码
#!/bin/bash
# Nginx 1.16.1 源码编译安装脚本
# 适用:CentOS 7
set -e

NGINX_VERSION=1.16.1
INSTALL_PREFIX=/usr/local/nginx

echo "========== 1. 安装依赖包 =========="
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

echo "========== 2. 安装 wget =========="
yum -y install wget

echo "========== 3. 下载安装包 =========="
cd /usr/local/src
if [ ! -f nginx-${NGINX_VERSION}.tar.gz ]; then
    wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
fi

echo "========== 4. 解压 =========="
tar -zxvf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}

echo "========== 5. 配置编译环境 =========="
./configure --prefix=${INSTALL_PREFIX} \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module

echo "========== 6. 编译并安装 =========="
make && make install

echo "========== 7. 验证 =========="
${INSTALL_PREFIX}/sbin/nginx -v
echo "安装完成,目录:${INSTALL_PREFIX}"

保存为 /usr/local/src/install_nginx.sh,执行:

bash 复制代码
chmod +x /usr/local/src/install_nginx.sh
/usr/local/src/install_nginx.sh

set -e 的作用是任何一条命令失败就立即退出,避免依赖没装成功时继续执行,最后得到一个看似成功实则残缺的安装。

五、常见问题

现象 原因 解决
./configure 报 C compiler cc is not found 未装 gcc yum -y install gcc
报 the HTTP rewrite module requires the PCRE library 未装 pcre-devel yum -y install pcre-devel
报 SSL modules require the OpenSSL library 未装 openssl-devel yum -y install openssl openssl-devel
wget: command not found 未装 wget yum install wget
make 报 No rule to make target ./configure 未成功 回到上一步看报错,装完依赖重跑
启动后浏览器访问不到 防火墙未放行 80 端口 systemctl stop firewalld 或 firewall-cmd --zone=public --add-port=80/tcp --permanent && firewall-cmd --reload
报 Address already in use 80 端口被占用(Apache 等) `netstat -tunlp

防火墙那条要特别注意:生产环境不要直接 systemctl stop firewalld,正确做法是只放行需要的端口:

bash 复制代码
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports

API 速览

命令 / 参数 作用
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel 安装 Nginx 编译所需的全部依赖
wget <url> 从指定 URL 下载文件,支持断点续传
yum install wget 安装 wget 命令
tar -zxvf <file> 解压 .tar.gz 包(-z gzip、-x 解包、-v 显示过程、-f 指定文件)
./configure --prefix=<dir> 检查编译环境并生成 Makefile,--prefix 指定安装目录
./configure --with-http_ssl_module 启用 HTTPS 支持
./configure --with-http_v2_module 启用 HTTP/2
./configure --with-http_gzip_static_module 启用预压缩文件支持
make 按 Makefile 编译源码
make install 把编译产物安装到 --prefix 指定目录
make && make install 编译成功后立即安装
systemctl stop firewalld 关闭防火墙(仅测试环境)
firewall-cmd --add-port=80/tcp --permanent 永久放行 80 端口(生产推荐)
`netstat -tunlp grep :80`

官方文档

总结

Nginx 与 Tomcat 是配合关系而非替代关系 。Nginx 扛连接、管静态、做转发;Tomcat 跑 Java 业务逻辑。理解这一点,后面看到「Nginx 部署前端 + 反向代理到 8080」的架构就不会困惑。

并发能力来自模型而非硬件 。master 进程管 worker,worker 用 epoll 单进程维持数万连接------这与 Tomcat 一请求一线程是本质差异。生产上 worker_processes 设为 CPU 核数即可,盲目调大反而增加调度开销。

源码编译的核心是 ./configure 这一步 。它不装东西,只做环境检查和生成 Makefile。看懂它的 Configuration summary 输出,就知道每个文件最终落在哪里。缺依赖的报错都集中在这一步,按提示补 -devel 包即可。

--prefix 要记牢 。将来加模块重编译时必须用同一个 --prefix,否则会覆盖现有安装。想加 ssl 模块的话,第一次装就该带上 --with-http_ssl_module------事后加需要重新编译二进制并平滑替换。

防火墙不要一关了之 。测试环境 systemctl stop firewalld 图省事可以理解,生产必须精确到端口放行。

下一篇看 Nginx 安装后的目录结构,搞清楚 conf、html、logs、sbin 四个目录各自放什么,以及 nginx.conf 为什么是后续所有配置的唯一入口。

相关推荐
Eason_LYC1 小时前
一个公开密钥放倒一片:Apache Shiro RememberMe 反序列化漏洞
java·网络安全·渗透测试·shiro·漏洞复现·反序列化·ai安全
笨笨饿2 小时前
#141_ Codex 本地 AI 代理工具链工作流重构:Headroom、Scrapling、Archify 统一接入
开发语言·人工智能·stm32·单片机·嵌入式硬件·ui·重构
承渊政道4 小时前
星空组网+Python实战:在另一台电脑上查看CSV报表
运维·开发语言·python·csv·星空组网
Liquad Li6 小时前
Salesforce 主流认证方式、流程、适用场景
开发语言·salesforce
LOVE️YOU9 小时前
Python 中 Tuple 为什么有时可 Hash,有时不可 Hash?
开发语言·python·哈希算法
ym hyd 1119 小时前
试题库管理系统源码 Java+SpringBoot+Vue3 前后分离
java·vue.js·spring boot·毕设
李永奉9 小时前
C语言-指针函数与函数指针及回调函数的使用
c语言·开发语言
小oo呆9 小时前
【学习心得】迭代器和可迭代对象
开发语言·python
AC赳赳老秦10 小时前
采集行为合规自检:OpenClaw 自动校验 robots 协议与采集频率,规避违规采集风险
java·开发语言·c++·python·php·deepseek·openclaw