欢迎光临
我们一直在努力

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

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

纲要

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

#mermaid-svg-ZzChq7vmt9qmBFqW{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-ZzChq7vmt9qmBFqW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZzChq7vmt9qmBFqW .error-icon{fill:#552222;}#mermaid-svg-ZzChq7vmt9qmBFqW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZzChq7vmt9qmBFqW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZzChq7vmt9qmBFqW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZzChq7vmt9qmBFqW .marker.cross{stroke:#333333;}#mermaid-svg-ZzChq7vmt9qmBFqW svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZzChq7vmt9qmBFqW p{margin:0;}#mermaid-svg-ZzChq7vmt9qmBFqW .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster-label text{fill:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster-label span{color:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster-label span p{background-color:transparent;}#mermaid-svg-ZzChq7vmt9qmBFqW .label text,#mermaid-svg-ZzChq7vmt9qmBFqW span{fill:#333;color:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW .node rect,#mermaid-svg-ZzChq7vmt9qmBFqW .node circle,#mermaid-svg-ZzChq7vmt9qmBFqW .node ellipse,#mermaid-svg-ZzChq7vmt9qmBFqW .node polygon,#mermaid-svg-ZzChq7vmt9qmBFqW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZzChq7vmt9qmBFqW .rough-node .label text,#mermaid-svg-ZzChq7vmt9qmBFqW .node .label text,#mermaid-svg-ZzChq7vmt9qmBFqW .image-shape .label,#mermaid-svg-ZzChq7vmt9qmBFqW .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZzChq7vmt9qmBFqW .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZzChq7vmt9qmBFqW .rough-node .label,#mermaid-svg-ZzChq7vmt9qmBFqW .node .label,#mermaid-svg-ZzChq7vmt9qmBFqW .image-shape .label,#mermaid-svg-ZzChq7vmt9qmBFqW .icon-shape .label{text-align:center;}#mermaid-svg-ZzChq7vmt9qmBFqW .node.clickable{cursor:pointer;}#mermaid-svg-ZzChq7vmt9qmBFqW .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZzChq7vmt9qmBFqW .arrowheadPath{fill:#333333;}#mermaid-svg-ZzChq7vmt9qmBFqW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZzChq7vmt9qmBFqW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZzChq7vmt9qmBFqW .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZzChq7vmt9qmBFqW .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZzChq7vmt9qmBFqW .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZzChq7vmt9qmBFqW .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster text{fill:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW .cluster span{color:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW 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-ZzChq7vmt9qmBFqW .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZzChq7vmt9qmBFqW rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZzChq7vmt9qmBFqW .icon-shape,#mermaid-svg-ZzChq7vmt9qmBFqW .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZzChq7vmt9qmBFqW .icon-shape p,#mermaid-svg-ZzChq7vmt9qmBFqW .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZzChq7vmt9qmBFqW .icon-shape .label rect,#mermaid-svg-ZzChq7vmt9qmBFqW .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZzChq7vmt9qmBFqW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZzChq7vmt9qmBFqW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZzChq7vmt9qmBFqW :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/nginxconf html logs sbin

一、Nginx 是什么

官方定义

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

拆开看这三重身份:

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

与 Tomcat 的区别

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

维度NginxTomcat
核心职责 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-1LWkBrpbsJK7ztiT{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-1LWkBrpbsJK7ztiT .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1LWkBrpbsJK7ztiT .error-icon{fill:#552222;}#mermaid-svg-1LWkBrpbsJK7ztiT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1LWkBrpbsJK7ztiT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1LWkBrpbsJK7ztiT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1LWkBrpbsJK7ztiT .marker.cross{stroke:#333333;}#mermaid-svg-1LWkBrpbsJK7ztiT svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1LWkBrpbsJK7ztiT p{margin:0;}#mermaid-svg-1LWkBrpbsJK7ztiT .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster-label text{fill:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster-label span{color:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster-label span p{background-color:transparent;}#mermaid-svg-1LWkBrpbsJK7ztiT .label text,#mermaid-svg-1LWkBrpbsJK7ztiT span{fill:#333;color:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT .node rect,#mermaid-svg-1LWkBrpbsJK7ztiT .node circle,#mermaid-svg-1LWkBrpbsJK7ztiT .node ellipse,#mermaid-svg-1LWkBrpbsJK7ztiT .node polygon,#mermaid-svg-1LWkBrpbsJK7ztiT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1LWkBrpbsJK7ztiT .rough-node .label text,#mermaid-svg-1LWkBrpbsJK7ztiT .node .label text,#mermaid-svg-1LWkBrpbsJK7ztiT .image-shape .label,#mermaid-svg-1LWkBrpbsJK7ztiT .icon-shape .label{text-anchor:middle;}#mermaid-svg-1LWkBrpbsJK7ztiT .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-1LWkBrpbsJK7ztiT .rough-node .label,#mermaid-svg-1LWkBrpbsJK7ztiT .node .label,#mermaid-svg-1LWkBrpbsJK7ztiT .image-shape .label,#mermaid-svg-1LWkBrpbsJK7ztiT .icon-shape .label{text-align:center;}#mermaid-svg-1LWkBrpbsJK7ztiT .node.clickable{cursor:pointer;}#mermaid-svg-1LWkBrpbsJK7ztiT .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-1LWkBrpbsJK7ztiT .arrowheadPath{fill:#333333;}#mermaid-svg-1LWkBrpbsJK7ztiT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1LWkBrpbsJK7ztiT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1LWkBrpbsJK7ztiT .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1LWkBrpbsJK7ztiT .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-1LWkBrpbsJK7ztiT .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1LWkBrpbsJK7ztiT .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster text{fill:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT .cluster span{color:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT 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-1LWkBrpbsJK7ztiT .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-1LWkBrpbsJK7ztiT rect.text{fill:none;stroke-width:0;}#mermaid-svg-1LWkBrpbsJK7ztiT .icon-shape,#mermaid-svg-1LWkBrpbsJK7ztiT .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1LWkBrpbsJK7ztiT .icon-shape p,#mermaid-svg-1LWkBrpbsJK7ztiT .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-1LWkBrpbsJK7ztiT .icon-shape .label rect,#mermaid-svg-1LWkBrpbsJK7ztiT .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1LWkBrpbsJK7ztiT .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-1LWkBrpbsJK7ztiT .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-1LWkBrpbsJK7ztiT :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

Tomcat模型

Tomcat

线程 1 → 连接 1

线程 2 → 连接 2

线程 N → 连接 N

Nginx模型

master 进程

worker 进程 1epoll 管理数万连接

worker 进程 2epoll 管理数万连接

实际项目中的常识:把 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 语言开发,编译前必须有编译环境,同时它依赖几个第三方库:

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 是否可用:

wget
# -bash: wget: command not found

若提示 command not found,先安装:

yum install wget

然后下载:

wget https://nginx.org/download/nginx-1.16.1.tar.gz

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

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

第三步:解压

tar -zxvf nginx-1.16.1.tar.gz

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

第四步:配置编译环境

cd nginx-1.16.1
./configure –prefix=/usr/local/nginx

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

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

输出大致如下:

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(现代站点基本必需),应加上:

./configure –prefix=/usr/local/nginx \\
–with-http_ssl_module \\
–with-http_v2_module \\
–with-http_gzip_static_module

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

第五步:编译并安装

make && make install

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

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

验证安装

cd /usr/local/nginx
ls
# conf html logs sbin

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

四、完整安装脚本

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

#!/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,执行:

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,正确做法是只放行需要的端口:

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 查看占用 80 端口的进程

官方文档

  • Nginx 官网:https://nginx.org/
  • Nginx 下载页:https://nginx.org/en/download.html
  • Nginx 官方安装文档(Building nginx from Sources):https://nginx.org/en/docs/configure.html
  • Nginx 核心模块指令参考:https://nginx.org/en/docs/ngx_core_module.html
  • Nginx 官方管理信号文档:https://nginx.org/en/docs/control.html

总结

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 为什么是后续所有配置的唯一入口。

赞(0)
未经允许不得转载:171主机测评 » Java 项目实战: 外卖平台优化-Nginx概述与源码编译安装
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址