keycloak 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. upstream keycloak_upstream {
  2. server 172.18.0.3:8080;
  3. }
  4. server {
  5. listen 80;
  6. server_name auth.mrx8086.com;
  7. # Redirect HTTP to HTTPS
  8. return 301 https://$host$request_uri;
  9. }
  10. server {
  11. listen 443 ssl;
  12. server_name auth.mrx8086.com;
  13. # SSL Configuration
  14. ssl_certificate /etc/nginx/ssl/mrx8086.com/fullchain.pem;
  15. ssl_certificate_key /etc/nginx/ssl/mrx8086.com/privkey.pem;
  16. ssl_session_timeout 1d;
  17. ssl_session_tickets off;
  18. # Modern SSL configuration
  19. ssl_protocols TLSv1.2 TLSv1.3;
  20. ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  21. ssl_prefer_server_ciphers off;
  22. # Security headers
  23. add_header X-Content-Type-Options nosniff always;
  24. add_header X-XSS-Protection "1; mode=block" always;
  25. add_header X-Frame-Options SAMEORIGIN always;
  26. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  27. # Content Security Policy
  28. add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; frame-src 'self'; frame-ancestors 'self'; connect-src 'self'" always;
  29. # Proxy settings - Added X-Forwarded headers here to apply to all proxied locations
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. proxy_set_header X-Forwarded-Proto $scheme;
  33. proxy_set_header X-Forwarded-Ssl on; # Optional, but explicit
  34. proxy_set_header X-Forwarded-Port $server_port;
  35. proxy_set_header X-Forwarded-Host $host;
  36. proxy_set_header Host $host;
  37. proxy_http_version 1.1;
  38. # Cookies sicher machen
  39. proxy_cookie_flags ~ secure samesite=lax;
  40. # Specific location for the token endpoint
  41. location ~ ^/auth/realms/[^/]+/protocol/openid-connect/token$ {
  42. proxy_pass http://keycloak_upstream;
  43. proxy_buffer_size 128k;
  44. proxy_buffers 4 256k;
  45. proxy_busy_buffers_size 256k;
  46. # WebSocket support (likely not needed for token endpoint, but keeping for consistency)
  47. proxy_set_header Upgrade $http_upgrade;
  48. proxy_set_header Connection "upgrade";
  49. # Timeouts
  50. proxy_connect_timeout 60s;
  51. proxy_send_timeout 60s;
  52. proxy_read_timeout 60s;
  53. }
  54. # Keycloak required paths
  55. location ~ ^/realms/ {
  56. proxy_pass http://keycloak_upstream;
  57. proxy_buffer_size 128k;
  58. proxy_buffers 4 256k;
  59. proxy_busy_buffers_size 256k;
  60. # WebSocket support
  61. proxy_set_header Upgrade $http_upgrade;
  62. proxy_set_header Connection "upgrade";
  63. # Timeouts
  64. proxy_connect_timeout 60s;
  65. proxy_send_timeout 60s;
  66. proxy_read_timeout 60s;
  67. }
  68. location /resources/ {
  69. proxy_pass http://keycloak_upstream;
  70. # Cache settings for static resources
  71. proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
  72. proxy_cache_valid 200 1d;
  73. expires 1d;
  74. add_header Cache-Control "public" always;
  75. }
  76. location /robots.txt {
  77. proxy_pass http://keycloak_upstream;
  78. }
  79. # Block sensitive paths
  80. location /admin/ {
  81. allow 172.23.160.0/20;
  82. deny all;
  83. proxy_pass http://keycloak_upstream; # Forward to upstream Keycloak server
  84. proxy_set_header Host $host;
  85. proxy_set_header X-Real-IP $remote_addr;
  86. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  87. proxy_set_header X-Forwarded-Proto $scheme;
  88. }
  89. location /metrics {
  90. deny all;
  91. return 403;
  92. }
  93. location /health {
  94. deny all;
  95. return 403;
  96. }
  97. # Error pages
  98. error_page 403 /403.html;
  99. error_page 404 /404.html;
  100. error_page 500 502 503 504 /50x.html;
  101. # Deny access to hidden files
  102. location ~ /\. {
  103. deny all;
  104. return 404;
  105. }
  106. # Root location for the main application - this needs to be last
  107. location / {
  108. proxy_pass http://keycloak_upstream;
  109. }
  110. }