【问题标题】:rest api direct vs nginx proxyrest api 直接与 nginx 代理
【发布时间】:2019-07-24 04:40:16
【问题描述】:

我是 nginx 和 CORS 的新手,我发现要做到这一点很有挑战性。

我在阻止 CORS 的服务器上托管了休息服务,因此安装了 nginx 来代理休息呼叫。什么有效:

  1. 启用 CORS 后,其余 api 调用(从角度代码)到后端服务器
  2. rest api 调用(来自 chrome)到启用 cors 的前端 nginx 服务器

什么不起作用:rest api 调用(从角度代码)到前端 nginx

我认为 CORS 部分有效,因为我不再看到该错误,但 angular 得到了空响应。

对于上述情况,我尝试过使用 GET 和 POST 方法。即使是失败的场景,响应码也是 200 OK。

这里是 nginx 配置文件:

upstream myserver {
    server      myserver.com:8443;
}

server {
    listen       443 ssl;
    listen       [::]:443 ssl;
    server_name  myserver.com;
    ssl_certificate     /some.crt;
    ssl_certificate_key /some.key;

    location /rest-service/ {
        # Simple requests
        if ($request_method ~* "(GET|POST)") {
            add_header "Access-Control-Allow-Origin"  *;
        }

    # Preflighted requests
    if ($request_method = OPTIONS ) {
      add_header "Access-Control-Allow-Origin"  *;
      add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
      add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
      return 200;
    }

        proxy_pass_header       Server;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Scheme $scheme;
        proxy_set_header        X-NginX-Proxy true;
        proxy_connect_timeout   5;
        proxy_read_timeout      240;
        proxy_intercept_errors  on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass                      https://myserver/rest-service/;
        proxy_ssl_trusted_certificate   /some.pem;
        proxy_ssl_verify                off;
        proxy_ssl_session_reuse         on;

    }
}

这是 angular/typescript 代码(从 loaclhost 运行):

  ngOnInit() {
    let url='https://myserver.com/rest-service/login?login=admin&password=password';
    this.http.get(this.url).subscribe((response) => {console.log(response); });
  }

【问题讨论】:

  • 你用过哪种浏览器?
  • 您需要使用stackoverflow.com/posts/54956629/edit 编辑/更新问题并添加有关请求和响应的完整详细信息:您的前端代码向请求添加了哪些标头?您是否收到针对 OPTIONS 请求的空响应的请求?响应的 HTTP 状态码是什么?浏览器在 devtools 控制台中记录的确切错误消息是什么?
  • 除了来自@sideshowbarker 的评论 - 请包括您撰写请求的 javascript。
  • 感谢您的回复,我已经添加了所有这些详细信息。

标签: nginx cors


【解决方案1】:

我想我发现了问题并将其发布在这里;希望它可以帮助某人。

以下工作:

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' 'http://localhost:4200' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,Cache-Control,Content-Type,Accept' always;
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    if ( $request_method = 'GET' ) {
        add_header 'Access-Control-Allow-Origin' 'http://localhost:4200' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,Cache-Control,Content-Type,Accept' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }
    if ( $request_method = 'POST' ) {
        add_header 'Access-Control-Allow-Origin' 'http://localhost:4200' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Origin,Keep-Alive,User-Agent,Cache-Control,Content-Type,Accept' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多