Skip to content

Instantly share code, notes, and snippets.

@deangrant
Last active July 22, 2023 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deangrant/66fd61da3894b28efa951cfa68ef3818 to your computer and use it in GitHub Desktop.
Save deangrant/66fd61da3894b28efa951cfa68ef3818 to your computer and use it in GitHub Desktop.
Azure Functions configuration for local Docker deployment, requires nginx reverse proxy for CORS support.
# Sets the network port the container listens on at runtime for NGINX reverse proxy.
NGINX_PORT_EXPOSE=
# Sets the network port the container listens on at runtime for Azure Functions.
AZURE_FUNCTIONS_PORT_EXPOSE=
nginx:
hostname: nginx
build:
context: .
dockerfile: ./nginx/Dockerfile
init: true
environment:
- 'AZURE_FUNCTIONS_PORT_EXPOSE=${AZURE_FUNCTIONS_PORT_EXPOSE}'
ports:
- '${NGINX_PORT_EXPOSE}:80'
healthcheck:
test: [
"CMD",
"service",
"nginx",
"status"
]
interval: 30s
timeout: 5s
retries: 3
# Set the base image for subsequent instructions.
FROM nginx:latest
# Copy the template configuration file from the current
# directory to inside the container.
COPY ./nginx/nginx.template /etc/nginx/conf.d/default.template
# Uses envsubst to generate configuration file dynamically
# by replacing environment variables in the template file and
# outputting the results to the default.conf file and starts the
# nginx server.
CMD sh -c "envsubst \"`env | \
awk -F = '{printf \" \\\\$%s\", $1}'`\" \
< /etc/nginx/conf.d/default.template > \
/etc/nginx/conf.d/default.conf && \
nginx -g 'daemon off;'"
# Listens on port 80 and handles requests with the server_name set
# to localhost.
server
{
listen 80;
server_name localhost;
# Responsible for handling requests that match the specified URL path.
# It acts as a reverse proxy, forwarding the requests to the upstream
# server.
location /runtime/webhooks/durabletask/instances/
{
# Identifies the actual IP address of the client when requests
# are proxied, indicate request has been forwarded by a reverse
# proxy, and instructs the backend not to compress the response.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Accept-Encoding "";
# Specify the backend to which requests are forwarded.
proxy_pass http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE};
# Adds headers to allow cross-origin requests. For 'OPTIONS' requests,
# there is additional logic which is used for preflight requests.
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "*" always;
add_header 'Access-Control-Allow-Credentials' 'false' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' "*" always;
add_header 'Access-Control-Allow-Credentials' 'false' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
# Modifies the response headers of the reverse proxy response, to rewrite
# the server.
proxy_redirect http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE}/ http://localhost:7071/;
sub_filter_types application/json;
sub_filter 'http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE}' 'http://localhost:7071';
sub_filter_once off;
}
# Responsible for handling requests that match the specified URL path.
# It acts as a reverse proxy, forwarding the requests to the upstream
# server.
location /api/
{
# Identifies the actual IP address of the client when requests
# are proxied, indicate request has been forwarded by a reverse
# proxy, and instructs the backend not to compress the response.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Accept-Encoding "";
# Specify the backend to which requests are forwarded.
proxy_pass http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE};
# Adds headers to allow cross-origin requests. For 'OPTIONS' requests,
# there is additional logic which is used for preflight requests.
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "*" always;
add_header 'Access-Control-Allow-Credentials' 'false' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' "*" always;
add_header 'Access-Control-Allow-Credentials' 'false' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
# Modifies the response headers of the reverse proxy response, to rewrite
# the server.
proxy_redirect http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE}/ http://localhost:7071/;
sub_filter_types application/json;
sub_filter 'http://host.docker.internal:${AZURE_FUNCTIONS_PORT_EXPOSE}' 'http://localhost:7071';
sub_filter_once off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment