From 6e9430675502d4bac43cb32cd869f0028dc424fc Mon Sep 17 00:00:00 2001 From: "Guillaume \"B.B.\" Van Hemmen" Date: Mon, 21 Oct 2024 09:58:34 +0200 Subject: [PATCH] #0000 - Add Nginx setup to Dockerfile with CORS support This commit introduces the Nginx setup by using the official Nginx Alpine image and copying the custom Nginx configuration into the Docker container. It also includes CORS headers in the Nginx configuration to allow requests from any origin with specific headers and methods. --- Dockerfile | 11 +++++++++++ nginx.conf | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile index 1219c09..3c91e38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,14 @@ +# Use the official Nginx image from Docker Hub FROM nginx:alpine3.20 +# Copy your application files to the appropriate directory if needed COPY dist /usr/share/nginx/html + +# Copy custom Nginx configuration file to the container +COPY nginx.conf /etc/nginx/nginx.conf + +# Expose the port that the application is running on +EXPOSE 80 + +# Start Nginx when the container launches +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..5d89e32 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,27 @@ +server { + listen 80; + server_name yourdomain.com; + + location / { + # other settings... + + # Allow CORS for all domains (or specify a particular domain instead of *) + add_header 'Access-Control-Allow-Origin' '*'; + + # Allow specific headers + add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization'; + + # Allow specific methods + add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; + + if ($request_method = 'OPTIONS') { + add_header 'Access-Control-Allow-Origin' '*'; + add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; + add_header 'Access-Control-Max-Age' 1728000; + add_header 'Content-Type' 'text/plain; charset=utf-8'; + add_header 'Content-Length' 0; + return 204; + } + } +}