NGinx не добавляет CORS-заголовки, несмотря на явное добавление

На нашем сервере работает API, который должен быть доступен каждому. Этот API работает за nginx.

До сих пор все мои попытки добавить CORS-заголовки были тщетными - запросы обслуживаются, но без необходимых CORS-заголовков. Как ни странно, в один момент заголовки прошли через несколько часов, но затем исчезли, хотя конфигурация не была изменена.

Любая помощь очень ценится.

Мой nginx.conf ниже:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    include     /etc/nginx/mime.types;
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''   '';
    }


    #
    # Server configuration
    #
    server {

        # SSL configuration (this is generated by certbot)
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        index index.html index.htm index.nginx-debian.html;

        server_name api.our_domain.org;

         location /transit/ {


            proxy_pass http://localhost:5001;
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-PathBase /transit/;
            proxy_set_header Anyways-Original $request_uri;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header    X-Forwarded-Host $http_host;    # necessary for proper absolute redirects and TeamCity CSRF check
            proxy_set_header    X-Forwarded-Proto $scheme;
            proxy_set_header    X-Forwarded-For $remote_addr;


            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';
             }

        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/genk.anyways.eu/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/genk.anyways.eu/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = api.our_domain.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name api.our_domain.org;
    listen 80;
    return 404; # managed by Certbot
}

0 ответов

Другие вопросы по тегам