Skip to content

Commit 48389aa

Browse files
committed
Add logging and proxy info; dont cache 400/500
1 parent 09fd4df commit 48389aa

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

nginx.conf

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ events {
99

1010
http {
1111
#include mime.types;
12-
access_log /dev/stdout;
12+
13+
# Define custom log format including upstream information
14+
log_format upstream '$remote_addr - $remote_user [$time_local] '
15+
'"$request" $status $body_bytes_sent '
16+
'"$http_referer" "$http_user_agent" '
17+
'upstream_addr=$upstream_addr '
18+
'upstream_status=$upstream_status '
19+
'upstream_response_time=$upstream_response_time '
20+
'request_time=$request_time';
21+
22+
access_log /dev/stdout upstream;
1323
default_type application/octet-stream;
1424
sendfile on;
1525

@@ -19,6 +29,18 @@ http {
1929

2030
# proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=365d max_size=256g use_temp_path=off;
2131

32+
# Map to determine if we should store the response based on status code
33+
map $upstream_status $store_response {
34+
~^[45] off; # Don't store 4xx or 5xx responses
35+
default /data/nginx/$original_uri; # Store all other responses
36+
}
37+
38+
# Map to determine cache control headers based on status code
39+
map $upstream_status $cache_control {
40+
~^[45] "no-cache, no-store, must-revalidate"; # Don't cache errors
41+
default "public, max-age=31536000, immutable"; # Cache successful responses
42+
}
43+
2244
server {
2345
root /data/nginx/;
2446
listen 80;
@@ -28,7 +50,7 @@ http {
2850
sendfile_max_chunk 1m;
2951
tcp_nopush on;
3052
tcp_nodelay on;
31-
keepalive_timeout 65;
53+
keepalive_timeout 65;
3254
autoindex on;
3355
etag on;
3456

@@ -42,7 +64,7 @@ http {
4264
proxy_hide_header CF-RAY;
4365
proxy_hide_header X-DNS-Prefetch-Control;
4466
# end
45-
67+
4668

4769
# add header to understand caching behavior
4870
add_header X-Relisten-Audio-Cache $upstream_cache_status;
@@ -67,32 +89,41 @@ http {
6789

6890
set $original_uri '$uri';
6991

92+
# If the file exists, serve it with caching headers; otherwise, proxy to archive.org.
93+
try_files $uri @fetch_archive;
94+
95+
# For files served from disk (not proxied), set cache headers
7096
expires max;
7197
add_header Cache-Control "public, max-age=31536000, immutable";
72-
# If the file exists, serve it; otherwise, proxy to archive.org.
73-
try_files $uri @fetch_archive;
98+
add_header X-Relisten-Proxy-Status "cached" always;
7499
}
75100

76101
location ^~ /phish.in/.*\.mp3 {
77102
# include cors.conf;
78103

79104
set $original_uri '$uri';
80105

106+
# If the file exists, serve it with caching headers; otherwise, proxy to phish.in.
107+
try_files $uri @fetch_phishin;
108+
109+
# For files served from disk (not proxied), set cache headers
81110
expires max;
82111
add_header Cache-Control "public, max-age=31536000, immutable";
83-
# If the file exists, serve it; otherwise, proxy to archive.org.
84-
try_files $uri @fetch_phishin;
112+
add_header X-Relisten-Proxy-Status "cached" always;
85113
}
86114

87115
location ^~ /www.panicstream.com/.*\.mp3 {
88116
# include cors.conf;
89117

90118
set $original_uri '$uri';
91119

120+
# If the file exists, serve it with caching headers; otherwise, proxy to panicstream.
121+
try_files $uri @fetch_panicstream;
122+
123+
# For files served from disk (not proxied), set cache headers
92124
expires max;
93125
add_header Cache-Control "public, max-age=31536000, immutable";
94-
# If the file exists, serve it; otherwise, proxy to archive.org.
95-
try_files $uri @fetch_panicstream;
126+
add_header X-Relisten-Proxy-Status "cached" always;
96127
}
97128

98129
location @fetch_archive {
@@ -104,13 +135,21 @@ http {
104135

105136
proxy_set_header Host archive.org;
106137
proxy_pass https://archive.org;
107-
108-
include proxy.conf;
138+
139+
include proxy.conf;
109140

110141
# Intercept redirect responses.
111142
proxy_intercept_errors on;
112143
recursive_error_pages on;
113144
error_page 301 302 303 307 308 = @handle_redirect;
145+
146+
# Set cache headers conditionally based on response status
147+
add_header Cache-Control $cache_control always;
148+
149+
# Add header showing this was proxied and the upstream URL
150+
add_header X-Relisten-Proxy-URL "$scheme://$proxy_host$request_uri" always;
151+
add_header X-Relisten-Proxy-Status "proxied" always;
152+
add_header X-Relisten-Upstream-Status $upstream_status always;
114153
}
115154

116155
location @fetch_phishin {
@@ -122,13 +161,21 @@ http {
122161

123162
proxy_set_header Host phish.in;
124163
proxy_pass https://phish.in;
125-
126-
include proxy.conf;
164+
165+
include proxy.conf;
127166

128167
# Intercept redirect responses.
129168
proxy_intercept_errors on;
130169
recursive_error_pages on;
131170
error_page 301 302 303 307 308 = @handle_redirect;
171+
172+
# Set cache headers conditionally based on response status
173+
add_header Cache-Control $cache_control always;
174+
175+
# Add header showing this was proxied and the upstream URL
176+
add_header X-Relisten-Proxy-URL "$scheme://$proxy_host$request_uri" always;
177+
add_header X-Relisten-Proxy-Status "proxied" always;
178+
add_header X-Relisten-Upstream-Status $upstream_status always;
132179
}
133180

134181
location @fetch_panicstream {
@@ -140,22 +187,37 @@ http {
140187

141188
proxy_set_header Host phish.in;
142189
proxy_pass https://phish.in;
143-
144-
include proxy.conf;
190+
191+
include proxy.conf;
145192

146193
# Intercept redirect responses.
147194
proxy_intercept_errors on;
148195
recursive_error_pages on;
149196
error_page 301 302 303 307 308 = @handle_redirect;
197+
198+
# Set cache headers conditionally based on response status
199+
add_header Cache-Control $cache_control always;
200+
201+
# Add header showing this was proxied and the upstream URL
202+
add_header X-Relisten-Proxy-URL "$scheme://$proxy_host$request_uri" always;
203+
add_header X-Relisten-Proxy-Status "proxied" always;
204+
add_header X-Relisten-Upstream-Status $upstream_status always;
150205
}
151206

152207
# This location handles intercepted redirects.
153208
location @handle_redirect {
154209
# include cors.conf;
155-
include proxy.conf;
210+
include proxy.conf;
156211

157212
set $saved_redirect_location '$upstream_http_location';
158213
proxy_pass $saved_redirect_location;
214+
215+
# Set cache headers conditionally based on response status
216+
add_header Cache-Control $cache_control always;
217+
218+
# Add headers showing this was a redirect
219+
add_header X-Relisten-Proxy-URL $saved_redirect_location always;
220+
add_header X-Relisten-Proxy-Status "proxied-redirect" always;
159221
}
160222

161223
# return 200 'location 3';

proxy.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ proxy_buffering on;
22
proxy_request_buffering off;
33

44
# Store the fetched file to disk at /data/nginx/<request_uri>
5-
proxy_store /data/nginx/$original_uri;
5+
# Only store successful responses (not 4xx or 5xx errors)
6+
proxy_store $store_response;
67
proxy_store_access user:rw group:rw all:r;
78

89
# Allow for a flexible timeout given archive.org’s reliability issues

0 commit comments

Comments
 (0)