diff --git a/Dockerfile b/Dockerfile index d5ea297..2189478 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,34 @@ -FROM nginx/nginx-prometheus-exporter as exporter +# Use the official Nginx base image FROM nginx:alpine -ENV PORT 80 +# Install curl to fetch nginx-prometheus-exporter +RUN apk add --no-cache curl tar -WORKDIR /app +# Set the version of nginx-prometheus-exporter to install +ENV NGINX_PROMETHEUS_EXPORTER_VERSION=1.1.2 -COPY app/ /usr/share/nginx/html -# copy config and exporter -COPY --from=exporter /usr/bin/nginx-prometheus-exporter /usr/bin/nginx-prometheus-exporter +# Download and install nginx-prometheus-exporter +RUN curl -L https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v${NGINX_PROMETHEUS_EXPORTER_VERSION}/nginx-prometheus-exporter_${NGINX_PROMETHEUS_EXPORTER_VERSION}_linux_amd64.tar.gz \ + | tar -zx -C /usr/local/bin + +# Copy the static application files to the Nginx document root +COPY ./app /usr/share/nginx/html + +# Copy the health check file +COPY health-check/liveness/index.html /usr/share/nginx/html/health-check/liveness/index.html +COPY health-check/readness/index.html /usr/share/nginx/html/health-check/readness/index.html + + +# Copy the custom Nginx configuration COPY conf/nginx.conf /etc/nginx/nginx.conf -COPY conf/supervisor.conf /etc/supervisor/supervisor.conf +COPY conf/default.conf /etc/nginx/conf.d/default.conf +COPY conf/stub_status.conf /etc/nginx/conf.d/stub_status.conf -RUN apk add --no-cache supervisor +# Expose ports for Nginx and Prometheus metrics +EXPOSE 80 9090 +# Add a health check instruction +HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost/health-check/liveness || exit 1 -EXPOSE 80 9113 -CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisor.conf"] +# Start Nginx and nginx-prometheus-exporter +CMD ["sh", "-c", "nginx -g 'daemon off;' & nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:8080/stub_status -web.listen-address :9090"] diff --git a/conf/default.conf b/conf/default.conf new file mode 100644 index 0000000..5bb6537 --- /dev/null +++ b/conf/default.conf @@ -0,0 +1,13 @@ +server { + listen 80; + location / { + root /usr/share/nginx/html; + index index.html; + } + location /health-check/liveness { + alias /usr/share/nginx/html/health-check/liveness/; + } + location /health-check/readness { + alias /usr/share/nginx/html/health-check/readness/; + } +} diff --git a/conf/nginx.conf b/conf/nginx.conf index 0b72ff4..20558f1 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,81 +1,27 @@ -user nginx; -worker_processes auto; - -error_log /var/log/nginx/error.log warn; -pid /var/run/nginx.pid; +user nginx; +worker_processes auto; +error_log /var/log/nginx/error.log; +pid /run/nginx.pid; events { - worker_connections 1024; + worker_connections 1024; } -include /etc/nginx/conf.d/*-root.conf; - http { - server_tokens off; - include /etc/nginx/mime.types; - default_type application/octet-stream; - - log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - '$status $body_bytes_sent "$http_referer" ' - '"$http_user_agent" "$http_x_forwarded_for"'; - - access_log /var/log/nginx/access.log main; - - keepalive_timeout 65; - sendfile on; - tcp_nopush on; - port_in_redirect off; - - server { - listen 80; - charset utf-8; - server_name _; - - real_ip_header x-forwarded-for; - set_real_ip_from 0.0.0.0/0; - real_ip_recursive on; + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; - root /app/dist; - index index.html; + access_log /var/log/nginx/access.log main; - location ~ /\. { - deny all; - access_log off; - log_not_found off; - return 404; - } + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; - location ~* \.(?:css|js)$ { - access_log off; - log_not_found off; - add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate"; - } + include /etc/nginx/mime.types; + default_type application/octet-stream; - location ~* \.(?:jpg|jpeg|gif|png|ico|xml|webp|eot|woff|woff2|ttf|svg|otf)$ { - access_log off; - log_not_found off; - expires 60m; - add_header Cache-Control "public"; - } - - location / { - try_files $uri $uri/ /; - } - - include /etc/nginx/conf.d/*-server.conf; - - } - - # metrics - server { - listen 8000; - server_name _; - - location /stub_status { - stub_status on; - access_log off; - } - } - - include /etc/nginx/conf.d/*-http.conf; -} \ No newline at end of file + include /etc/nginx/conf.d/*.conf; +} diff --git a/conf/stub_status.conf b/conf/stub_status.conf new file mode 100644 index 0000000..ff985af --- /dev/null +++ b/conf/stub_status.conf @@ -0,0 +1,8 @@ +server { + listen 8080; + location /stub_status { + stub_status; + allow 127.0.0.1; + deny all; + } +} diff --git a/conf/supervisor.conf b/conf/supervisor.conf deleted file mode 100644 index daf5be5..0000000 --- a/conf/supervisor.conf +++ /dev/null @@ -1,7 +0,0 @@ -[supervisord] -nodaemon=true -[program:nginx] -command=nginx -c /etc/nginx/nginx.conf - -[program:exporter] -command=nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:8000/stub_status \ No newline at end of file diff --git a/health-check/liveness/index.html b/health-check/liveness/index.html new file mode 100644 index 0000000..a26ff89 --- /dev/null +++ b/health-check/liveness/index.html @@ -0,0 +1 @@ +healthy diff --git a/health-check/readness/index.html b/health-check/readness/index.html new file mode 100644 index 0000000..ad50b53 --- /dev/null +++ b/health-check/readness/index.html @@ -0,0 +1 @@ +ready \ No newline at end of file