插件窝 干货文章 Nginx反向代理跨域基本配置方法

Nginx反向代理跨域基本配置方法

header Control Access Origin 529    来源:    2025-04-26

Nginx反向代理跨域配置方法

跨域问题(CORS)是前端开发中常见的问题,使用Nginx反向代理可以有效解决跨域问题。以下是Nginx配置跨域的基本方法:

基本跨域配置

server {
    listen 80;
    server_name your.domain.com;

    location /api/ {
        proxy_pass http://backend-server/;  # 后端服务地址

        # 核心跨域配置
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';

        # 处理OPTIONS预检请求
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

配置说明

  1. Access-Control-Allow-Origin:

    • * 表示允许所有域名访问
    • 也可以指定特定域名:add_header 'Access-Control-Allow-Origin' 'https://your-frontend.com';
  2. Access-Control-Allow-Methods:

    • 定义允许的HTTP方法
  3. Access-Control-Allow-Headers:

    • 定义允许的请求头
  4. OPTIONS预检请求处理:

    • 浏览器在发送复杂请求前会先发送OPTIONS请求
    • 需要单独处理并返回204状态码

安全增强配置

# 限制允许的域名
map $http_origin $cors_origin {
    default "";
    "~^https://(example\.com|sub\.example\.com)$" $http_origin;
}

server {
    # ...其他配置...

    location /api/ {
        # 使用变量控制允许的源
        add_header 'Access-Control-Allow-Origin' $cors_origin;
        add_header 'Access-Control-Allow-Credentials' 'true';

        # ...其他配置...
    }
}

常见问题解决

  1. 多个域名需要跨域访问:

    • 使用$http_origin变量动态设置允许的源
    • 或者在后端服务中处理跨域头
  2. 携带Cookie跨域:

    • 需要设置Access-Control-Allow-Credentials: true
    • 此时Access-Control-Allow-Origin不能为*,必须指定具体域名
  3. Nginx配置不生效:

    • 检查是否有其他配置覆盖了add_header指令
    • Nginx的add_header会继承父作用域,但如果子作用域有add_header,父作用域的会失效

完整示例

http {
    # 定义允许的跨域域名
    map $http_origin $cors_origin {
        default "";
        "~^https://(www\.example\.com|api\.example\.com)$" $http_origin;
    }

    server {
        listen 80;
        server_name api.example.com;

        location / {
            # 反向代理到实际后端
            proxy_pass http://localhost:8080;

            # 跨域配置
            if ($cors_origin) {
                add_header 'Access-Control-Allow-Origin' $cors_origin;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With';
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Max-Age' 86400;
            }

            # 处理OPTIONS请求
            if ($request_method = 'OPTIONS') {
                return 204;
            }

            # 其他代理设置
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

通过以上配置,Nginx可以有效地处理跨域请求,同时保持一定的安全性。根据实际需求调整允许的域名、方法和头信息。