从同一服务器上的应用程序访问我的api时出现CORS错误

时间:2019-12-30 01:24:45

标签: angular cors asp.net-core-webapi

我有一个带有nginx的Amazon EC2 Linux服务器。

它具有.NET Core API以及我的Angular应用。

API配置(来自MS文档)

server {
    listen 80;
    listen [::]:80;
    server_name api.ZZZ.com;
    location / {
            proxy_pass              http://localhost:5000;
            proxy_http_version      1.1;
            proxy_set_header        Upgrade $http_upgrade;
            proxy_set_header        Connection keep-alive;
            proxy_set_header        Host $host;
            proxy_cache_bypass      $http_upgrade;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
    }
}

我的网络应用程序配置:

server {
    listen 80;
    listen [::]:80;
    server_name ZZZ.com www.ZZZ.com;
    root /var/www/ZZZ;
    server_tokens off;
    index index.html;

    location / {
            try_files $uri $uri/ /index.html =404;
    }
    location ~* \.(png|jpg)$ {
            expires max;
            add_header Pragma Public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
}

在我的API项目的launchSettings.json中,有以下代码段:

"iisExpress": {
  "applicationUrl": "http://localhost:55941"
}

但是,根据MS文档,如果存在http://localhost:5001,我必须将其删除。显然我的没有,所以我把上面的那行留在那里。

因此,当我的网站在线时,出现以下CORS错误:

Access to XMLHttpRequest at 'http://api.ZZZ.com/YYY' from origin 'http://www.ZZZ.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

但是它们不是不同的域!我所知道的。请帮忙!

0 个答案:

没有答案