Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding CORS headers #310

Open
adrianblakey opened this issue May 10, 2019 · 1 comment
Open

Adding CORS headers #310

adrianblakey opened this issue May 10, 2019 · 1 comment

Comments

@adrianblakey
Copy link

adrianblakey commented May 10, 2019

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?

@gadost
Copy link

gadost commented Jun 7, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants