@@ -4,33 +4,59 @@ events {
4
4
}
5
5
6
6
http {
7
+ map $http_upgrade $connection_upgrade {
8
+ default upgrade;
9
+ '' close;
10
+ }
11
+
7
12
# Upstream definition for HAProxy RPC load balancer
8
13
upstream rpc_nodes {
9
14
server 127.0.0.1:8545;
10
15
}
11
16
17
+ upstream ws_nodes {
18
+ server 127.0.0.1:8546;
19
+ }
20
+
12
21
server {
13
22
listen 8080 ;
14
23
15
24
# Main location for RPC traffic
16
25
location / {
17
- # Enable CORS from any origin
18
- # add_header 'Access-Control-Allow-Origin' '*' always;
19
- # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
20
- # add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
26
+ # Remove any existing CORS headers from upstream
27
+ proxy_hide_header 'Access-Control-Allow-Origin' ;
28
+ proxy_hide_header 'Access-Control-Allow-Methods' ;
29
+ proxy_hide_header 'Access-Control-Allow-Headers' ;
30
+ proxy_hide_header 'Access-Control-Max-Age' ;
21
31
22
- # # Handle preflight requests
23
- # if ($request_method = 'OPTIONS') {
24
- # add_header 'Access-Control-Allow-Origin' '*';
25
- # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
26
- # add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
27
- # add_header 'Access-Control-Max-Age' 1728000;
28
- # add_header 'Content-Type' 'text/plain; charset=utf-8';
29
- # add_header 'Content-Length' 0;
30
- # return 204;
31
- # }
32
-
33
- # Forward requests to HAProxy
32
+ # Handle preflight requests
33
+ if ($request_method = 'OPTIONS' ) {
34
+ add_header 'Access-Control-Allow-Origin' '*' ;
35
+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' ;
36
+ add_header 'Access-Control-Allow-Headers' 'Content-Type' ;
37
+ add_header 'Access-Control-Max-Age' 1728000 ;
38
+ add_header 'Content-Type' 'text/plain; charset=utf-8' ;
39
+ add_header 'Content-Length' 0 ;
40
+ return 204 ;
41
+ }
42
+
43
+ # Add CORS headers for non-OPTIONS requests
44
+ if ($request_method != 'OPTIONS' ) {
45
+ add_header 'Access-Control-Allow-Origin' '*' ;
46
+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' ;
47
+ add_header 'Access-Control-Allow-Headers' 'Content-Type' ;
48
+ }
49
+
50
+ # WebSocket specific configuration
51
+ proxy_http_version 1.1 ;
52
+ proxy_set_header Upgrade $http_upgrade ;
53
+ proxy_set_header Connection $connection_upgrade ;
54
+
55
+ # Route WebSocket traffic to ws_nodes, regular traffic to rpc_nodes
56
+ if ($http_upgrade = "websocket" ) {
57
+ proxy_pass http://ws_nodes;
58
+ }
59
+
34
60
proxy_pass http://rpc_nodes;
35
61
proxy_set_header Host $host ;
36
62
proxy_set_header X-Real-IP $remote_addr ;
51
77
# Short timeouts for quick health check responses
52
78
proxy_connect_timeout 2s ;
53
79
proxy_read_timeout 2s ;
54
- proxy_pass http://127.0.0.1:8546 /health;
80
+ proxy_pass http://127.0.0.1:8547 /health;
55
81
add_header Content-Type text/plain;
56
82
}
57
83
}
0 commit comments