php - NGINX 'Access-Control-Allow-Origin' header 包含多个值

标签 php ajax nginx

我有一个带有 PHP 的 NGINX 服务器(假设主机名是 http://myserver.com )。我有一个 PHP 脚本,我正在通过 XHR 从本地主机上的网页访问它。我将它用作类似于 freegeoip.net 的 GeoIP 服务器。

我试图将 XHR 锁定到特定域。

这是我的配置设置:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

    fastcgi_param GEOIP_COUNTRY_CODE $geoip2_data_country_code;
    fastcgi_param GEOIP_COUNTRY_NAME $geoip2_data_country_name;
    fastcgi_param GEOIP_COUNTRY_GEONAME_ID $geoip2_data_country_geoname_id;
    fastcgi_param GEOIP_CITY_NAME $geoip2_data_city_name;
    fastcgi_param GEOIP_CITY_GEONAME_ID $geoip2_data_city_geoname_id;
    fastcgi_param GEOIP_CONTINENT_CODE $geoip2_data_city_continent_code;
    fastcgi_param GEOIP_CONTINENT_GEONAME_ID $geoip2_data_city_continent_geoname_id;
    fastcgi_param GEOIP_LATITUDE $geoip2_data_city_location_latitude;
    fastcgi_param GEOIP_LONGITUDE $geoip2_data_city_location_longitude;
    fastcgi_param GEOIP_TIME_ZONE $geoip2_data_city_location_timezone;
    fastcgi_param GEOIP_ISP $geoip2_data_city_traits_isp;
    fastcgi_param GEOIP_IP_ADDRESS $geoip2_data_city_traits_ip_address;

    set $cors "";

    if ($http_origin ~* 'https?://(www\.domain1\.com|www\.domain2\.com)')
    {
        set $cors "true";
    }

    if ($cors = 'true')
    {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,Pragma,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
    }

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

我遇到的问题是,当我执行 XHR 请求时,出现以下错误:

XMLHttpRequest 无法加载 http://myserver.com/。 'Access-Control-Allow-Origin' header 包含多个值 '*, http://localhost',但只允许一个。因此不允许访问源“http://localhost”。

我在配置文件中只有一次调用 add_header 'Access-Control-Allow-Origin' "$http_origin";,那么为什么我有多个值?有没有办法可以禁用第一个调用,即 *

最佳答案

1.) 让应用程序动态批准并添加响应 header 。

$allowed_domains = ['http://allowed.com','http://another_allowed.com'];

function add_cors_header() {
    if (in_array($_SERVER['http_origin'], $allowed_domains)) {
        header('Access-Control-Allow-Origin', $_SERVER['http_origin']);
    }
}

2.) 或者安装启用 Lua 的 Nginx 的 OpenResty 版本并执行相同的操作,但在 Nginx conf 文件中使用 Lua。

关于php - NGINX 'Access-Control-Allow-Origin' header 包含多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38963180/

相关文章:

php - 将时间戳与数据库中的当前时间进行比较

javascript - 我在所有工具提示中得到与我的第一个 id 相同的文本,即使它不属于该 id

javascript - 使用 AJAX jQuery 进行投票

c# - 使用 AJAX 调用的 JavaScript 对话框不同步

php - 为什么我的 post 方法在 Laravel 中不起作用?

php - INSERT INTO 查询中的 SQL 错误

javascript - 如何在 reactjs 的 componentDidUpdate 中打破无限循环

docker - 通过 nginx websocket 代理连接时 WebApp 崩溃

python - 用于激活 nginx 配置/网站的小服务

api - 缓存第三方API调用的最佳做法是什么?