Reverse Proxy Arkasında CORS Ayarlama

Reverse Proxy üzerinden çalışan servisler kimi zaman tutarsız bir şekilde CORS hatası alınmasına sebep olabiliyor, api isteklerinde olmazsa olmaz OPTIONS isteği Preflighted geçersiz oluyor ve sayfalar yüklenmiyor. Tabii ki bu durum her istekte meydana gelmiyor.(bilenler bilir pek hoş olmayan bir hatadır bu.)

Normalde kullandığımız genel yapılandırmada olan bütün isteklere eklediğimiz ve oldukça memnun olduğumuz CORS yapılandırmamız.

location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        #
        # 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;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
}

Reverse Proxy arkasında çalışan ve zaman zaman CORS hatası aldığımız için yeniden düzenlediğimiz ve iyileştirdiğimiz CORS yapılandırmamız.

location / { 
    # Simple requests
    if ($request_method ~* "(GET|POST|DELETE|PATCH)") {
        add_header "Access-Control-Allow-Origin"  * always;
    }

    # Preflighted requests
    if ($request_method = OPTIONS ) {
        add_header "Access-Control-Allow-Origin"  * always;
        add_header "Access-Control-Allow-Methods" "GET, POST, DELETE, PATCH, OPTIONS, HEAD";
        add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    proxy_redirect off;
    proxy_set_header Host $host;
    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_pass 127.0.0.1:8080;"
    proxy_read_timeout 300;
}

Sistem Uzmanı, Linux Hacısı, El-Kernel

Yorum yapın