简体   繁体   中英

Access-Control-Allow-Origin cors error happening on NodeJS or Nginx

First at all, I have researched about the topic after writing the question, there are many similar question but I think the problem here is different:

No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

We are getting this Access-Control-Allow-Origin cors error just when we send a heavy request :

Access to XMLHttpRequest at ' https://backend .*****.es/xxxxxx' from origin ' https://www.testing .*******.es' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

The rest of the request are working fine, I tried setting:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  next();
});

but I got the same error .

I am starting to think it can be related with nginx .

This is the architecture we are using:

  • NodeJs, Expressjs
  • Middlewares:

    const LIMIT = '100mb'; const global = () => [ morganMiddleware(), compression(), cors(), bodyParser.urlencoded({ extended: true, limit: LIMIT }), bodyParser.json({ limit: LIMIT }), helmet(), ];

  • Server: Nginx 12.14.1

  • Host in AWS Elastic BeanStalk

Let me know if anyone have any clue what can be happening because I do not know if it is coming from our nodejs server, or nginx. We have tested many solutions and are still checking out other options.

We are getting this error in our sever:

2020/02/20 10:56:00 [error] 2784#0: *127 client intended to send too large body: 4487648 bytes, client: 172.31.3.222, server: , request: "PUT /cars/drafts/f7124841-f72c-4133-b49e-d9f709b4cf4d HTTP/1.1", host: "backend.xxxxx.es", referrer: " https://www.testing.xxxx.es/sellcar/photos/f7124841-f72c-4133-b49e-d9f709b4cf4d "

So besides the error that appears in the front is Access-Control-Allow-Origin it is related with the a limit set in NGINX.

Now we just need to figure out how to access to our elastic beanstalk instance to change that variables and that is already solved.

SSH to Elastic Beanstalk instance

To solve it, create a file x.config inside .ebextensions and change this variables with the next yaml file:

files:
  "/etc/nginx/conf.d/01-client_max_body_size.conf":
    mode: 000644
    owner: root
    group: root
    content: |
      # MODIFIED VARIABLES ADDED BY 01-client_max_body_size.conf
      client_max_body_size 12m;
      client_body_buffer_size 16k;
container_commands:
  nginx_reload:
    command: sudo service nginx reload

Then redeploy you environment.

Thank you very much all for your help!

Located the type of Amazon Linux 2 (AL2) on your Elastic Beanstalk Dashboard:

Elastic Beanstalk 平台类型

Create the following directory structure from the root directory of your App:

.platform/nginx/conf.d/

Create the following file:

.platform/nginx/conf.d/01_custom_ngix.conf

Add client_max_body_size and change the value on the following inside your 01_custom_ngix.conf file you created (the example here is 1000M = 1GB) and make sure your AWS Instance Types is large enough t2.medium:

client_max_body_size 1000M;
client_body_buffer_size 100M;
...    
...    
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

Git Commit your changes.

Deploy your code:

> eb deploy 

options: eb deploy --verbose --debug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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