enabling cors on a nginx server in ElasticBeanstalk?

后端 未结 1 503
走了就别回头了
走了就别回头了 2021-01-03 01:34

Im deploying my Nodejs app to AWS Elastic Beanstalk running nginx.

The app is essentially an api, which i can make calls to and retrieve back JSON data i.e. myapi.aw

相关标签:
1条回答
  • 2021-01-03 01:50

    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.

    Below is the configuration that worked for me, also running node.js on Beanstalk, and a cloudfront-hosted client application calling the 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()
    })
    
    0 讨论(0)
提交回复
热议问题