Browse Source

add nginx proxy cors stuff

master
Markus Bergholz 4 years ago
parent
commit
b3eb32adb5
  1. 65
      til.md

65
til.md

@ -177,4 +177,69 @@ async function main() {
}
main()
```
# nginx
proxy an api service and add cors headers and basic auth
```
load_module modules/ngx_http_headers_more_filter_module.so;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Authorization "Basic ZW5lcmdpY29zOmVuZXJnaWNvcw==";
set $cors "1";
if ($request_method = 'OPTIONS') {
set $cors "${cors}o";
}
if ($cors = "1") {
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Credentials: true';
}
if ($cors = "1o") {
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept';
add_header Content-Length 0;
add_header Content-Type text/plain;
return 204;
}
proxy_pass https://some.url;
}
}
}
```
A Dockerfile would look like this
```
FROM alpine:3.7
RUN apk --update --no-cache add nginx nginx-mod-http-headers-more
COPY nginx.conf /etc/nginx/nginx.conf
RUN mkdir /run/nginx
EXPOSE 80
CMD nginx -g 'daemon off;'
```
Loading…
Cancel
Save