一、官方下载链接
1. 源码包下载(最新稳定版)
wgethttps://nginx.org/download/nginx-1.30.0.tar.gz这是截至目前的最新稳定版(Stable version),生产环境推荐使用稳定版。
2. 预编译包(推荐,通过官方仓库安装)
如果你使用的是RHEL/CentOS/AlmaLinux/Rocky Linux系列:
# 创建官方 nginx 仓库sudotee/etc/yum.repos.d/nginx.repo<<'EOF' [nginx-stable] name=nginx stable repo baseurl=https://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=https://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true EOF# 安装sudoyuminstallnginx如果你使用的是Ubuntu/Debian系列:
# 安装依赖sudoaptupdatesudoaptinstallcurlgnupg2 ca-certificates lsb-release# 导入官方 GPG 密钥curl-fsSLhttps://nginx.org/keys/nginx_signing.key|sudogpg--dearmor-o/usr/share/keyrings/nginx-archive-keyring.gpg# 添加 stable 仓库echo"deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://nginx.org/packages/ubuntu$(lsb_release-cs)nginx"|sudotee/etc/apt/sources.list.d/nginx.list# 设置仓库优先级echo-e"Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n"|sudotee/etc/apt/preferences.d/99nginx# 安装sudoaptupdatesudoaptinstallnginx二、源码编译部署教程(最灵活的方式)
如果你的系统包管理器不可用,或者需要自定义模块,推荐源码编译安装:
步骤 1:安装编译依赖
RHEL/CentOS 系列:
sudoyum groupinstall"Development Tools"sudoyuminstallpcre pcre-devel zlib zlib-devel openssl openssl-develUbuntu/Debian 系列:
sudoaptupdatesudoaptinstallbuild-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev步骤 2:下载并解压源码
cd/usr/local/srcwgethttps://nginx.org/download/nginx-1.30.0.tar.gztar-zxvfnginx-1.30.0.tar.gzcdnginx-1.30.0步骤 3:配置编译参数
./configure\--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_v2_module\--with-http_gzip_static_module\--with-http_stub_status_module\--with-pcre\--with-zlib\--with-openssl常用模块说明:
--with-http_ssl_module:HTTPS 支持--with-http_v2_module:HTTP/2 支持--with-http_gzip_static_module:Gzip 静态压缩--with-http_stub_status_module:Nginx 状态监控
如果报错:就找报错原因
cd/data/cm/nginx-1.30.0 ./configure\--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_v2_module\--with-http_gzip_static_module\--with-http_stub_status_module\--with-pcre\--with-zlib\--with-opensslcd/data/cm/nginx-1.30.0 ./configure\--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_v2_module\--with-http_gzip_static_module\--with-http_stub_status_module\--with-pcre=/data/cm/pcre2-10.45\--with-zlib=/data/cm/zlib-1.3.1\--with-openssl=/data/cm/openssl-3.5.2步骤 4:编译并安装
make-j$(nproc)sudomakeinstall步骤 5:创建软链接(方便全局调用)
sudoln-s/usr/local/nginx/sbin/nginx /usr/local/bin/nginx步骤 6:验证安装
nginx-v# 输出类似:nginx version: nginx/1.30.0# 启动 nginx/usr/local/nginx/sbin/nginx# 停止 nginx(快速停止)/usr/local/nginx/sbin/nginx-sstop# 优雅停止(处理完当前请求再停)/usr/local/nginx/sbin/nginx-squit# 重载配置(修改配置后无需重启)/usr/local/nginx/sbin/nginx-sreload# 重新打开日志文件(日志切割时用)/usr/local/nginx/sbin/nginx-sreopen# 检查配置文件语法/usr/local/nginx/sbin/nginx-t# 启动/usr/local/nginx/sbin/nginx# 查看进程ps-ef|grepnginx# 测试 10015 端口curl-Ihttp://127.0.0.1:10015/步骤 7:创建 systemd 服务文件
sudotee/etc/systemd/system/nginx.service<<'EOF' [Unit] Description=nginx - high performance web server After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOFsudosystemctl daemon-reloadsudosystemctlenablenginxsudosystemctl start nginx三、快速验证
# 检查 nginx 是否运行curl-I127.0.0.1# 预期输出:# HTTP/1.1 200 OK# Server: nginx/1.30.0四、重要文件路径
如果你按照之前的编译参数(--prefix=/usr/local/nginx)安装,配置文件在:
/usr/local/nginx/conf/nginx.conf相关重要路径一览
| 用途 | 路径 |
|---|---|
| 主配置文件 | /usr/local/nginx/conf/nginx.conf |
| 子配置文件目录 | /usr/local/nginx/conf/ |
| 默认网站根目录 | /usr/local/nginx/html/ |
| 访问日志 | /usr/local/nginx/logs/access.log |
| 错误日志 | /usr/local/nginx/logs/error.log |
| PID 文件 | /usr/local/nginx/logs/nginx.pid |
| 二进制文件 | /usr/local/nginx/sbin/nginx |
快速查看/编辑配置文件
# 查看配置文件cat/usr/local/nginx/conf/nginx.conf# 编辑配置文件(用 vi 或 nano)vi/usr/local/nginx/conf/nginx.conf# 检查配置文件语法是否正确nginx-t# 重载配置(修改后无需重启)nginx-sreload如果你之前通过 yum/apt 安装过 nginx
如果系统里还残留着之前的 nginx 安装,配置文件可能在/etc/nginx/nginx.conf。但按照你当前的编译安装路径,生效的是/usr/local/nginx/conf/nginx.conf。
nginx.conf配置示例:
user root;worker_processes auto;error_log /usr/local/nginx/logs/error.log warn;pid /usr/local/nginx/logs/nginx.pid;events{worker_connections1024;}http{include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout65;server{listen80;server_name localhost;location /{root html;index index.html index.htm;}error_page500502503504/50x.html;location=/50x.html{root html;}}upstream cm_backend{server10.222.1.30:3000;}server{listen10015;server_name _;client_max_body_size 1024M;location /api/ws/doc-chat{proxy_pass http://cm_backend/api/ws/doc-chat;proxy_http_version1.1;proxy_set_header Upgrade$http_upgrade;proxy_set_header Connection"upgrade";proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;proxy_read_timeout 86400s;proxy_send_timeout 86400s;}location /api/ws/question/answer{proxy_pass http://cm_backend/api/ws/question/answer;proxy_http_version1.1;proxy_set_header Upgrade$http_upgrade;proxy_set_header Connection"upgrade";proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;proxy_read_timeout 86400s;proxy_send_timeout 86400s;}location /api/{proxy_pass http://cm_backend;proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 600s;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;}location /{root /usr/local/nginx/html/smart;index index.html index.htm;try_files$uri$uri/ /index.html;}error_page500502503504/50x.html;location=/50x.html{root html;}}}