D
D
Denis Neichev2017-12-10 15:59:01
Nginx
Denis Neichev, 2017-12-10 15:59:01

Issues with CORS on Centos 7 with Nginx 1.12.2 on board. What could be wrong?

On a server running Centos 7 and Nginx 1.12.2, there is a CORS issue.
nginx config:

server {
    listen 80;
    server_name ;

    charset utf-8;
    client_max_body_size 128M;

    autoindex on;
    server_tokens off;

    root   /../../frontend/current;
    access_log  /var/log/nginx/access.log  main;
    error_log  /var/log/nginx/error.log warn;

    # - SSL CONFIGURATIONS
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # - REDIRECT FROM HTTP TO HTTPS
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }



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

        location /api/ {
            proxy_pass_request_headers  on;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header        Host $http_host;
                proxy_pass              http://127.0.0.1:3000/api/;
        }
    }
}

Go is used as backend. When contacting the server via CORS, it gives an error:
Запрос из постороннего источника заблокирован: Политика одного источника запрещает чтение удаленного ресурса на https://..../api/login. (Причина: отсутствует заголовок CORS «Access-Control-Allow-Origin»).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-12-10
@dneichev

add_header is not sent because if in Nginx doesn't work the way you think.
Use map instead of if.
https://www.nginx.com/resources/wiki/start/topics/...
agentzh.blogspot.ru/2011/03/how-nginx-location-if-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question