Skip to content

Instantly share code, notes, and snippets.

@fliphess
Last active May 23, 2021 13:29
Show Gist options
  • Save fliphess/0a565c638494945e5e1a1f56f52c2ec3 to your computer and use it in GitHub Desktop.
Save fliphess/0a565c638494945e5e1a1f56f52c2ec3 to your computer and use it in GitHub Desktop.
Custom error pages for nginx ingress controller
################################################################################
## Custom headers forwarded by the nginx ingress controller ##
## ##
## X-Code HTTP status code retuned by the request ##
## X-Format Value of the Accept header sent by the client ##
## X-Original-URI URI that caused the error ##
## X-Namespace Namespace where the backend Service is located ##
## X-Ingress-Name Name of the Ingress where the backend is defined ##
## X-Service-Name Name of the Service backing the backend ##
## X-Service-Port Port number of the Service backing the backend ##
## X-Request-ID Unique ID that identifies the request - ##
## same as for backend service ##
################################################################################
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
map $request_time $total_request_time {
default $request_time;
"" 0;
"~(-,)" 0;
"~([^,\s]+\s*)$" $1;
}
map $request_length $total_request_length {
default $request_length;
"" 0;
"~(-,)" 0;
"~([^,\s]+\s*)$" $1;
}
log_format main escape=json '{"@timestamp": "$time_iso8601","zs_component": "zaaksysteem-custom-errors", "zs_pod_name": "$hostname", "service_name": "$http_x_service_name", "service_port": "$http_x_service_port", "ingress_name": "$http_x_ingress_name", "ingress_request_id": "$http_x_request_id", "message": "$request", "vhost": "$host", "req": { "remote_addr": "$remote_addr", "x-forward-for": "$proxy_add_x_forwarded_for", "hostname": "$host", "namespace": "$http_x_namespace", "format": "$http_x_format", "instance_hostname": "$host", "action_path": "$http_x_original_uri", "remote_user": "$remote_user", "body_bytes_sent": $body_bytes_sent, "status": $status, "original_status": "$http_x_code", "request_uri": "$request_uri", "request_method": "$request_method", "request_time": $total_request_time, "request_length": $total_request_length, "request_proto": "$server_protocol", "request_completion": "$request_completion", "http_referrer": "$http_referer", "http_user_agent": "$http_user_agent"}}';
rewrite_log off;
access_log /dev/stdout main;
error_log /dev/stderr error;
map $http_x_code $internal_redirect {
default /523;
404 /404;
418 /418;
429 /429;
500 /500;
502 /502;
503 /503;
523 /523;
}
server {
listen 8080 default_server;
error_page 404 /error_pages/error_404.html;
error_page 429 /error_pages/error_429.html;
error_page 418 /error_pages/error_418.html;
error_page 500 /error_pages/error_500.html;
error_page 502 /error_pages/error_502.html;
error_page 503 /error_pages/error_503.html;
error_page 523 /error_pages/error_523.html;
location /healthz {
access_log off;
return 200 "healthy\n";
}
location /metrics {
stub_status on;
}
location @internal_redirect {
rewrite "^(.*)/?" $internal_redirect last;
}
location / {
try_files $uri @internal_redirect;
}
location /error_pages/ {
root /var/www/html;
}
location = /404 { internal; return 404; }
location = /418 { internal; return 418; }
location = /429 { internal; return 429; }
location = /500 { internal; return 500; }
location = /502 { internal; return 502; }
location = /503 { internal; return 503; }
location = /523 { internal; return 523; }
}
}
ingress:
...
defaultBackend:
enabled: true
replicaCount: 3
image:
runAsUser: 0 ## nginx forks to used nginx
pullPolicy: Always
repository: registry.gitlab.com/mintlab/devops/docker/ingress-custom-errors
tag: latest
...
imagePullSecrets:
- name: gitlab-key
...
controller:
config:
custom-http-errors: 404,429,418,500,502,503,523
FROM nginx:alpine
RUN set -ex \
&& rm -rvf /etc/nginx/nginx.conf /etc/nginx/conf.d
COPY www/ /var/www/html/
COPY nginx.conf /etc/nginx/nginx.conf
RUN date > /etc/buildtime
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment