docker-gen icon indicating copy to clipboard operation
docker-gen copied to clipboard

Adding CORS headers

Open adrianblakey opened this issue 6 years ago • 1 comments

I have been trying to add CORS headers to the nginx proxy. I have a simple test that does not seem to work. Any ideas?

I used the example in the docker-gen docs and did this:

docker run -d -p 80:80 --name nginx -v /tmp/nginx:/etc/nginx/conf.d -t nginx
docker run -d --name nginx-gen --volumes-from nginx    -v /var/run/docker.sock:/tmp/docker.sock:ro    -v /tmp/templates:/etc/docker-gen/templates    -t jwilder/docker-gen -notify-sighup nginx -watch -only-exposed /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf

I copied the nginx.tmpl from the nginx proxy into /tmp/templates and edited it.

Since I am doing a simple test without proxying anything I assume I can add headers to the "server {" - so that when I hit the server it will return the headers and the 503.

The stanza looks like this:

server {
        server_name _; # This is just an invalid value which will never trigger on a real hostname.
        listen 80;
        {{ if $enable_ipv6 }}
        listen [::]:80;
        {{ end }}
        access_log /var/log/nginx/access.log vhost;
        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';
        return 503;
}

If I docker exec -it id /bin/bash in the running nginx container I can see the /etc/nginx/conf.d/default.conf has the "add_header's" in it.

...
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
        server_name _; # This is just an invalid value which will never trigger on a real hostname.
        listen 80;
        access_log /var/log/nginx/access.log vhost;
        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';
        return 503;
}

If I point chrome at http://localhost and open the tools, the response headers only contain:

HTTP/1.1 503 Service Temporarily Unavailable
Server: nginx/1.15.12
Date: Fri, 10 May 2019 20:45:58 GMT
Content-Type: text/html
Content-Length: 600
Connection: keep-alive

Any ideas what I am doing wrong?

adrianblakey avatar May 10 '19 21:05 adrianblakey

Answer is here:

return 503;

See: http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain >variables.

gadost avatar Jun 07 '19 19:06 gadost