>
← 返回投肯智能知识库首页

信创环境下Nginx反向代理配置与安全防护

难度:中级 阅读时间:约15分钟 更新日期:2026年6月

目录

    背景

    信创环境(国产化替代环境)正在成为政府、央企、金融机构的标配。在这些场景下,Web服务通常部署在鲲鹏、飞腾等国产CPU服务器上,运行着银河麒麟、统信UOS等国产操作系统。由于部分开源软件在国产平台上的兼容性问题,Nginx作为最常用的反向代理工具,在信创环境下的正确配置变得尤为重要。

    本文面向需要在信创服务器上部署Web服务的运维工程师,讲解如何在国产操作系统上正确安装、配置Nginx反向代理,并且做好安全防护。适合已经对Linux有基本了解,但需要在信创平台落地的技术人员。

    一、信创环境下的Nginx安装

    在信创环境下安装Nginx主要有两种方式:包管理器安装和编译安装。包管理器安装最简单,但部分国产系统的默认仓库版本较老;编译安装可以获取最新稳定版,功能更完整。

    1.1 银河麒麟Kylin系统

    银河麒麟Kylin OS基于Debian/Ubuntu衍生,安装Nginx比较简单:

    # 更新软件源
    sudo apt update
    
    # 安装Nginx
    sudo apt install nginx -y
    
    # 验证安装
    nginx -v
    sudo systemctl start nginx
    sudo systemctl enable nginx
    提示:麒麟V10(基于Debian)使用apt安装,麒麟V10 SP1/SP2(基于Kylin社区版)同样适用。若系统为龙蜥Anolis社区版,则使用yum/dnf。

    1.2 统信UOS专业版

    统信UOS基于Debian系,安装方式相同:

    sudo apt update
    sudo apt install nginx -y
    sudo systemctl enable nginx --now

    1.3 编译安装获取最新稳定版

    如果需要最新版本或特定模块(如OpenWAF),可以从源码编译:

    # 安装编译依赖
    sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
    
    # 下载Nginx最新稳定版
    cd /tmp
    wget http://nginx.org/download/nginx-1.26.1.tar.gz
    tar -xzf nginx-1.26.1.tar.gz
    cd nginx-1.26.1
    
    # 编译配置
    ./configure --prefix=/etc/nginx \
      --sbin-path=/usr/sbin/nginx \
      --modules-path=/usr/lib/nginx/modules \
      --conf-path=/etc/nginx/nginx.conf \
      --error-log-path=/var/log/nginx/error.log \
      --pid-path=/var/run/nginx.pid \
      --with-http_ssl_module \
      --with-http_v2_module \
      --with-http_realip_module
    
    # 编译并安装
    make -j$(nproc)
    sudo make install
    
    # 创建systemd服务文件
    sudo tee /etc/systemd/system/nginx.service > /dev/null << 'EOF'
    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network-online.target
    [Service]
    Type=forking
    PIDFile=/var/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    [Install]
    WantedBy=multi-user.target
    EOF
    
    sudo systemctl daemon-reload
    sudo systemctl enable nginx --now

    1.4 验证Nginx运行状态

    # 检查服务状态
    sudo systemctl status nginx
    
    # 检查端口监听
    sudo ss -tlnp | grep :80
    
    # 测试页面访问
    curl -I http://localhost

    二、反向代理基础配置

    反向代理是Nginx最核心的功能。通过反向代理,可以将外部请求转发到内网的Web应用,同时实现负载均衡、SSL终结、静态资源缓存等功能。

    2.1 最基本的反向代理配置

    # /etc/nginx/conf.d/reverse-proxy.conf
    
    server {
        listen 80;
        server_name example.toukenai.cn;
    
        # 将请求转发到内网后端服务
        location / {
            proxy_pass http://127.0.0.1:8080;
            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;
        }
    
        # 静态文件由Nginx直接处理,提升性能
        location /static/ {
            proxy_pass http://127.0.0.1:8080;
            expires 7d;
            add_header Cache-Control "public, immutable";
        }
    }
    提示:proxy_set_header系列指令非常重要。缺少这些头信息,后端应用将无法获取真实客户端IP,也无法知道请求来源协议(http还是https)。

    2.2 负载均衡配置

    当后端有多个服务实例时,可以使用Nginx的负载均衡功能:

    # upstream块定义后端服务器池
    upstream backend {
        least_conn;  # 最少连接数算法
        server 127.0.0.1:8080 weight=3;
        server 127.0.0.1:8081 weight=2;
        server 127.0.0.1:8082 weight=1;
        keepalive 32;  # 保持连接数
    }
    
    server {
        listen 80;
        server_name example.toukenai.cn;
    
        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }
    }

    2.3 HTTPS反向代理配置

    在信创环境下部署HTTPS,推荐使用Let's Encrypt免费证书,配合certbot自动续期:

    server {
        listen 443 ssl http2;
        server_name example.toukenai.cn;
    
        # SSL证书配置
        ssl_certificate /etc/letsencrypt/live/example.toukenai.cn/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.toukenai.cn/privkey.pem;
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
    
        # 现代TLS配置
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
        ssl_prefer_server_ciphers off;
        add_header Strict-Transport-Security "max-age=63072000" always;
    
        location / {
            proxy_pass http://127.0.0.1:8080;
            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;
        }
    }
    
    # HTTP强制跳转HTTPS
    server {
        listen 80;
        server_name example.toukenai.cn;
        return 301 https://$host$request_uri;
    }

    三、安全防护配置

    在信创环境下对外提供服务,安全防护是必须考虑的问题。以下是生产环境中常用的Nginx安全配置。

    3.1 防止常见Web攻击

    # 安全响应头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    
    # 禁止访问敏感文件
    location ~ /\. {
        deny all;
    }
    
    location ~ /\.git {
        deny all;
    }
    
    location ~ /\.env {
        deny all;
    }
    
    # 限制请求方法(只允许GET/POST/HEAD)
    if ($request_method !~ ^(GET|POST|HEAD)$) {
        return 405;
    }

    3.2 限流配置防止DDoS

    # 限流配置(基于IP限制请求频率)
    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
    
    server {
        location / {
            limit_req zone=req_limit burst=20 nodelay;
            limit_req_status 503;
        }
    }
    
    # 基于IP的白名单(内网IP不限流)
    geo $limit {
        default 1;
        10.0.0.0/8 0;
        172.16.0.0/12 0;
        192.168.0.0/16 0
    map $limit $limit_key {
        1 $binary_remote_addr;
        0 "";
    }
    
    limit_req_zone $limit_key zone=req_limit:10m rate=10r/s;

    3.3 WAF基础规则(ModSecurity联动)

    对于高安全要求的信创环境,建议在Nginx前端串联ModSecurity作为Web应用防火墙:

    # Nginx + ModSecurity反向代理配置
    server {
        listen 80;
        server_name example.toukenai.cn;
    
        ModSecurityEnabled on;
        ModSecurityConfig /etc/modsecurity/modsecurity.conf;
    
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    注意:ModSecurity在国产操作系统上的编译和依赖配置较为复杂,建议使用预编译包或Docker方式部署,以避免依赖兼容问题。

    四、生产环境调优

    4.1 Worker进程与连接数配置

    # /etc/nginx/nginx.conf
    
    worker_processes auto;  # 自动匹配CPU核心数
    worker_rlimit_nofile 65535;  # 打开文件描述符上限
    
    events {
        worker_connections 4096;  # 单worker最大连接数
        use epoll;  # Linux下使用epoll多路复用
        multi_accept on;
    }

    4.2 缓冲区与超时配置

    proxy_buffering on;
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    
    # 超时配置
    proxy_connect_timeout 60s;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;

    4.3 日志与监控

    # 访问日志配置
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    'rt=$request_time uct="$http_x_real_ip"';
    
    access_log /var/log/nginx/access.log main;
    
    # 错误日志(生产环境建议info级别以上)
    error_log /var/log/nginx/error.log warn;

    五、效果验证

    完成配置后,按以下步骤验证:

    # 1. 检查配置语法
    sudo nginx -t
    
    # 2. 重载配置(不中断服务)
    sudo nginx -s reload
    
    # 3. 测试反向代理是否生效
    curl -I http://example.toukenai.cn
    
    # 4. 查看实时日志
    sudo tail -f /var/log/nginx/access.log
    
    # 5. 使用ab压测工具进行简单压力测试
    ab -n 1000 -c 100 http://example.toukenai.cn/
    提示:使用ab压测时注意目标URL的真实地址,避免压测到生产环境。压测结果中的Requests per second反映的是Nginx反向代理的处理能力。

    总结

    本文详细讲解了信创环境(银河麒麟Kylin、统信UOS)下Nginx的安装、反向代理配置、HTTPS设置以及安全防护方案。核心要点:

    在信创服务器上正确配置Nginx反向代理,既能提升Web服务的响应速度,又能保护内网后端应用,是政府央企信息化改造中不可或缺的一环。

    六、信创环境特殊注意事项

    6.1 国产CPU兼容性问题

    在鲲鹏、飞腾等国产CPU服务器上运行Nginx时,需要注意以下几点:

    6.2 国密算法支持(可选)

    政务项目通常需要支持国密SM2/SM3/SM4算法,需要在编译时启用GmSSL:

    # 安装国密库
    sudo apt install gmssl -y
    
    # 编译Nginx时添加国密模块
    ./configure --with-http_ssl_module --with-openssl=/path/to/gmssl ...
    
    # 配置中使用国密证书
    ssl_certificate /etc/nginx/sm2/certificate.sm2;
    ssl_certificate_key /etc/nginx/sm2/private.sm2;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers Sm2Sm3Sm4:high;

    6.3 信创环境防火墙配置

    # 麒麟Kylin / 统信UOS 防火墙
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
    
    # 或者使用iptables(传统方式)
    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    sudo iptables-save > /etc/iptables/rules.v4

    6.4 常见问题排查

    问题现象可能原因解决方法
    Nginx启动失败端口被占用或权限不足检查ss -tlnp | grep :80,修改/etc/nginx权限
    反向代理返回502后端服务未启动或地址错误确认127.0.0.1:8080服务运行,检查proxy_pass地址
    静态文件访问慢未启用缓存或Gzip添加expires指令,启用gzip压缩
    HTTPS不安全警告证书配置错误或过期使用certbot续期,检查证书路径
    鲲鹏CPU上编译失败编译器版本不兼容使用gcc 7.x以上版本,添加-mcpu=native

    信创环境下的Nginx运维与标准Linux环境整体差异不大,重点关注CPU架构兼容性和国密算法支持即可。