File size: 4,169 Bytes
6a49f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Nginx Cache Control Configuration

## Issue

When the application is deployed behind an nginx reverse proxy, file content changes may not be reflected after saving due to nginx caching responses (issue #114).

**Symptoms:**

- Works correctly with direct uvicorn server (localhost)
- Fails when accessed through nginx reverse proxy
- Old file content is served even after saving changes
- Processing instructions (xml-model) added via TEI Wizard disappear after navigation

**Root Cause:**

- Backend sets proper Cache-Control headers (`no-cache, no-store, must-revalidate`)
- Nginx reverse proxy may ignore or override these headers
- Nginx caches responses by default unless explicitly configured not to

## Solution

Add a specific location block for `/api/` that disables nginx caching for all API endpoints and ensures Cache-Control headers are passed through.

### Configuration

Add this location block **before** the general `location /` block in your nginx configuration:

```nginx
# API endpoints - must not be cached by nginx (fixes #114)
location /api/ {
    proxy_pass http://127.0.0.1:8010;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;

    # Disable nginx caching - respect backend Cache-Control headers
    proxy_cache off;
    proxy_buffering off;
    proxy_no_cache 1;
    proxy_cache_bypass 1;

    # Ensure Cache-Control headers from backend are passed through
    proxy_pass_header Cache-Control;
    proxy_pass_header Pragma;
    proxy_pass_header Expires;

    # Reinforce cache headers at proxy level
    add_header Cache-Control "no-cache, no-store, must-revalidate" always;
    add_header Pragma "no-cache" always;
    add_header Expires "0" always;
}
```

### Key Directives Explained

- `proxy_cache off` - Disables nginx's proxy caching
- `proxy_buffering off` - Disables response buffering (helps with streaming responses)
- `proxy_no_cache 1` - Prevents caching even if cache is enabled globally
- `proxy_cache_bypass 1` - Bypasses cache for this location
- `proxy_pass_header` - Ensures backend headers are passed through
- `add_header ... always` - Adds headers to all responses (including errors)

### Deployment Steps

1. **Backup current configuration:**

   ```bash
   sudo cp /etc/nginx/sites-available/pdf-tei-editor-dev /etc/nginx/sites-available/pdf-tei-editor-dev.backup
   ```

2. **Update nginx configuration:**

   ```bash
   sudo nano /etc/nginx/sites-available/pdf-tei-editor-dev
   ```

   Add the location block from above **before** the `location /` block.

3. **Test configuration:**

   ```bash
   sudo nginx -t
   ```

4. **Reload nginx:**

   ```bash
   sudo systemctl reload nginx
   ```

5. **Verify the fix:**
   - Open a file in the editor
   - Make a change (e.g., add xml-model PI via TEI Wizard)
   - Save the file
   - Navigate to another file
   - Navigate back
   - Verify the changes are still present

### Reference Configuration

See [nginx-dev.conf](../../nginx-dev.conf) for a complete example configuration.

## Testing

To verify cache headers are working correctly:

```bash
# Check headers from your server
curl -I https://pdf-tei-editor-dev.panya.de/api/files/{stable_id}
```

Expected headers:

```
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
```

## Alternative: Disable Caching Globally

If you want to disable caching for all API endpoints:

```nginx
location /api/ {
    proxy_pass http://127.0.0.1:8010;
    # ... (same proxy settings as above)
    proxy_cache off;
    proxy_buffering off;
    # ... (same cache headers as above)
}
```

This is simpler but may impact performance for endpoints that could benefit from caching.

## See Also

- [Issue #114](https://github.com/user/repo/issues/114) - File content not persisting after save
- [fastapi_app/routers/files_serve.py](../../fastapi_app/routers/files_serve.py) - Backend cache headers
- [tests/api/v1/files_serve_caching.test.js](../../tests/api/v1/files_serve_caching.test.js) - Caching behavior test