Nginx Access-Control-Allow-Origin 不生效

这是我的nginx配置文件。nginx部署的server cname是test.com,然后我配置的是如果http访问会跳转到https端口,因为用了load balancer,443的请求会被转发到test.com的90端口。将代码部署到test.com后,本地开发环境会报CORS错误,不知道是哪里配置错误,有人可以帮忙看看吗,谢谢。

Access to XMLHttpRequest at 'http://test.com/api/test/test' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    rewrite ^/(.+)$ https://test.com/$1 permanent;
}

server {
    listen       90;
    listen  [::]:90;
    server_name  localhost;

    access_log  /var/log/nginx/host.access.log  main;

    error_page   500 502 503 504  /50x.html;

    location / {
       rewrite / /angular;
      }

    location  /angular {
      add_header    Cache-Control no-store;
      root /etc/nginx/html;
      try_files $uri $uri/ /angular/index.html;
    }

    location ^~ /api/ {
        proxy_pass  http://proxy.com:1338;
        proxy_set_header Host                   $host;
        proxy_set_header X-Real-IP              $remote_addr;
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        rewrite ^/api/(.*) /$1 break;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
阅读 4.2k
1 个回答
新手上路,请多包涵

你把nginx配置里面的server_name都改成test.com

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