简体   繁体   English

在ElasticBeanstalk中的nginx服务器上启用CORS?

[英]enabling cors on a nginx server in ElasticBeanstalk?

Im deploying my Nodejs app to AWS Elastic Beanstalk running nginx. 我将Nodejs应用程序部署到运行nginx的AWS Elastic Beanstalk。

The app is essentially an api, which i can make calls to and retrieve back JSON data ie myapi.awselasticbeanstalk.com/api/get_stuff etc. 该应用程序本质上是一个api,我可以对其进行调用并检索回JSON数据,即myapi.awselasticbeanstalk.com/api/get_stuff等。

Im trying to enable CORS so i can access the server from my javascript application (the client). 我试图启用CORS,以便我可以从我的javascript应用程序(客户端)访问服务器。

As per amazon documentation I can edit or extend the nginx configuration adding a config file to the .ebextensions folder. 根据亚马逊文档,我可以编辑或扩展nginx配置,将配置文件添加到.ebextensions文件夹。

cors.config cors.config

files:
  /etc/nginx/conf.d/cors.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
        location / {
             if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                #
                # Custom headers and headers various browsers *should* be OK with but aren't
                #
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
                #
                # Tell client that this pre-flight info is valid for 20 days
                #
                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 = 'POST') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
                add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
             }
             if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
                add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
             }
        }

but this is still not working for me. 但这仍然不适合我。

If you set CORS headers in the response that goes out of your node/express application, you don't need to add anything to Nginx configuration. 如果您在节点/ express应用程序发出的响应中设置CORS标头,则无需向Nginx配置添加任何内容。

Below is the configuration that worked for me, also running node.js on Beanstalk, and a cloudfront-hosted client application calling the API. 以下是对我有用的配置,该配置也在Beanstalk上运行node.js,以及在Cloudfront托管的客户端应用程序调用API。

Modify as needed: 根据需要修改:

server.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Credentials', true)
  res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
  next()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM