Skip to content

Commit ca3d219

Browse files
committed
Fix nginx relay WebSocket routing to handle both /relay and /relay/ paths
- Update nginx configuration to use regex matching for relay location - Add rewrite rule to strip /relay prefix before forwarding to relay service - Handle WebSocket connections for both URL formats that clients might use - Update README with complete working nginx configuration example - Update fixed_nginx_config.conf with tested configuration for reference
1 parent 0f74e2f commit ca3d219

File tree

2 files changed

+151
-22
lines changed

2 files changed

+151
-22
lines changed

README.md

Lines changed: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,132 @@ For development, you can run services directly:
111111

112112
### Nginx Proxy (Production Recommended)
113113
For production deployment, nginx handles:
114-
1. **Wallet Service Proxying**: `/wallet/*``localhost:9003`
115-
2. **SSL Termination**: Single certificate for entire application
116-
3. **WebSocket Proxying**: Proper upgrade headers for relay WebSocket
117-
4. **Static Asset Caching**: Optimal performance for React app
118-
5. **Security Headers**: CORS, CSP, and other protections
114+
1. **Relay WebSocket Proxying**: `/relay` and `/relay/``localhost:9001` (strips prefix)
115+
2. **Wallet Service Proxying**: `/wallet/*``localhost:9003`
116+
3. **SSL Termination**: Single certificate for entire application
117+
4. **WebSocket Proxying**: Proper upgrade headers for relay WebSocket
118+
5. **Static Asset Caching**: Optimal performance for React app
119+
6. **Security Headers**: CORS, CSP, and other protections
120+
121+
#### Complete Working Nginx Configuration
122+
Here's a complete working nginx configuration for the HORNETS Relay Panel (tested on macOS and Linux):
123+
124+
```nginx
125+
# Define upstream servers for each service (using explicit IPv4 addresses)
126+
upstream transcribe_api {
127+
server 127.0.0.1:8000;
128+
}
129+
130+
upstream relay_service {
131+
server 127.0.0.1:9001;
132+
}
133+
134+
upstream panel_service {
135+
server 127.0.0.1:9002;
136+
}
137+
138+
upstream wallet_service {
139+
server 127.0.0.1:9003;
140+
}
141+
142+
# WebSocket connection upgrade mapping
143+
map $http_upgrade $connection_upgrade {
144+
default upgrade;
145+
'' close;
146+
}
147+
148+
# Main server block listening on HTTP
149+
server {
150+
listen 80; # Nginx listens on port 80 locally
151+
server_name _; # Accept all hostnames (localhost, ngrok, custom domains, etc.)
152+
153+
# Basic Security Headers
154+
add_header X-Frame-Options "SAMEORIGIN";
155+
add_header X-Content-Type-Options "nosniff";
156+
add_header X-XSS-Protection "1; mode=block";
157+
server_tokens off;
158+
159+
# Increase buffer sizes for large files
160+
client_max_body_size 100M;
161+
162+
# Forward client IP and protocol
163+
proxy_set_header X-Real-IP $remote_addr;
164+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
165+
proxy_set_header X-Forwarded-Proto $scheme;
166+
proxy_set_header Host $host;
167+
168+
# Health check endpoint - exact match first
169+
location = /health {
170+
access_log off;
171+
return 200 "healthy\n";
172+
add_header Content-Type text/plain;
173+
}
174+
175+
# Relay WebSocket service - handle both /relay and /relay/
176+
location ~ ^/relay/?$ {
177+
# Strip the /relay prefix (with or without trailing slash) when forwarding to the service
178+
rewrite ^/relay/?$ / break;
179+
180+
proxy_pass http://relay_service;
181+
182+
# WebSocket-specific headers
183+
proxy_http_version 1.1;
184+
proxy_set_header Upgrade $http_upgrade;
185+
proxy_set_header Connection $connection_upgrade;
186+
proxy_set_header Host $host;
187+
proxy_cache_bypass $http_upgrade;
188+
189+
# Extended timeouts for WebSocket connections
190+
proxy_read_timeout 86400s;
191+
proxy_send_timeout 86400s;
192+
proxy_connect_timeout 60s;
193+
194+
# Additional headers for tunnel compatibility
195+
proxy_set_header X-Forwarded-Proto $scheme;
196+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
197+
proxy_set_header X-Real-IP $remote_addr;
198+
}
199+
200+
# Transcribe service
201+
location /transcribe/ {
202+
rewrite ^/transcribe/(.*)$ /$1 break;
203+
proxy_pass http://transcribe_api;
204+
}
205+
206+
# Wallet service
207+
location /wallet/ {
208+
rewrite ^/wallet/(.*)$ /$1 break;
209+
proxy_pass http://wallet_service;
210+
}
211+
212+
# Default location - Panel service (frontend + API) - MUST BE LAST
213+
location / {
214+
proxy_pass http://panel_service;
215+
proxy_set_header Host $host;
216+
proxy_set_header X-Real-IP $remote_addr;
217+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
218+
proxy_set_header X-Forwarded-Proto $scheme;
219+
220+
# Handle WebSocket if needed
221+
proxy_http_version 1.1;
222+
proxy_set_header Upgrade $http_upgrade;
223+
proxy_set_header Connection $connection_upgrade;
224+
}
225+
}
226+
```
227+
228+
**Key Configuration Details:**
229+
- **Relay WebSocket**: Uses regex matching `^/relay/?$` to handle both `/relay` and `/relay/` paths
230+
- **Rewrite Rule**: Strips the `/relay` prefix before forwarding to the relay service at port 9001
231+
- **WebSocket Support**: Proper upgrade headers and extended timeouts for WebSocket connections
232+
- **Service Routing**: Panel (root), wallet (`/wallet/`), transcribe (`/transcribe/`), and relay (`/relay`)
233+
- **Security**: Basic security headers and proper client IP forwarding
234+
235+
**Deployment Steps:**
236+
1. Save this configuration to `/etc/nginx/sites-available/hornets` (or `/opt/homebrew/etc/nginx/conf.d/hornets.conf` on macOS)
237+
2. Enable the site: `sudo ln -s /etc/nginx/sites-available/hornets /etc/nginx/sites-enabled/`
238+
3. Test configuration: `sudo nginx -t`
239+
4. Reload nginx: `sudo nginx -s reload`
119240

