Nginx는 Nodejs(Dokku)를 프록시합니다. CORS 응답 헤더가 통과하지 않음

Nginx는 Nodejs(Dokku)를 프록시합니다. CORS 응답 헤더가 통과하지 않음

나는 Dokku를 사용하여 DigitalOcean에서 내 앱을 호스팅하고 있습니다. 독쿠Heroku와 유사한 환경을 시뮬레이션하는 Docker 앱을 프록시하려면 nginx 1.6을 실행하세요. 앱은 모두 아래와 같이 유사한 기본 구성을 공유합니다.

내 Node.js 서버는CORS 미들웨어www.myapp.com이 api.myapp.com을 호출할 수 있도록 브라우저에 지시하려면 다음을 수행하세요.

이것은 내 로컬 컴퓨터에서 잘 작동합니다. 배포할 때 브라우저에 CORS 오류가 표시됩니다.

XMLHttpRequest cannot load https://api.myapp.com/r_u_up. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.myapp.com' is therefore not allowed access. The response had HTTP status code 502.

그럼 뭐, 끝났어.

나는 이것을 찾았다nginx CORS 구성하지만 그것은 매우 지저분해 보인다. 이 오래된 코드인가요, 아니면 최선의 방법인가요? 이것플러그인은 해당 구성을 사용합니다.

나는 응답 헤더를 통과시키는 간단한 구성을 선호합니다. 내 앱은 이를 가로채기 위해 nginx가 필요하지 않습니다. 어떻게 구성할 수 있나요?

앱 nginx.conf:

upstream www { server 172.17.0.135:5000; }
server {
  listen      [::]:80;
  listen      80;
  server_name www.myapp.com ;
  return 301 https://www.myapp.com$request_uri;
}

server {
  listen      [::]:443 ssl spdy;
  listen      443 ssl spdy;
  server_name www.myapp.com;


  keepalive_timeout   70;
  add_header          Alternate-Protocol  443:npn-spdy/2;
  location    / {
    proxy_pass  http://www;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
  }
  include /home/dokku/www/nginx.conf.d/*.conf;
}

답변1

업데이트: 따라서 CORS는 좀비화-워킹-데드-미친 사양이라는 것이 밝혀졌습니다. 예, nginx 구성으로 이 작업을 수행하는 것이 가장 좋은 방법입니다.

http://enable-cors.org/

nginx가 가장 좋은 방법인 이유는 nginx가 클라이언트에 가장 빠르고 가장 가까운 프로세스이기 때문입니다.

nginx가 앱(node.js, php, Rails 등)을 건드리지 않고도 요청을 처리할 수 있다면 앱이 더 쉽게 확장되고 더 빠르게 실행될 것입니다.

관련 정보