CORS работал, но после настройки Nginx на использование HTTPS я получаю ошибку CORS

1
7

Вот моя конфигурация Nginx:

У меня есть 2 экземпляра AWS EC2:

Они используют разные сертификаты SSL. У меня установлена ​​безопасность на api.MYDOMAIN, чтобы разрешить входящему правилу HTTPS поступать с IP-адреса домена выше. Ранее я исправил CORS. После обновления api.MYDOMAIN до HTTPS теперь CORS не работает, и я не могу понять, почему.

Я настроил следующий журнал:

map $http_origin $cors_origin {

   ~^https?://(www.)?MYDOMAIN$ $http_origin;
   default "";
}
map "$cors_origin" $cors_cred {
   ~^$ false;
   default true;
}

map "$request_method" $cors_method {
   ~^OPTIONS$ true;
   ~^GET$ true;
   ~^DELETE$ true;
   ~^PATCH$ true;
   ~^POST$ true;
   ~^PUT$ true;
   default false;
}

# echo into log to test variables
log_format upstream_time "$time_local http_origin=$http_origin remote_addr=$remote_addr allow_origin=$cors_origin cred=$cors_cred cors_method=$cors_method request_method=$request_method";

server {
  listen  80;
  server_name *.MYDOMAIN;
  return 301 https://$host$request_uri;
}
server {
  listen  443 ssl;

  ssl_certificate /etc/letsencrypt/live/api.MYDOMAIN/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/api.MYDOMAIN/privkey.pem;
  ssl_protocols       TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  ssl_ciphers         HIGH:!aNULL:!MD5;

  server_name api.MYDOMAIN;

  location / {
    access_log /var/log/nginx/access.log upstream_time;

    if ($cors_origin) {
      add_header 'Access-Control-Allow-Credentials' $cors_cred always;
      add_header 'Access-Control-Allow-Origin' $cors_origin always;
      add_header 'Access-Control-Allow-Methods' 'GET, DELETE, PATCH, POST, PUT, OPTIONS' always;
      add_header 'Access-Control-Allow-Headers' 'Accept, Authorization, Keep-Alive, Origin, DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    if ($request_method = 'OPTIONS') {
      # 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;
    }
# test
# root /var/www/html; index index.html;
    proxy_pass http://localhost:8081;
    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;
  } # End location
} # End server
19/Aug/2024:20:45:11 +0000 http_origin=https://www.MYDOMAIN remote_addr=MYIP allow_origin=https://www.MYDOMAIN cred=true cors_method=true request_method=OPTIONS
Петр
Вопрос задан16 января 2024 г.

1 Ответ

2
Доброслав
Ответ получен2 сентября 2024 г.

Ваш ответ

Загрузить файл.