Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...