120241
## 📋 Prerequisites
121242

fixed_nginx_config.conf

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,27 @@ server {
3232
add_header X-XSS-Protection "1; mode=block";
3333
server_tokens off;
3434

35+
# Increase buffer sizes for large files
36+
client_max_body_size 100M;
37+
3538
# Forward client IP and protocol
3639
proxy_set_header X-Real-IP $remote_addr;
3740
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3841
proxy_set_header X-Forwarded-Proto $scheme;
3942
proxy_set_header Host $host;
4043

41-
location /transcribe/ {
42-
rewrite ^/transcribe/(.*)$ /$1 break;
43-
proxy_pass http://transcribe_api;
44+
# Health check endpoint - exact match first
45+
location = /health {
46+
access_log off;
47+
return 200 "healthy\n";
48+
add_header Content-Type text/plain;
4449
}
4550

46-
# Relay WebSocket service
47-
location /relay {
51+
# Relay WebSocket service - handle both /relay and /relay/
52+
location ~ ^/relay/?$ {
53+
# Strip the /relay prefix (with or without trailing slash) when forwarding to the service
54+
rewrite ^/relay/?$ / break;
55+
4856
proxy_pass http://relay_service;
4957

5058
# WebSocket-specific headers
@@ -65,29 +73,29 @@ server {
6573
proxy_set_header X-Real-IP $remote_addr;
6674
}
6775

76+
# Transcribe service
77+
location /transcribe/ {
78+
rewrite ^/transcribe/(.*)$ /$1 break;
79+
proxy_pass http://transcribe_api;
80+
}
81+
6882
# Wallet service
6983
location /wallet/ {
7084
rewrite ^/wallet/(.*)$ /$1 break;
7185
proxy_pass http://wallet_service;
72-
proxy_set_header Host $host;
73-
proxy_set_header X-Real-IP $remote_addr;
74-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
75-
proxy_set_header X-Forwarded-Proto $scheme;
7686
}
7787

78-
# Health check endpoint
79-
location /health {
80-
access_log off;
81-
return 200 "healthy\n";
82-
add_header Content-Type text/plain;
83-
}
84-
85-
# Default location - Panel service (frontend + API)
88+
# Default location - Panel service (frontend + API) - MUST BE LAST
8689
location / {
8790
proxy_pass http://panel_service;
8891
proxy_set_header Host $host;
8992
proxy_set_header X-Real-IP $remote_addr;
9093
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9194
proxy_set_header X-Forwarded-Proto $scheme;
95+
96+
# Handle WebSocket if needed
97+
proxy_http_version 1.1;
98+
proxy_set_header Upgrade $http_upgrade;
99+
proxy_set_header Connection $connection_upgrade;
92100
}
93101
}

0 commit comments

Comments
 (0)