Skip to content

Instantly share code, notes, and snippets.

@rubot
Last active April 5, 2020 18:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rubot/10c79ee0086a8a246eb43ab631f3581f to your computer and use it in GitHub Desktop.
Save rubot/10c79ee0086a8a246eb43ab631f3581f to your computer and use it in GitHub Desktop.
server {
listen 443 ssl http2 proxy_protocol;
include /ssl.conf.include;
ssl_certificate /etc/nginx/certs/main.crt;
ssl_certificate_key /etc/nginx/certs/main.key;
server_name example.org;
#auth_basic "closed site";
#auth_basic_user_file /run/secrets/default.htpasswd;
set $upstream_endpoint app;
location / {
proxy_pass http://$upstream_endpoint;
}
}
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;
client_max_body_size 128M;
# oscp-stapling enabled
# how: https://raymii.org/s/tutorials/OCSP_Stapling_on_nginx.html
# why: https://letsencrypt.org/docs/integration-guide/
ssl_stapling on;
ssl_stapling_verify on;
#resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Enable use of replicas when they change their ip.
# For round robin, change endpoint_mode under docker deploy tag
# 127.0.0.11 is docker swarms dns server
resolver 127.0.0.11 valid=30s;
# Dont log health checks
# https://bjornjohansen.no/exclude-requests-from-nginx-access-log
map $remote_addr $log_ip {
"127.0.0.1" 0;
default 1;
}
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
default $http_x_forwarded_proto;
'' $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
default $http_x_forwarded_port;
'' $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
default upgrade;
'' close;
}
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
default off;
https on;
}
# https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
# https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/
map $proxy_protocol_addr $real_remote_addr {
default $proxy_protocol_addr;
'' $remote_addr;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '[$time_local] $server_port $host $real_remote_addr - $remote_user '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log vhost if=$log_ip;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $real_remote_addr;
proxy_set_header X-Forwarded-For $real_remote_addr;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
# three level subdomains for pages caused that problem
# https://gist.github.com/muhammadghazali/6c2b8c80d5528e3118613746e0041263
# Apply fix for very long server names
server_names_hash_bucket_size 128;
#sendfile off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
include /etc/nginx/stream.d/*.stream;
stream {
resolver_timeout 5s;
# Enable use of replicas when they change their ip.
# For round robin, change endpoint_mode under docker deploy tag
# 127.0.0.11 is docker swarms dns server
resolver 127.0.0.11 valid=30s;
#log_format vstream '-: $hostname : $protocol : $proxy_protocol_addr : $remote_addr : $server_addr :-';
#access_log /var/log/nginx/access.log vstream;
#error_log /var/log/nginx/error.log info;
# set does not work in stream module, using map here
map '' $upstream_endpoint {
default proxy_nginx:443;
}
server {
listen 443;
proxy_pass $upstream_endpoint;
proxy_protocol on;
}
}
configs:
nginx.conf:
external: true
name: nginx.conf
nginx.stream:
external: true
name: nginx.stream
example.conf:
external: true
name: example.conf
real_ip.conf:
external: true
name: real_ip.conf
ssl.conf.include:
external: true
name: ssl.conf.include
services:
nginx:
configs:
- source: nginx.conf
target: /etc/nginx/nginx.conf
- source: real_ip.conf
target: /etc/nginx/conf.d/real_ip.conf
- source: example.conf
target: /etc/nginx/conf.d/example.conf
- source: ssl.conf.include
deploy:
replicas: 2
update_config:
delay: 10s
parallelism: 1
image: nginx:1-alpine
stream:
configs:
- source: nginx.conf
target: /etc/nginx/nginx.conf
- source: nginx.stream
target: /etc/nginx/stream.d/nginx.stream
deploy:
mode: global
update_config:
delay: 10s
parallelism: 1
image: nginx:1-alpine
ports:
- mode: host
protocol: tcp
published: 443
target: 443
version: '3.6'
set_real_ip_from 10.0.0.0/16;
real_ip_header proxy_protocol;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000";
server_tokens off;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment