-1

I can't Enable CORS on my API Gateway instance, this is how it looks: Everything is installed on an nginx server under ubuntu 20.04.

  1. React Font-end: https://example.com -nginx
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    root /home/ubuntu/front;
    index index.html index.htm;

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }

    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri $uri/ /index.html;
    }

    return 301 https://example.com$request_uri;

}


server {

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; # managed by Certbot


    server_name example.com www.example.com;

    root /home/ubuntu/front;
    index index.html index.htm;

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }

    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri $uri/ /index.html;
    }

}

  1. express Back-end: https://api.mydomain.com
    • code add to js
app.use(cors());

´ - nginx

upstream api {
        server xx.xx.xx.xx;
}


server {
    server_name api.mydomain.com;
    location / {

        proxy_pass http://127.0.0.1:4001;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;


        proxy_connect_timeout 30;
        proxy_send_timeout 30;


    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/xx.xxxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/livexx.xxxx.com/privkey.pem; # managed by Certbot

}

  1. minio objectstorage: https://minio.example.com
server {
 listen 443 ssl;
 server_name minio.example.com;

 ssl_certificate           /etc/minio/certs/public.crt;
 ssl_certificate_key       /etc/minio/certs/private.key;

 # To allow special characters in headers
 ignore_invalid_headers off;
 # Allow any size file to be uploaded.
 # Set to a value such as 1000m; to restrict file size to a specific value
 client_max_body_size 1000m;
 # To disable buffering
 proxy_buffering off;

 location / {
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header Host $http_host;

   proxy_connect_timeout 300;
   # Default is HTTP/1, keepalive is only enabled in HTTP/1.1
   proxy_http_version 1.1;
   proxy_set_header Connection "";
   chunked_transfer_encoding off;

   proxy_pass https://localhost:9000; 

   # Ajouter les headers de contrôle d'accès CORS
   #add_header 'Access-Control-Allow-Origin' '*' always;
   #add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
   #add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;
   #add_header 'Access-Control-Allow-Credentials' 'true' always;


 }

i can manage to make http requests from example.com to api.example.com without having cors errors but when i do an http request fomr example.com to api.example.com then from api.example.com to mini.example.com(or any api), i have cors error " Access to xmlhttprequest at https://api.example.com/upload from https://example.com has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested ressource

  • Have you tried to log the headers before responding to the request `console.log(res.getHeaders())` to make sure the * 'access-control-allow-origin'* header is present. Also do you have this line `app.use(cors());` in your code above the route handler? – Molda Jul 30 '20 at 07:58
  • res.getHeader('Access-Control-Allow-Origin') = '*' – fidel fide Jul 30 '20 at 08:39

1 Answers1

-1
proxy_set_header Access-Control-Allow-Origin "*";

sovled the issue