keycloak 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. # Root location for the main application
  39. location / {
  40. proxy_pass http://keycloak_upstream;
  41. }
  42. # Specific location for the token endpoint
  43. location ~ ^/auth/realms/[^/]+/protocol/openid-connect/token$ {
  44. proxy_pass http://keycloak_upstream;
  45. proxy_buffer_size 128k;
  46. proxy_buffers 4 256k;
  47. proxy_busy_buffers_size 256k;
  48. # WebSocket support (likely not needed for token endpoint, but keeping for consistency)
  49. proxy_set_header Upgrade $http_upgrade;
  50. proxy_set_header Connection "upgrade";
  51. # Timeouts
  52. proxy_connect_timeout 60s;
  53. proxy_send_timeout 60s;
  54. proxy_read_timeout 60s;
  55. }
  56. # Keycloak required paths
  57. location /realms/ {
  58. proxy_pass http://keycloak_upstream;
  59. proxy_buffer_size 128k;
  60. proxy_buffers 4 256k;
  61. proxy_busy_buffers_size 256k;
  62. # WebSocket support
  63. proxy_set_header Upgrade $http_upgrade;
  64. proxy_set_header Connection "upgrade";
  65. # Timeouts
  66. proxy_connect_timeout 60s;
  67. proxy_send_timeout 60s;
  68. proxy_read_timeout 60s;
  69. }
  70. location /resources/ {
  71. proxy_pass http://keycloak_upstream;
  72. # Cache settings for static resources
  73. proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
  74. proxy_cache_valid 200 1d;
  75. expires 1d;
  76. add_header Cache-Control "public" always;
  77. }
  78. location /robots.txt {
  79. proxy_pass http://keycloak_upstream;
  80. }
  81. # Block sensitive paths
  82. location /admin/ {
  83. allow 172.23.160.0/20;
  84. deny all;
  85. proxy_pass http://keycloak_upstream; # Forward to upstream Keycloak server
  86. proxy_set_header Host $host;
  87. proxy_set_header X-Real-IP $remote_addr;
  88. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  89. proxy_set_header X-Forwarded-Proto $scheme;
  90. }
  91. location /metrics {
  92. deny all;
  93. return 403;
  94. }
  95. location /health {
  96. deny all;
  97. return 403;
  98. }
  99. # Error pages
  100. error_page 403 /403.html;
  101. error_page 404 /404.html;
  102. error_page 500 502 503 504 /50x.html;
  103. # Deny access to hidden files
  104. location ~ /\. {
  105. deny all;
  106. return 404;
  107. }
  108. }