Чтобы отключить проверку CORS (Cross-Origin Resource Sharing) на стороне клиента, необходимо добавить необходимые CORS-заголовки в конфигурацию вашего Nginx. Это позволяет вашему серверу отвечать с такими заголовками, которые сообщают браузерам, что к ресурсам разрешен доступ с других доменов. Вот пример того, как вы можете обновить ваш nginx.conf для добавления CORS-заголовков:
server {
listen 80;
server_name example.com;
# CORS headers
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' '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;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' '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;
root /usr/share/nginx/html;
index index.html;
}
location /adminer/ {
proxy_pass http://adminer:8080/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
location /api/auth/ {
proxy_pass http://auth-service:80/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
location /api/rights/ {
proxy_pass http://rights-service:80/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
location /api/skins/ {
proxy_pass http://minecraft-skins-service:80/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
location /api/minecraft-auth/ {
proxy_pass http://minecraft-auth-service:80/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
location /api/packs/ {
client_max_body_size 2000M;
proxy_pass http://pack-registry:80/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
include /etc/nginx/cors.conf;
}
}
# cors.conf file
# Save the following lines in separate file /etc/nginx/cors.conf
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' '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;
Не забудьте перезапустить Nginx после внесения изменений:
sudo nginx -t && sudo systemctl restart nginx
Этот пример устанавливает заголовки Access-Control-Allow-Origin на *, что разрешает доступ ко всем источникам. Пожалуйста, учтите, что такой подход по умолчанию предотвращает наталкивание на CORS-проблемы, но это может не подходить для всех случаев использования, особенно если у вас есть ограничения по безопасности. Вы можете поменять * на конкретный домен, чтобы ограничить доступ счастливыми доменами.