Rocky、OpenEuler安装nginx并设置开机自启 概述整体流程分为三大块1. 安装 Nginx 2. 配置 Nginx3套生产可用配置模板 3. 配套补充知识点 1Nginx 负载均衡配置 2Nginx HTTPS 证书配置 3IDEA2023 多 SpringBoot 实例运行一、安装 NginxCentOS/Rocky Linux dnf 编译安装1. 安装编译依赖首次安装依赖dnfinstall-ygcc-c pcre pcre-devel zlib zlib-devel openssl openssl-devel若升级 openSSL高版本则升级排除仅升级nginx 不建议执行依赖安装dnfinstall-ygcc-c pcre pcre-devel zlib zlib-devel--excludeopenssl,openssl-devel,openssl-libs2. 官网下载稳定版 Nginx官网地址http://nginx.org/en/download.html脚本一键下载2026-07-06 更新1.30.3 稳定版​​​​cd/usr/localwgethttps://nginx.org/download/nginx-1.30.3.tar.gz3. 解压源码包无 tar 工具先安装dnfinstalltar解压命令tar-xzvfnginx-1.30.3.tar.gz4. 编译安装 Nginx4.1 进入源码目录cd/usr/local/nginx-1.30.34.2 configure 编译参数带完整模块./configure--prefix/usr/local/nginx\--userwww\--groupwww\--with-http_stub_status_module\--with-http_ssl_module\--with-http_gzip_static_module\--with-http_v2_module\--with-file-aio\--with-threads\--with-http_realip_module\--with-pcre\--with-pcre-jit参数说明参数作用--prefix/usr/local/nginxNginx 安装根目录--userwww运行进程用户--groupwww运行进程用户组--with-http_stub_status_module监控状态模块--with-http_ssl_moduleHTTPS SSL 支持--with-http_gzip_static_module静态资源预压缩--with-http_v2_moduleHTTP/2 协议--with-file-aio文件异步IO提升静态文件性能--with-threads线程并发支持--with-http_realip_module反向代理获取真实客户端IP--with-pcre --with-pcre-jit正则JIT加速4.3 编译安装makeinstall查看安装目录whereisnginx安装完成后可删除源码包cd..rm-rf/usr/local/nginx-1.30.35. 配置系统自启动 运行用户5.1 创建 www 用户与用户组groupadd-fwwwuseradd-gwww www5.2 创建 systemd 服务文件cd/etc/systemd/systemvinginx.servicenginx.service完整内容[Unit] Descriptionnginx-The High-performance HTTP Server Afternetwork.target [Service] Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStartPre/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload/usr/local/nginx/sbin/nginx -s reload ExecStop/usr/local/nginx/sbin/nginx -s stop PrivateTmptrue [Install] WantedBymulti-user.target6. 加载服务 设置开机自启# 重载systemd服务列表systemctl daemon-reload# 启动Nginxsystemctl start nginx.service# 查看运行状态systemctl status nginx.service# 设置开机自启systemctlenablenginx.service7. 开机自启验证 防火墙放行7.1 重启验证端口监听reboot# 安装netstat工具dnfinstallnet-tools# 查看80端口占用netstat-tlunp|grep807.2 防火墙永久放行80端口firewall-cmd--zonepublic --add-port80/tcp--permanentfirewall-cmd--reload验证浏览器直接访问服务器IP测试页面8. Nginx 运维常用操作8.1 基础启停、重载、校验命令# 启动systemctl start nginx.service# 平滑重载配置不中断业务systemctl reload nginx.service# 重启服务systemctl restart nginx.service# 正常停止等待现有连接处理完systemctl stop nginx.service# 强制快速停止/usr/local/nginx/sbin/nginx-squit# 查看运行状态systemctl status nginx.service# 校验配置文件是否合法/usr/local/nginx/sbin/nginx-t# 查看编译参数、已启用模块/usr/local/nginx/sbin/nginx-V8.2 简易重装流程先停止服务再删除nginx编译后的位置# 停止服务 systemctl stop nginx.service # 删除编译后的源码 rm -rf /usr/local/nginx再按本章安装步骤重新编译安装最后启动服务。# 重新编译覆盖安装执行本章4.1~4.3步骤# 启动服务systemctl start nginx.service二、Nginx 生产配置模板模板1基础 Vue 前后端分离 HTTP 配置通用无特殊业务说明开启gzip、大文件超时、文件上传限制、后端接口反向代理、前端history路由刷新兼容# Nginx 进程数一般设置为和 CPU 核数一样可设置 auto会自动根据 CPU 核数分配适当的进程数 worker_processes auto; # 日志严重程度debug info notice warn error默认 crit alert emerg生产一般如下3种 error_log logs/error.log; # Nginx 的错误日志存放目录 # error_log logs/error.log notice; # error_log logs/error.log info; #pid logs/nginx.pid; # Nginx 服务启动时的 pid 存放位置 events { # 根据操作系统自动选择建议指定事件驱动模型避免 Nginx 误判环境 use epoll; # 每个进程允许最大并发数 # 小规模的服务器512或1024中等规模的服务器2048或4096大规模的服务器8192 或更高 # 考虑到内存占用和CPU的利用率一般建议不要将worker_connections设置得过高 worker_connections 2048; # 默认off高并发下建议开让 worker 每次尽量多 accept 新连接 multi_accept on; # 默认on,避免多个 worker 同时抢占 accept减少惊群现象 accept_mutex on; } http { include mime.types;# 文件扩展名与类型映射表 default_type application/octet-stream;# 默认文件类型 # 设置日志模式 #log_format main $remote_addr - $remote_user [$time_local] $request # $status $body_bytes_sent $http_referer # $http_user_agent $http_x_forwarded_for; # 全局关闭访问日志 access_log off; #access_log logs/access.log main; # Nginx访问日志存放位置 sendfile on;# 开启高效传输模式 #tcp_nopush on;# 减少网络报文段的数量 keepalive_timeout 30;# 保持连接的时间也叫超时时间单位秒 gzip on;#表示开启压缩功能 gzip_static on;#静态文件压缩开启 # 设置压缩的最低文件大小默认值是 20 字节 gzip_min_length 5k;# 设置为 1KB 或更大避免对小文件压缩 # 设置使用的压缩算法一般是 gzip gzip_comp_level 5;# 范围是 1-9数字越大压缩率越高但占用 CPU 更多 # 开启对特定文件类型的压缩(不建议压缩紧凑格式图片) gzip_types text/plain text/css application/javascript application/json application/xml text/xml application/xmlrss text/javascript application/font-woff2 application/font-woff application/font-otf; # 不压缩的 MIME 类型 gzip_disable msie6;# 禁止压缩 IE6 浏览器 # 压缩缓存控制 gzip_vary on;# 设置响应头 Vary: Accept-Encoding # 压缩后文件传输 gzip_buffers 4 16k;# 设定缓冲区大小 #认证后台 server { listen 80; # 88 ssl 本服务监听的端口号 server_name localhost; # 主机名称 client_body_timeout 3600s; # 客户端上传 body 超时 send_timeout 3600s; # 向客户端发送响应超时 client_max_body_size 2148m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 3600; # 1h 保证大文件能正常传输 proxy_send_timeout 3600; # 1h 保证大文件能正常传输 proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; location / { # root 规定了通过监听的端口号访问的文件目录 root /usr/share/nginx/html/grain/dist; # 配置资源重新跳转防止刷新后页面丢失 try_files $uri $uri/ /index.html; # index 规定了该目录下指定哪个文件 index index.html index.htm; } # 配置后端接口的跨域代理 # 对于路径为 api 的接口帮助他跳转到指定的地址 location /api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 本机上运行的后端接口 proxy_pass http://jar-grain-9000:9000/; } location /status{ stub_status on; } } }模板2Vite Vue 增强版含缓存策略、SSE长连接、文件上传下载优化新增特性index.html 禁止缓存发版即时生效assets 静态资源缓存7天SSE 消息推送长连接专用配置文件上传/下载关闭缓冲区直传优化# Nginx 进程数一般设置为和 CPU 核数一样可设置 auto会自动根据 CPU 核数分配适当的进程数 worker_processes auto; # 日志严重程度debug info notice warn error默认 crit alert emerg生产一般如下3种 error_log logs/error.log; # Nginx 的错误日志存放目录 # error_log logs/error.log notice; # error_log logs/error.log info; #pid logs/nginx.pid; # Nginx 服务启动时的 pid 存放位置 events { # 根据操作系统自动选择建议指定事件驱动模型避免 Nginx 误判环境 use epoll; # 每个进程允许最大并发数 # 小规模的服务器512或1024中等规模的服务器2048或4096大规模的服务器8192 或更高 # 考虑到内存占用和CPU的利用率一般建议不要将worker_connections设置得过高 worker_connections 2048; # 默认off高并发下建议开让 worker 每次尽量多 accept 新连接 multi_accept on; # 默认on,避免多个 worker 同时抢占 accept减少惊群现象 accept_mutex on; } http { include mime.types;# 文件扩展名与类型映射表 default_type application/octet-stream;# 默认文件类型 # 设置日志模式 #log_format main $remote_addr - $remote_user [$time_local] $request # $status $body_bytes_sent $http_referer # $http_user_agent $http_x_forwarded_for; # 全局关闭访问日志 access_log off; #access_log logs/access.log main; # Nginx访问日志存放位置 sendfile on;# 开启高效传输模式 #tcp_nopush on;# 减少网络报文段的数量 keepalive_timeout 30;# 保持连接的时间也叫超时时间单位秒 gzip on;#表示开启压缩功能 gzip_static on;#静态文件压缩开启 # 设置压缩的最低文件大小默认值是 20 字节 gzip_min_length 5k;# 设置为 1KB 或更大避免对小文件压缩 # 设置使用的压缩算法一般是 gzip gzip_comp_level 5;# 范围是 1-9数字越大压缩率越高但占用 CPU 更多 # 开启对特定文件类型的压缩(不建议压缩紧凑格式图片) gzip_types text/plain text/css application/javascript application/json application/xml text/xml application/xmlrss text/javascript application/font-woff2 application/font-woff application/font-otf; # 不压缩的 MIME 类型 gzip_disable msie6;# 禁止压缩 IE6 浏览器 # 压缩缓存控制 gzip_vary on;# 设置响应头 Vary: Accept-Encoding # 压缩后文件传输 gzip_buffers 4 16k;# 设定缓冲区大小 #认证后台 server { listen 80; # 88 ssl 本服务监听的端口号 server_name localhost; # 主机名称 client_body_timeout 3600s; # 客户端上传 body 超时 send_timeout 3600s; # 向客户端发送响应超时 client_max_body_size 2148m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 3600; # 1h 保证大文件能正常传输 proxy_send_timeout 3600; # 1h 保证大文件能正常传输 proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; # 首页 index.html — 禁止缓存保证每次发版生效 location /index.html { root /opt/sm-crypto/process-center-web/dist; add_header Cache-Control no-cache, no-store, must-revalidate; add_header Pragma no-cache; add_header Expires 0; try_files $uri 404; } # 静态资源 /assets/缓存7天不带immutable允许刷新更新 location /assets/ { root /opt/sm-crypto/process-center-web/dist; expires 7d; add_header Cache-Control public; } location / { # root 规定了通过监听的端口号访问的文件目录 root /usr/share/nginx/html/grain/dist; # 配置资源重新跳转防止刷新后页面丢失 try_files $uri $uri/ /index.html; # index 规定了该目录下指定哪个文件 index index.html index.htm; } # SSE 专用 location /api/common/stream/connect/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # sse配置 proxy_http_version 1.1; # SSE 必须 HTTP/1.1 proxy_set_header Connection ; # 保持长连接 chunked_transfer_encoding off; # 保证消息实时 # 本机上运行的后端接口 proxy_pass http://jar-grain-9000:9000/common/stream/connect/; } # 文件上传/预览接口关闭缓冲区 location /api/common/file/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 文件接口 proxy_request_buffering off; # 上传直传 proxy_buffering off; # 下载直传 proxy_max_temp_file_size 0; # 禁止写临时文件 # 禁止 Nginx gzip 压缩 gzip off; # 本机上运行的后端接口 proxy_pass http://jar-grain-9000:9000/common/file/; } # 配置后端接口的跨域代理 # 对于路径为 api 的接口帮助他跳转到指定的地址 location /api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 本机上运行的后端接口 proxy_pass http://jar-grain-9000:9000/; } location /status{ stub_status on; } } }模板3完整通用 Nginx 配置大全含 upstream 负载均衡、多虚拟主机、IP黑白名单# 以下是全局段配置 #user administrator administrators; #配置用户或者组默认为nobody nobody。 #worker_processes 2; #设置进程数默认为1 #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址 error_log log/error.log debug; #制定日志路径级别debug|info|notice|warn|error|crit|alert|emerg # events段配置信息 events { accept_mutex on; #设置网路连接序列化防止惊群现象发生默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接默认为off #use epoll; #事件驱动模型select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大连接数默认为512 } # http、配置请求信息 http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型默认为text/plain #access_log off; #取消服务日志 log_format myFormat $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; #自定义格式 access_log log/access.log myFormat; #combined为日志格式的默认值 sendfile on; #允许sendfile方式传输文件默认为off可以在http块server块location块。 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值默认为0即不设上限。 keepalive_timeout 65; #连接超时时间默认为75s可以在httpserverlocation块。 upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备 } error_page 404 https://www.baidu.com; #错误页 # 第一个Server区块开始表示一个独立的虚拟主机站点 server { keepalive_requests 120; #单连接请求上限次数。 listen 4545; #监听端口 server_name 127.0.0.1; #监听地址 location ~*^.$ { #请求的url过滤正则匹配~为区分大小写~*为不区分大小写。 #root path; #根目录 #index vv.txt; #设置默认页 proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表 deny 127.0.0.1; #拒绝的ip allow 172.18.5.54; #允许的ip } } }三、配套补充知识点1. Nginx 负载均衡配置参考文档CSDN-Nginx4种负载均衡实现方式 https://blog.csdn.net/m0_64210833/article/details/131954254内容包含配置文件解析、轮询/加权轮询/ip_hash/最少连接四种负载策略实操。2. Nginx HTTPS 证书配置自签名证书参考文档CSDN-OpenSSL生成Nginx自签名证书https://blog.csdn.net/qq_26408545/article/details/135966610覆盖 openssl req 核心参数、自签名证书生成、Nginx ssl 模块绑定证书配置。3. IDEA2023 运行多 SpringBoot 实例参考文档CSDN-IDEA2023 运行多 SpringBoot 实例https://blog.csdn.net/qq_26408545/article/details/136801288