通过nginx解决跨域问题

nginx版本 1.29.1

问题

10.20.4.1 访问这个10.20.4.2:5000服务跨域了,麻烦处理一下

跨域报错

根据您图片中的错误信息,前端运行在 http://10.20.4.1,试图向 http://10.20.4.2:50000/upload发送 POST 请求,但被 CORS 策略阻止,因为响应中没有 Access-Control-Allow-Origin头部。

应该5000是python端口

我采用了nginx代理,添加允许跨域

bash 复制代码
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/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 /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;

    # 动态设置允许的CORS源
    # 1. 检查 map 块:确保包含 http://10.20.4.1
    map $http_origin $cors_allowed_origin {
        default "";
        "http://10.20.4.1" "http://10.20.4.1"; # 必须存在这一行
        "http://10.20.4.2" "http://10.20.4.2"; # 必须存在这一行
        # ... 其他允许的来源
    }

    server {
        listen 50000;
        server_name 10.20.4.2;

        # 2. 检查 server 或 location 块:确保添加了 CORS 头
        location / {
            # 处理 OPTIONS 预检请求
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' $cors_allowed_origin;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Length' 0;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                return 204;
            }

            # 反向代理到后端
            proxy_pass http://10.20.4.2:5000;
            # ... 其他 proxy_set_header ...

            # 为实际响应添加 CORS 头,使用 always 确保任何状态码都添加
            add_header 'Access-Control-Allow-Origin' $cors_allowed_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' '*' always;
        }
    }
}

测试实际 POST 请求

此命令模拟前端实际发送的 POST 请求,检查响应是否包含 CORS 头部。

bash 复制代码
curl -X OPTIONS "http://10.20.4.2:50000" \
  -H "Origin: http://10.20.4.1" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type" \
  -v
相关推荐
骇客野人6 小时前
Redis 完整安装部署方案(Linux,分【源码编译】+【Docker】两种,推荐生产用源码,测试快速用 Docker)
运维
蓝速科技9 小时前
会议室门牌公告通知发布选型与落地指南丨蓝速科技
大数据·运维·数据库·人工智能·科技
chicheese10 小时前
Linux 面试速查表:从初级到高级,附高频场景答题模板
linux·运维
Linux-lucky10 小时前
32-38-Linux学习之旅之MySQL综合
linux·运维·学习·mysql·ubuntu
智能运维指南11 小时前
2026年ITSM系统怎么选?四款主流方案与五维评估模型
运维·itsm·嘉为蓝鲸
我命由我1234511 小时前
Git 推送报错:error: src refspec main does not match any
运维·git·gitee·github·运维开发·学习方法·版本控制
骇客野人11 小时前
测试环境MySQL迁移Vastbase(海量数据库)完整改造与落地实施方案
运维·服务器
迪康Defender11 小时前
终端安全实战:如何高效解决企业U盘泄密与管控难题
运维·网络·安全·web安全·终端安全管理
zly350012 小时前
VMware Converter Standalone 物理机转化为虚拟机后源1硬盘变成了2个硬盘(2个硬盘文件)虚拟机无法启动。
linux·运维·服务器
智能运维指南12 小时前
2026 企业智能运维平台选型:私有化、信创、全链路打通该怎么权衡?
运维·嘉为蓝鲸·aiops平台·一体化运维平台