本文档详细说明了在无法访问外网的麒麟服务器 V10 系统上离线安装 Nginx 的完整方案,用于部署 DMS 系统(Django+Vue 前后端分离架构)。
在有网络连接的相同系统版本计算机上执行以下步骤:
# 1. 添加 Nginx 官方仓库
wget -P /tmp http://nginx.org/packages/rhel/7/noarch/RPMS/nginx-release-rhel-7-0.el.noarch.rpm
# 2. 下载 Nginx 主程序包
yum install --downloadonly --downloaddir=/tmp nginx
# 或者直接下载特定版本的 Nginx RPM 包
wget -P /tmp http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.24.0-1.el7.ngx.x86_64.rpm
将以下文件复制到麒麟服务器: - nginx-release-rhel-7-0.el.noarch.rpm - nginx-1.24.0-1.el7.ngx.x86_64.rpm (或最新版本)
# 1. 安装 Nginx 仓库配置
sudo rpm -ivh nginx-release-*.rpm
# 2. 安装 Nginx
sudo rpm -ivh nginx-*.rpm
# 3. 验证安装
nginx -v
由于 HTTPD 和 Nginx 都会使用 80/443 端口,需要先停止 HTTPD 服务:
# 1. 查看 HTTPD 状态
sudo systemctl status httpd
# 2. 停止 HTTPD 服务
sudo systemctl stop httpd
# 3. 禁用 HTTPD 自动启动
sudo systemctl disable httpd
# 4. 检查端口占用情况
sudo netstat -tlnp | grep :80
sudo netstat -tlnp | grep :443
# 1. 启动 Nginx
sudo systemctl start nginx
# 2. 设置开机自启
sudo systemctl enable nginx
# 3. 检查 Nginx 状态
sudo systemctl status nginx
# 检查 Nginx 配置语法
sudo nginx -t
# 访问默认页面验证
curl http://localhost
# 检查进程
ps aux | grep nginx
# 创建配置目录
# 检查 Nginx 配置和日志目录(安装 Nginx 时通常会自动创建)
# 一般情况下,/etc/nginx/conf.d 和 /var/log/nginx 在安装后已存在
ls -la /etc/nginx/conf.d
ls -la /var/log/nginx
# 如果上述目录不存在,才需要手动创建:
# sudo mkdir -p /etc/nginx/conf.d
# sudo mkdir -p /var/log/nginx
# 或者,如果您希望将所有 Nginx 相关文件集中管理,可以创建统一目录:
# sudo mkdir -p /usr/share/nginx/conf.d
# sudo mkdir -p /usr/share/nginx/logs
# 创建 DMS 前端文件目录,这里我们使用 /var/www/dms(与 httpd 一致的标准路径)
sudo mkdir -p /var/www/dms
# 或者您也可以选择使用 /usr/share/nginx/html(Nginx默认路径)
# sudo mkdir -p /usr/share/nginx/html
将dms.conf 文件保存在 /etc/nginx/conf.d/ 目录下,并根据需要进行修改。不要修改默认的主配置文件nginx.conf 文件。
sudo nano /etc/nginx/conf.d/dms.conf
主要修改以下内容:
C:/nginx-1.28.0/cert/ 改为 /etc/ssl/certs//var/www/dms/示例配置,详细配置请联系工程师:
server { listen 80; server_name 192.168.0.124 localhost; # 支持特定IP和所有其他域名/IP访问 # 将所有HTTP请求永久重定向到HTTPS return 301 https://$host$request_uri; } # HTTPS服务器块 server { listen 443 ssl; server_name 192.168.0.124 localhost; # 支持特定IP和所有其他域名/IP访问 # SSL证书配置 ssl_certificate /etc/ssl/certs/dms.crt; ssl_certificate_key /etc/ssl/certs/dms.key; ssl_protocols TLSv1.2 TLSv1.3; # 仅启用TLS 1.2/1.3,彻底禁用TLS1.0/1.1和SSL ssl_prefer_server_ciphers on; # 优先使用服务器端加密套件 ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; # 高安全加密套件(仅兼容TLS1.2+) ssl_session_timeout 1d; # 延长会话超时,提升性能 ssl_session_cache shared:SSL_DMS:10m; # 优化会话缓存 ssl_session_tickets off; # 关闭会话票证,提升安全性 # ========== Vue前端页面 ========== location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; # 支持Vue history模式 } # ========== Django后端静态文件(Admin、DRF等)========== location /static/ { alias /opt/DMS/staticfiles/; # Django collectstatic收集目录 expires 30d; add_header Cache-Control "public"; access_log off; # 安全加固:禁止执行脚本 location ~* \.(php|py|pl|cgi)$ { deny all; } } # ========== Django媒体文件(用户上传)========== location /media/ { alias /opt/DMS/public/uploads/; # 用户上传文件目录 expires 30d; add_header Cache-Control "public"; # 安全加固:禁止执行脚本 location ~* \.(php|py|pl|cgi)$ { deny all; } } # ========== Vue前端静态资源(js/css/图片等)========== # 注意:这个规则只匹配 /usr/share/nginx/html 下的文件 # 由于 ^~ 优先级的 /static/ 和 /media/ 已经定义,不会冲突 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { root /usr/share/nginx/html; # 明确指定root expires 1y; add_header Cache-Control "public, immutable"; try_files $uri =404; } # ========== 错误页面配置 ========== error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; # 错误页面放在前端目录 } # API 请求代理到后端Django服务 # 注意:proxy_pass末尾的斜杠会重写路径,将/api/captcha转发为/captcha location ^~ /api/ { proxy_pass http://127.0.0.1:9090/; # 代理到后端Django服务地址(注意末尾斜杠) proxy_set_header Host $host; # 设置Host头 proxy_set_header X-Real-IP $remote_addr; # 设置真实IP头 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置转发IP链 proxy_set_header X-Forwarded-Proto $scheme; # 设置转发协议(HTTP/HTTPS) } # SSE特定配置 代理到后端Django服务地址 location ^~ /sse/dashboard/ { proxy_pass http://127.0.0.1:9090/dashboard/; 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; # SSE特定配置 proxy_set_header Connection ''; proxy_http_version 1.1; proxy_buffering off; proxy_cache off; } }
如果麒麟服务器启用了防火墙,需要开放 80 和 443 端口:
# 使用 firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# 启动 Nginx
sudo systemctl start nginx
# 停止 Nginx
sudo systemctl stop nginx
# 重启 Nginx
sudo systemctl restart nginx
# 重新加载配置
sudo nginx -s reload
# 测试配置文件
sudo nginx -t
# 设置开机自启
sudo systemctl enable nginx
# 取消开机自启
sudo systemctl disable nginx
如果 Nginx 启动失败,检查端口占用:
sudo netstat -tlnp | grep :80
sudo netstat -tlnp | grep :443
使用以下命令检查配置文件语法:
sudo nginx -t
sudo systemctl status nginx
/etc/ssl/certs/ 目录/etc/nginx/conf.d/ 目录sudo nginx -s reload
本方案提供了在麒麟服务器 V10 上离线安装 Nginx 的完整步骤,确保 DMS 系统能够正常部署并提供 HTTPS 服务。通过合理的配置,Nginx 将作为反向代理服务器,处理前端静态资源请求和后端 API 请求的转发。
PM2.5/10、温湿度、有害气体等实时监测