EyesPain
@EyesPain

Как настроить CORS в nginx-ingress-controller?

Всем, привет! Нужна помощь с nginx ingress controller. У меня два фронта по https обращаются к одному gateway серверу. Ingress самого gateway у меня хранит сертификаты домена уровня *.domen.tech. А в аннотации nginx.ingress.kubernetes.io/cors-allow-origin пробовал ставить значение "https://front1.domen.tech, https://front2.domen.tech". Но когда фронт, с поддоменом domen.tech делает запросы на gateway, то браузер выдаёт ошибку:

Access to XMLHttpRequest at 'https://gateway.domen.tech/auth/login' from origin 'https://front1.domen.tech' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute


Перерыл весь issue самого nginx-ingress controller'а. Проблема оказалась актуально до сих пор. Понятное дело, если установить конкретный адрес в аннотацию nginx.ingress.kubernetes.io/cors-allow-origin, то всё заработает. Но просто проблема в том, что нужно, чтобы два фронта, обращались на один gateway сервер.

Я так же пробовал полностью удалять настройки CORS из Ingress самого gateway и перенести на уровень самого golang приложения - не вышло. Браузер уже выдавал ошибку:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.


Как исправить эту проблему?
  • Вопрос задан
  • 1139 просмотров
Пригласить эксперта
Ответы на вопрос 1
Viji
@Viji
Associate DevOps Engineer
А такой вариант не работает - просто дополнения к конф-ии nginx?

#
# Wide-open CORS config for nginx
#
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
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';
#
# Tell client that this pre-flight info is valid for 2 days
#
add_header 'Access-Control-Max-Age' 172800;
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-Credentials' 'true';
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';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
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';
}
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы