Skip to content

Commit aac0abc

Browse files
committed
experiment: Add reverse proxy path rewriting testbench
- Add Caddyfile.experiment with path stripping configuration - Add test script to validate the setup - Document the experiment approach and differences from main PR This tests if we can offload URL path handling entirely to the reverse proxy.
1 parent 9fa6a91 commit aac0abc

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

Caddyfile.experiment

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Experimental Caddy Configuration for Path Rewriting
2+
# This config strips the /codimd prefix before forwarding to the app
3+
# The app runs at root but knows it's behind a proxy at /codimd
4+
5+
{
6+
# Disable automatic HTTPS for local testing
7+
auto_https off
8+
}
9+
10+
:8080 {
11+
# Handle Socket.IO separately - rewrite path for WebSocket upgrade
12+
handle /codimd/socket.io/* {
13+
uri strip_prefix /codimd
14+
reverse_proxy localhost:3000 {
15+
# WebSocket support
16+
header_up X-Forwarded-For {remote_host}
17+
header_up X-Forwarded-Proto {scheme}
18+
header_up X-Forwarded-Host {host}
19+
header_up X-Forwarded-Prefix /codimd
20+
}
21+
}
22+
23+
# Handle all /codimd/* requests
24+
handle /codimd/* {
25+
# Strip /codimd prefix before forwarding
26+
uri strip_prefix /codimd
27+
28+
# Reverse proxy to app running at root
29+
reverse_proxy localhost:3000 {
30+
# Pass headers for proxy awareness
31+
header_up X-Forwarded-For {remote_host}
32+
header_up X-Forwarded-Proto {scheme}
33+
header_up X-Forwarded-Host {host}
34+
header_up X-Forwarded-Prefix /codimd
35+
}
36+
}
37+
38+
# Handle /codimd without trailing slash
39+
handle_path /codimd {
40+
redir /codimd/ 301
41+
}
42+
43+
# Redirect root to /codimd/
44+
handle / {
45+
redir /codimd/ 301
46+
}
47+
48+
# Enable logging for debugging
49+
log {
50+
output stdout
51+
level DEBUG
52+
format console
53+
}
54+
}

EXPERIMENT_README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Reverse Proxy Path Rewriting Experiment
2+
3+
This branch tests a **reverse proxy-only approach** where:
4+
- **App runs at root** (no URL path mounting in Express)
5+
- **App is aware of the URL path** (for generating correct URLs in templates)
6+
- **Caddy handles all path rewriting** (strips `/codimd` before forwarding)
7+
8+
## Configuration
9+
10+
### App Configuration (config.json)
11+
```json
12+
{
13+
"urlPath": "codimd", // App knows it's at /codimd for URL generation
14+
"domain": "localhost",
15+
"urlAddPort": true
16+
}
17+
```
18+
19+
### Caddy Configuration (Caddyfile.experiment)
20+
Caddy will:
21+
1. Strip `/codimd` prefix before forwarding to app
22+
2. App receives requests as if running at root
23+
3. App generates URLs with `/codimd` prefix (via serverURL)
24+
25+
## Key Differences from Main PR
26+
27+
| Aspect | Main PR Approach | This Experiment |
28+
|--------|------------------|-----------------|
29+
| Express mounting | Mounts at `/codimd` | Mounts at `/` |
30+
| Path handling | App handles path | Caddy strips path |
31+
| URL generation | Uses serverURL with path | Uses serverURL with path |
32+
| Standalone mode | ✅ Works | ❌ Requires proxy |
33+
| Socket.IO | App configures path | Caddy rewrites path |
34+
35+
## Testing
36+
37+
1. Start app (runs at root internally):
38+
```bash
39+
npm start
40+
```
41+
42+
2. Start Caddy with path rewriting:
43+
```bash
44+
caddy run --config Caddyfile.experiment
45+
```
46+
47+
3. Access at: `http://localhost:8080/codimd/`
48+
49+
## Expected Issues to Solve
50+
51+
1. ❓ Socket.IO path - needs special Caddy handling
52+
2. ❓ OAuth redirects - may need adjustment
53+
3. ❓ Asset URLs - should work if templates use serverURL correctly
54+
4. ❓ API calls - client-side code needs to use serverURL

test-reverse-proxy-experiment.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/bin/bash
2+
3+
# Test script for reverse proxy path rewriting experiment
4+
echo "🧪 Testing Reverse Proxy Path Rewriting Experiment"
5+
echo "=================================================="
6+
echo
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m' # No Color
13+
14+
# Check if app is running
15+
echo "1. Checking if CodiMD is running on port 3000..."
16+
if curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/ | grep -q "200\|302\|301"; then
17+
echo -e " ${GREEN}✅ CodiMD is running on port 3000${NC}"
18+
else
19+
echo -e " ${RED}❌ CodiMD is NOT running on port 3000${NC}"
20+
echo -e " ${YELLOW}Start it with: npm start${NC}"
21+
exit 1
22+
fi
23+
echo
24+
25+
# Check if Caddy is running
26+
echo "2. Checking if Caddy is running on port 8080..."
27+
if curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/ 2>/dev/null | grep -q "301\|200"; then
28+
echo -e " ${GREEN}✅ Caddy is running on port 8080${NC}"
29+
else
30+
echo -e " ${RED}❌ Caddy is NOT running on port 8080${NC}"
31+
echo -e " ${YELLOW}Start it with: caddy run --config Caddyfile.experiment${NC}"
32+
exit 1
33+
fi
34+
echo
35+
36+
# Test root redirect
37+
echo "3. Testing root redirect (/ → /codimd/)..."
38+
response=$(curl -s -I http://localhost:8080/ 2>/dev/null)
39+
if echo "$response" | grep -q "301 Moved Permanently" && echo "$response" | grep -q "Location:.*codimd"; then
40+
echo -e " ${GREEN}✅ Root redirect working${NC}"
41+
else
42+
echo -e " ${RED}❌ Root redirect failed${NC}"
43+
echo "$response"
44+
fi
45+
echo
46+
47+
# Test /codimd redirect to /codimd/
48+
echo "4. Testing /codimd redirect (→ /codimd/)..."
49+
response=$(curl -s -I http://localhost:8080/codimd 2>/dev/null)
50+
if echo "$response" | grep -q "301" && echo "$response" | grep -q "Location:.*codimd/"; then
51+
echo -e " ${GREEN}✅ /codimd redirect working${NC}"
52+
else
53+
echo -e " ${RED}❌ /codimd redirect failed${NC}"
54+
echo "$response"
55+
fi
56+
echo
57+
58+
# Test main app access
59+
echo "5. Testing main app access (/codimd/)..."
60+
response=$(curl -s -I http://localhost:8080/codimd/ 2>/dev/null)
61+
if echo "$response" | grep -q "200 OK"; then
62+
echo -e " ${GREEN}✅ Main app accessible${NC}"
63+
else
64+
echo -e " ${RED}❌ Main app access failed${NC}"
65+
echo "$response"
66+
fi
67+
echo
68+
69+
# Test static assets
70+
echo "6. Testing static assets (/codimd/favicon.png)..."
71+
response=$(curl -s -I http://localhost:8080/codimd/favicon.png 2>/dev/null)
72+
if echo "$response" | grep -q "200 OK"; then
73+
echo -e " ${GREEN}✅ Static assets working${NC}"
74+
else
75+
echo -e " ${RED}❌ Static assets failed${NC}"
76+
echo "$response"
77+
fi
78+
echo
79+
80+
# Test build assets
81+
echo "7. Testing webpack bundles (/codimd/build/...)..."
82+
# Find a build file
83+
buildFile=$(ls public/build/*.eot 2>/dev/null | head -1 | xargs basename 2>/dev/null)
84+
if [ -n "$buildFile" ]; then
85+
response=$(curl -s -I "http://localhost:8080/codimd/build/$buildFile" 2>/dev/null)
86+
if echo "$response" | grep -q "200 OK"; then
87+
echo -e " ${GREEN}✅ Build assets working${NC}"
88+
else
89+
echo -e " ${RED}❌ Build assets failed${NC}"
90+
echo "$response"
91+
fi
92+
else
93+
echo -e " ${YELLOW}⚠️ No build files found to test${NC}"
94+
fi
95+
echo
96+
97+
# Test what the app receives (check headers)
98+
echo "8. Checking what app receives from Caddy..."
99+
echo " Testing if X-Forwarded-Prefix header is passed..."
100+
# This would need app logging to verify
101+
echo -e " ${YELLOW}ℹ️ Check app logs to verify X-Forwarded-Prefix: /codimd${NC}"
102+
echo
103+
104+
# Test direct app access (should work at root)
105+
echo "9. Testing direct app access (without proxy)..."
106+
response=$(curl -s -I http://localhost:3000/ 2>/dev/null)
107+
if echo "$response" | grep -q "200\|302\|301"; then
108+
echo -e " ${GREEN}✅ App works at root (no URL path mounting)${NC}"
109+
echo -e " ${YELLOW}ℹ️ This means path rewriting experiment is active${NC}"
110+
else
111+
echo -e " ${YELLOW}⚠️ App response: $(echo "$response" | head -1)${NC}"
112+
fi
113+
echo
114+
115+
echo "=================================================="
116+
echo "🎯 Experiment Status Summary"
117+
echo "=================================================="
118+
echo
119+
echo "The reverse proxy path rewriting approach requires:"
120+
echo "1. ✅ Caddy to strip /codimd prefix before forwarding"
121+
echo "2. ✅ App to run at root (no URL path mounting)"
122+
echo "3. ⚠️ App to generate URLs with /codimd (via serverURL config)"
123+
echo "4. ⚠️ Socket.IO path rewriting in Caddy"
124+
echo
125+
echo "Access the app at: http://localhost:8080/codimd/"
126+
echo

0 commit comments

Comments
 (0)