在Nginx部署Web应用,如何保障后端API的安全

1. 使用HTTPS和http2.0

参考:Nginx配置HTTP2.0_nginx 支持 2.0-CSDN博客

2. 设置严格的CORS策略

通过add_header指令设置CORS头。

只允许来自https://frontend.yourdomain.com的请求访问API

复制代码
location /api/ {
    if ($http_origin ~* (https://frontend\.yourdomain\.com)) {
        add_header 'Access-Control-Allow-Origin' $http_origin;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    }

    if ($request_method = 'OPTIONS') {
        return 204;
    }

    proxy_pass http://backend;
}

3. 实现身份验证

对于JWT认证,后端服务负责生成和验证令牌,Nginx仅需检查令牌的存在性。为了实现这一点,可以在Nginx中添加一个自定义的Lua脚本(需要安装ngx_http_lua_module)或者直接在Nginx配置中进行简单的检查:

复制代码
location /api/ {
    set $auth 0;
    if ($http_authorization ~ "^Bearer (.+)$") {
        set $auth 1;
    }
    if ($auth = 0) {
        return 401 "Missing or invalid Authorization header";
    }

    proxy_pass http://backend;
}

4. 限制IP地址

复制代码
location /api/secure {
    allow 192.168.1.0/24;  # 允许的子网
    deny all;              # 拒绝其他所有
    proxy_pass http://backend;
}

5. 使用限流

防止滥用或DDoS攻击,使用limit_req模块来限制请求速率

复制代码
http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

    server {
        location /api/ {
            limit_req zone=one burst=5 nodelay;
            proxy_pass http://backend;
        }
    }
}

6. 实施WAF(Web应用防火墙)

安装ModSecurity并在Nginx中启用它:

复制代码
# 安装ModSecurity
sudo apt-get install libmodsecurity3 modsecurity-crs

# 启用ModSecurity
sudo nano /etc/nginx/modsec/modsecurity.conf-recommended

编辑/etc/nginx/sites-available/yourdomain,添加以下行以加载ModSecurity:

复制代码
load_module modules/ngx_http_modsecurity_module.so;

server {
    ...
    modsecurity on;
    modsecurity_rules_file /etc/nginx/modsec/main.conf;
    ...
}

创建或编辑规则文件/etc/nginx/modsec/main.conf以包含OWASP Core Rule Set或其他自定义规则。

7. 日志记录和监控

确保启用了适当的日志级别,并定期审查日志文件。也可以集成第三方监控工具如ELK Stack、Prometheus等。

复制代码
http {
    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 /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;
}

8. 更新和维护

保持系统和软件包的更新是至关重要的。使用自动化的包管理器如APT(Debian/Ubuntu)或YUM(CentOS/RHEL)来定期更新

复制代码
# 对于Debian/Ubuntu
sudo apt-get update && sudo apt-get upgrade -y

# 对于CentOS/RHEL
sudo yum update -y

同时,可以订阅安全公告并及时应用补丁,考虑使用自动化工具如Ansible、Puppet或Chef来进行系统管理和配置部署。

相关推荐
可观测性用观测云7 小时前
云原生网关 Ingress-Nginx 链路追踪实战:OpenTelemetry 采集与观测云集成方案
nginx·kubernetes
用户962377954481 天前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机1 天前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机1 天前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户962377954481 天前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star1 天前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户962377954482 天前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
闲云一鹤2 天前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
cipher3 天前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全