Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx security #195

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docker/nginx-gateway-k8s.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ proxy_cache_path /var/cache/nginx/pods levels=1:2 keys_zone=pods:2m max_size=4m
proxy_cache_path /var/cache/nginx/rbac levels=1:2 keys_zone=rbac:256k max_size=3m inactive=60m use_temp_path=off;
proxy_cache_path /var/cache/nginx/rbac2 levels=1:2 keys_zone=rbac2:256k max_size=3m inactive=60m use_temp_path=off;

# Setup a connection zone to control number of connections
limit_conn_zone $binary_remote_addr zone=limitconnbyaddr:20m;
# Return status code for too many connections
limit_conn_status 429;

map $uri $new {
/ /online/;
/online /online/;
Expand All @@ -24,6 +29,15 @@ server {
gzip on;
root /usr/share/nginx/html/;

# Limit a single IP to 75 connections
limit_conn limitconnbyaddr 75;

# Limit the keepalive of connections (60s default)
keepalive_timeout 60s;

# Do not show server version
server_tokens off;

# Performance tuning
subrequest_output_buffer_size ${NGINX_SUBREQUEST_OUTPUT_BUFFER_SIZE};
client_body_buffer_size ${NGINX_CLIENT_BODY_BUFFER_SIZE};
Expand All @@ -32,6 +46,16 @@ server {
# For debugging location rewrite
#rewrite_log on;

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}

# Prevent click jacking attacks
add_header X-Frame-Options "SAMEORIGIN";

# Only accept newer ssl protocols
ssl_protocols TLSv1.2 TLSv1.3;

if ($new) {
rewrite ^ $new redirect;
}
Expand Down
25 changes: 25 additions & 0 deletions docker/nginx-gateway.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ proxy_cache_path /var/cache/nginx/pods levels=1:2 keys_zone=pods:2m max_size=4m
proxy_cache_path /var/cache/nginx/rbac levels=1:2 keys_zone=rbac:256k max_size=3m inactive=60m use_temp_path=off;
proxy_cache_path /var/cache/nginx/rbac2 levels=1:2 keys_zone=rbac2:256k max_size=3m inactive=60m use_temp_path=off;

# Setup a connection zone to control number of connections
limit_conn_zone $binary_remote_addr zone=limitconnbyaddr:20m;
# Return status code for too many connections
limit_conn_status 429;

map $uri $new {
/ /online/;
/online /online/;
Expand All @@ -15,6 +20,7 @@ map $http_upgrade $connection_upgrade {
'' close;
}


server {
listen 8443 ssl;
server_name localhost;
Expand All @@ -24,6 +30,15 @@ server {
gzip on;
root /usr/share/nginx/html/;

# Limit a single IP to 75 connections
limit_conn limitconnbyaddr 75;

# Limit the keepalive of connections (60s default)
keepalive_timeout 60s;

# Do not show server version
server_tokens off;

# Performance tuning
subrequest_output_buffer_size ${NGINX_SUBREQUEST_OUTPUT_BUFFER_SIZE};
client_body_buffer_size ${NGINX_CLIENT_BODY_BUFFER_SIZE};
Expand All @@ -32,6 +47,16 @@ server {
# For debugging location rewrite
#rewrite_log on;

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}

# Prevent click jacking attacks
add_header X-Frame-Options "SAMEORIGIN";

# Only accept newer ssl protocols
ssl_protocols TLSv1.2 TLSv1.3;

if ($new) {
rewrite ^ $new redirect;
}
Expand Down
24 changes: 24 additions & 0 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Setup a connection zone to control number of connections
limit_conn_zone $binary_remote_addr zone=limitconnbyaddr:20m;
# Return status code for too many connections
limit_conn_status 429;

map $uri $new {
/ /online/;
}
Expand All @@ -16,6 +21,25 @@ server {
gzip on;
root /usr/share/nginx/html/;

# Limit a single IP to 75 connections
limit_conn limitconnbyaddr 75;

# Limit the keepalive of connections (60s default)
keepalive_timeout 60s;

# Do not show server version
server_tokens off;

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}

# Prevent click jacking attacks
add_header X-Frame-Options "SAMEORIGIN";

# Only accept newer ssl protocols
ssl_protocols TLSv1.2 TLSv1.3;

if ($new) {
rewrite ^ $new redirect;
}
Expand Down
5 changes: 2 additions & 3 deletions scripts/generate-proxying.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEMP_DIR=$(mktemp --tmpdir -d generate-proxying.XXXXXX)
exithandler() {
exitcode=$?
if [ "$exitcode" != "0" ]; then
echo "WARNING: unsuccessful exit code: $?" >&2
echo "WARNING: unsuccessful exit code: $exitcode" >&2
fi

rm -rf "$TEMP_DIR"
Expand Down Expand Up @@ -112,8 +112,7 @@ openssl req -new -key server.key -out server.csr -config csr.conf
# Issue the signed certificate
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 10000 -extensions v3_ext -extfile csr.conf

${KUBECLI} get secret ${SECRET_NAME} -n ${NAMESPACE} > /dev/null
if [ $? == 0 ]; then
if ${KUBECLI} get secret ${SECRET_NAME} -n ${NAMESPACE} 1> /dev/null 2>& 1; then
echo "The secret ${SECRET_NAME} in ${NAMESPACE} already exists"
exit 0
fi
Expand Down