nginx
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| nginx [2023-09-05 17:45:40] – [HTTP Header] david | nginx [2024-07-27 22:34:48] (aktuell) – [SSL/TLS] david | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== NGINX ====== | ||
| + | |||
| + | * Neues: [[https:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | ===== Basic Authentication ===== | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | ===== Konfiguration auf FreeBSD ===== | ||
| + | |||
| + | > cd / | ||
| + | > cd / | ||
| + | > cd / | ||
| + | |||
| + | <code txt / | ||
| + | ... | ||
| + | nginx_enable=YES | ||
| + | php_fpm_enable=YES | ||
| + | fcgiwrap_enable=" | ||
| + | fcgiwrap_user=" | ||
| + | fcgiwrap_socket=" | ||
| + | fcgiwrap_socket_owner=" | ||
| + | fcgiwrap_socket_mode=" | ||
| + | </ | ||
| + | |||
| + | > / | ||
| + | > / | ||
| + | > / | ||
| + | |||
| + | |||
| + | ===== Konfigurationsdatei (FreeBSD) ===== | ||
| + | <code nginx / | ||
| + | # Redirect all HTTP traffic to HTTPS | ||
| + | server { | ||
| + | listen 80 default_server; | ||
| + | listen [::]:80 default_server; | ||
| + | |||
| + | location / { | ||
| + | return 301 https:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # Default server | ||
| + | server { | ||
| + | listen 443 ssl http2 default_server; | ||
| + | listen [::]:443 ssl http2 default_server; | ||
| + | |||
| + | ssl_certificate | ||
| + | ssl_certificate_key | ||
| + | |||
| + | location / { | ||
| + | return 404; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + | listen 443 ssl http2; | ||
| + | listen [::]:443 ssl http2; | ||
| + | |||
| + | # Server Whitelist for SNI Header (Server Name Indication) | ||
| + | server_name EXAMPLE.COM *.TESTSERVER.COM 10.0.0.100; | ||
| + | |||
| + | ssl_certificate | ||
| + | ssl_certificate_key | ||
| + | |||
| + | # verify chain of trust of OCSP response using Root CA and Intermediate certs | ||
| + | ssl_trusted_certificate / | ||
| + | |||
| + | # OCSP stapling | ||
| + | ssl_stapling on; | ||
| + | ssl_stapling_verify on; | ||
| + | |||
| + | # Improve HTTPS performance with session resumption | ||
| + | ssl_session_timeout 1d; | ||
| + | ssl_session_cache shared: | ||
| + | ssl_session_tickets off; | ||
| + | |||
| + | # only TLS 1.3 ciphers -> 100% ssllabs (cipher strength) | ||
| + | ssl_protocols TLSv1.3; | ||
| + | ssl_prefer_server_ciphers on; | ||
| + | |||
| + | # curves which are equivalent of >=4096 rsa (only secp521r1 and secp384r1) -> 100% ssllabs (kex/key exchange) | ||
| + | ssl_ecdh_curve secp521r1: | ||
| + | |||
| + | # HSTS (ngx_http_headers_module is required) | ||
| + | add_header Strict-Transport-Security " | ||
| + | |||
| + | # replace with the IP address of your resolver | ||
| + | #resolver 127.0.0.1; | ||
| + | #resolver [2606: | ||
| + | resolver 192.168.1.4 [2606: | ||
| + | |||
| + | root /home/http; | ||
| + | |||
| + | index index.html index.php doku.php; | ||
| + | |||
| + | client_max_body_size 15M; | ||
| + | client_body_buffer_size 128K; | ||
| + | |||
| + | location / | ||
| + | { | ||
| + | try_files $uri $uri/ @dokuwiki; | ||
| + | } | ||
| + | |||
| + | location ^~ /conf/ | ||
| + | { | ||
| + | return 403; | ||
| + | } | ||
| + | |||
| + | location ^~ /data/ | ||
| + | { | ||
| + | return 403; | ||
| + | } | ||
| + | |||
| + | location ~ /\.ht | ||
| + | { | ||
| + | deny all; | ||
| + | } | ||
| + | |||
| + | location @dokuwiki | ||
| + | { | ||
| + | rewrite ^/ | ||
| + | rewrite ^/ | ||
| + | rewrite ^/ | ||
| + | rewrite ^/ | ||
| + | } | ||
| + | |||
| + | location ~ \.php$ | ||
| + | { | ||
| + | include fastcgi_params; | ||
| + | fastcgi_pass 127.0.0.1: | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | } | ||
| + | |||
| + | location /cgi-bin/ | ||
| + | { | ||
| + | include fcgiwrap_params; | ||
| + | fastcgi_pass unix:/ | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | } | ||
| + | |||
| + | location ~ / | ||
| + | { | ||
| + | auth_basic | ||
| + | auth_basic_user_file | ||
| + | |||
| + | include fcgiwrap_params; | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | fastcgi_pass | ||
| + | } | ||
| + | | ||
| + | location / | ||
| + | { | ||
| + | auth_basic | ||
| + | auth_basic_user_file | ||
| + | |||
| + | autoindex on; | ||
| + | } | ||
| + | |||
| + | location /foto/ | ||
| + | { | ||
| + | auth_basic | ||
| + | auth_basic_user_file | ||
| + | } | ||
| + | |||
| + | location ~* ^(\/ | ||
| + | proxy_pass http:// | ||
| + | proxy_set_header X-Forwarded-For $remote_addr; | ||
| + | proxy_set_header X-Forwarded-Proto $scheme; | ||
| + | proxy_set_header Host $host; | ||
| + | |||
| + | # Nginx by default only allows file uploads up to 1M in size | ||
| + | # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml | ||
| + | client_max_body_size 50M; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Beispielkonfiguration (Ubuntu) ===== | ||
| + | |||
| + | Unterstützt PHP, CGI und Webapps hinter einem Reverse Proxy (z.B. NodeJS und ASP.NET Core) | ||
| + | |||
| + | <code nginx / | ||
| + | # Redirect all HTTP traffic to HTTPS | ||
| + | server { | ||
| + | listen 80 default_server; | ||
| + | listen [::]:80 default_server; | ||
| + | |||
| + | location / { | ||
| + | return 301 https:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # Default server | ||
| + | server { | ||
| + | listen 443 ssl http2 default_server; | ||
| + | listen [::]:443 ssl http2 default_server; | ||
| + | |||
| + | include snippets/ | ||
| + | |||
| + | location / { | ||
| + | return 404; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + | listen 443 ssl http2; | ||
| + | listen [::]:443 ssl http2; | ||
| + | |||
| + | include snippets/ | ||
| + | |||
| + | # Server Whitelist for SNI Header (Server Name Indication) | ||
| + | server_name EXAMPLE.COM *.TESTSERVER.COM 10.0.0.100; | ||
| + | |||
| + | root /var/www; | ||
| + | |||
| + | index index.html index.php; | ||
| + | |||
| + | # replace with the IP address of your resolver | ||
| + | #resolver 127.0.0.1; | ||
| + | resolver [2606: | ||
| + | |||
| + | # redirect to custom 404 error page on 403 or 404 | ||
| + | error_page 403 =404 /404.html; | ||
| + | error_page 404 =404 /404.html; | ||
| + | |||
| + | client_max_body_size 15M; | ||
| + | client_body_buffer_size 128K; | ||
| + | |||
| + | location / | ||
| + | { | ||
| + | autoindex off; | ||
| + | try_files $uri $uri/ =404; | ||
| + | } | ||
| + | |||
| + | location ~ /\.ht | ||
| + | { | ||
| + | deny all; | ||
| + | } | ||
| + | |||
| + | location ^~ /data/ | ||
| + | { | ||
| + | autoindex on; | ||
| + | auth_basic " | ||
| + | auth_basic_user_file / | ||
| + | } | ||
| + | |||
| + | # CGI | ||
| + | location ^~ /cgi-bin/ | ||
| + | { | ||
| + | include fastcgi_params; | ||
| + | |||
| + | fastcgi_pass unix:/ | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | } | ||
| + | |||
| + | # PHP | ||
| + | location ~ \.php$ | ||
| + | { | ||
| + | include snippets/ | ||
| + | include fastcgi_params; | ||
| + | |||
| + | fastcgi_pass unix:/ | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | } | ||
| + | |||
| + | # NodeJS | ||
| + | location ^~ / | ||
| + | { | ||
| + | include snippets/ | ||
| + | proxy_pass http:// | ||
| + | } | ||
| + | |||
| + | # ASP.NET Core | ||
| + | location ^~ / | ||
| + | { | ||
| + | include snippets/ | ||
| + | proxy_pass http:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Konfiguration mit Snippets (Ubuntu) ===== | ||
| + | |||
| + | <code nginx / | ||
| + | user www-data; | ||
| + | worker_processes auto; | ||
| + | pid / | ||
| + | include / | ||
| + | |||
| + | events { | ||
| + | worker_connections 768; | ||
| + | # multi_accept on; | ||
| + | } | ||
| + | |||
| + | http { | ||
| + | map $http_upgrade $connection_upgrade { | ||
| + | default upgrade; | ||
| + | '' | ||
| + | } | ||
| + | |||
| + | include mime.types; | ||
| + | default_type application/ | ||
| + | |||
| + | include snippets/ | ||
| + | http2 on; | ||
| + | http3 on; | ||
| + | |||
| + | client_body_buffer_size 128K; | ||
| + | client_max_body_size 128M; | ||
| + | fastcgi_hide_header X-Powered-By; | ||
| + | gzip on; | ||
| + | index index.html; | ||
| + | proxy_hide_header X-Powered-By; | ||
| + | resolver [2606: | ||
| + | sendfile on; | ||
| + | server_tokens off; | ||
| + | tcp_nopush on; | ||
| + | types_hash_max_size 2048; | ||
| + | |||
| + | ## | ||
| + | # Basic Settings | ||
| + | ## | ||
| + | |||
| + | # | ||
| + | # | ||
| + | |||
| + | # server_names_hash_bucket_size 64; | ||
| + | # server_name_in_redirect off; | ||
| + | |||
| + | #include mime.types; | ||
| + | # | ||
| + | |||
| + | ## | ||
| + | # SSL Settings | ||
| + | ## | ||
| + | |||
| + | # | ||
| + | # | ||
| + | # | ||
| + | |||
| + | ## | ||
| + | # Logging Settings | ||
| + | ## | ||
| + | |||
| + | access_log / | ||
| + | error_log / | ||
| + | |||
| + | ## | ||
| + | # Gzip Settings | ||
| + | ## | ||
| + | |||
| + | #gzip on; | ||
| + | |||
| + | # gzip_vary on; | ||
| + | # gzip_proxied any; | ||
| + | # gzip_comp_level 6; | ||
| + | # gzip_buffers 16 8k; | ||
| + | # gzip_http_version 1.1; | ||
| + | # gzip_types text/plain text/css application/ | ||
| + | |||
| + | ## | ||
| + | # Virtual Host Configs | ||
| + | ## | ||
| + | |||
| + | include / | ||
| + | include / | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code nginx / | ||
| + | server { | ||
| + | listen 80 default_server; | ||
| + | listen [::]:80 default_server; | ||
| + | |||
| + | include snippets/ | ||
| + | |||
| + | location / { | ||
| + | return 308 https:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + | listen 443 quic default_server; | ||
| + | listen [::]:443 quic default_server; | ||
| + | listen 443 ssl default_server; | ||
| + | listen [::]:443 ssl default_server; | ||
| + | |||
| + | include snippets/ | ||
| + | include snippets/ | ||
| + | |||
| + | location / { | ||
| + | return 404; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code nginx / | ||
| + | server { | ||
| + | listen 443 quic; | ||
| + | listen [::]:443 quic; | ||
| + | listen 443 ssl; | ||
| + | listen [::]:443 ssl; | ||
| + | |||
| + | server_name ~^www.(?< | ||
| + | |||
| + | include snippets/ | ||
| + | include snippets/ | ||
| + | |||
| + | location / { | ||
| + | return 308 https:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code nginx / | ||
| + | server { | ||
| + | listen 443 quic; | ||
| + | listen [::]:443 quic; | ||
| + | listen 443 ssl; | ||
| + | listen [::]:443 ssl; | ||
| + | |||
| + | # Server Whitelist for SNI Header (Server Name Indication) | ||
| + | server_name EXAMPLE.COM; | ||
| + | root / | ||
| + | |||
| + | include snippets/ | ||
| + | include snippets/ | ||
| + | include snippets/ | ||
| + | |||
| + | location / { | ||
| + | try_files $uri $uri/ =404; | ||
| + | } | ||
| + | |||
| + | location /data/ { | ||
| + | auth_basic " | ||
| + | auth_basic_user_file / | ||
| + | |||
| + | autoindex on; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Snippets ==== | ||
| + | |||
| + | === SSL/TLS === | ||
| + | |||
| + | siehe [[letsencrypt# | ||
| + | |||
| + | <code nginx / | ||
| + | # ssl certificate and private key | ||
| + | # ECDSA | ||
| + | ssl_certificate | ||
| + | ssl_certificate_key | ||
| + | |||
| + | # RSA | ||
| + | ssl_certificate | ||
| + | ssl_certificate_key | ||
| + | |||
| + | # SSL Labs (Cipher Strength): min. AES-256 equivalent for 100% grade (TLS 1.3 requires a AES-128 cipher tho) | ||
| + | ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384: | ||
| + | ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384: | ||
| + | # | ||
| + | ssl_conf_command Options ServerPreference, | ||
| + | |||
| + | # curl -so / | ||
| + | # ln -s / | ||
| + | ssl_dhparam dhparam.pem; | ||
| + | |||
| + | # SSL Labs (Key Exchange): min RSA 4096 equivalent curves for 100% grade (x25519 is only equivalent to RSA 3072) | ||
| + | ssl_ecdh_curve X448: | ||
| + | # | ||
| + | |||
| + | ssl_prefer_server_ciphers on; | ||
| + | ssl_protocols TLSv1.3 TLSv1.2; | ||
| + | |||
| + | # improve HTTPS performance with session resumption | ||
| + | ssl_session_timeout 1d; | ||
| + | ssl_session_cache shared: | ||
| + | ssl_session_tickets off; | ||
| + | |||
| + | # OCSP stapling | ||
| + | ssl_stapling on; | ||
| + | ssl_stapling_verify on; | ||
| + | |||
| + | # verify chain of trust of OCSP response using root ca and intermediate certs | ||
| + | # combine to one file as this directive can only be specified once | ||
| + | # cat / | ||
| + | ssl_trusted_certificate / | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | === HTTP Header === | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | <code nginx / | ||
| + | # HSTS | ||
| + | # 1 year = 31536000 seconds | ||
| + | #add_header Strict-Transport-Security ' | ||
| + | # HSTS preloading (see https:// | ||
| + | add_header Strict-Transport-Security ' | ||
| + | |||
| + | # HTTP/2 & HTTP/3 | ||
| + | # 1 day = 86400 seconds | ||
| + | add_header Alt-Svc ' | ||
| + | |||
| + | add_header Referrer-Policy ' | ||
| + | |||
| + | add_header X-Content-Type-Options ' | ||
| + | add_header X-XSS-Protection '1; mode=block' | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | === HTTP Header (Policy) === | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | <code nginx / | ||
| + | add_header Content-Security-Policy " | ||
| + | |||
| + | add_header Permissions-Policy " | ||
| + | </ | ||
| + | |||
| + | |||
| + | === PHP & CGI Location-Blöcke === | ||
| + | |||
| + | <code nginx / | ||
| + | # FastCGI | ||
| + | |||
| + | # these two lines | ||
| + | #include fastcgi_params; | ||
| + | # | ||
| + | # can also be replaced by | ||
| + | #include snippets/ | ||
| + | # if it exists | ||
| + | |||
| + | # CGI | ||
| + | location ~ / | ||
| + | { | ||
| + | if (!-f $request_filename) { return 404; } | ||
| + | include fastcgi_params; | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | fastcgi_param HTTPS $https; | ||
| + | fastcgi_pass unix:/ | ||
| + | } | ||
| + | |||
| + | # PHP | ||
| + | location ~ \.php$ | ||
| + | { | ||
| + | if (!-f $request_filename) { return 404; } | ||
| + | include fastcgi_params; | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| + | fastcgi_param HTTPS $https; | ||
| + | fastcgi_pass unix:/ | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Reverse Proxy Header für Webapplikationen === | ||
| + | |||
| + | <code nginx / | ||
| + | # Proxy Headers for web apps | ||
| + | proxy_http_version 1.1; | ||
| + | proxy_set_header Connection ' | ||
| + | proxy_set_header Connection keep-alive; | ||
| + | proxy_set_header Upgrade $http_upgrade; | ||
| + | proxy_set_header Host $host; | ||
| + | proxy_set_header X-Real-IP $remote_addr; | ||
| + | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| + | proxy_set_header X-Forwarded-Proto $scheme; | ||
| + | proxy_cache_bypass $http_upgrade; | ||
| + | </ | ||
| + | |||
| + | |||
| + | === WebM MIME-Types für Matroska Dateien === | ||
| + | |||
| + | * WebM MIME types for Matroska files | ||
| + | |||
| + | <code nginx / | ||
| + | location ~ \.mkv$ { | ||
| + | types { | ||
| + | video/webm mkv; | ||
| + | audio/webm mka; | ||
| + | text/vtt mks; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
