Skip to content

Commit b5ad027

Browse files
committed
Enable proxy_cache_revalidate for both root and subdomains
* This unifies the proxy settings for root and the subdomains. * We don't need `proxy_cache_valid` because the node app will set `Cache-Control` headers for everything. * Set `Host` request header and `X-Cache` response header in all cases. * Most importantly, enable proxy_cache_revalidate, allowing nginx to perform conditional `GET` requests, passing the `Last-Modified` header.
1 parent 241c79f commit b5ad027

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

nginx/nginx.sh

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ GZIP="gzip on;
2323
LISTEN="listen 80;
2424
listen [::]:80;"
2525

26+
if [ "$SUPPORT_BYPASS" = "true" ]; then
27+
BYPASS="proxy_cache_bypass \$http_pragma;"
28+
fi
29+
30+
# Common proxy settings
31+
PROXY="proxy_pass http://app:8080;
32+
proxy_set_header Host \$host;
33+
proxy_cache cache;
34+
proxy_cache_revalidate on;
35+
$BYPASS
36+
add_header X-Cache \$upstream_cache_status;"
37+
2638
# Erase the conf file
2739
> $CONF_FILE
2840

@@ -53,49 +65,38 @@ EOF
5365
fi
5466
fi
5567

68+
CACHE_DIR="$DATA_DIR/nginx-cache"
69+
mkdir -p $CACHE_DIR
70+
5671
# Top domain server
5772
cat >> $CONF_FILE <<EOF
73+
proxy_cache_path $CACHE_DIR keys_zone=cache:${KEYS_SIZE}m levels=1:2 max_size=${MAX_SIZE}m;
74+
5875
server {
5976
$LISTEN
6077
$SERVER_NAME
6178
$SSL
6279
$GZIP
6380
6481
location / {
65-
proxy_pass http://app:8080;
66-
proxy_set_header Host \$host;
82+
$PROXY
6783
}
6884
}
6985
EOF
7086

7187
if [ -n "$DOMAIN" ] && [ "$SUBDOMAINS" = "true" ]; then
7288

73-
CACHE_DIR="$DATA_DIR/nginx-cache"
74-
mkdir -p $CACHE_DIR
75-
76-
if [ "$SUPPORT_BYPASS" = "true" ]; then
77-
BYPASS="proxy_cache_bypass \$http_pragma;"
78-
fi
79-
8089
# Subdomain server
8190
cat >> $CONF_FILE <<EOF
8291
83-
proxy_cache_path $CACHE_DIR keys_zone=cache:${KEYS_SIZE}m levels=1:2 max_size=${MAX_SIZE}m;
84-
8592
server {
8693
$LISTEN
8794
server_name *.${DOMAIN};
8895
$SSL
8996
$GZIP
90-
proxy_cache cache;
91-
proxy_cache_valid 301 365d;
92-
proxy_cache_valid any 1d;
93-
$BYPASS
9497
9598
location / {
96-
proxy_pass http://app:8080;
97-
proxy_set_header Host \$host;
98-
add_header X-Cache \$upstream_cache_status;
99+
$PROXY
99100
}
100101
}
101102
EOF

0 commit comments

Comments
 (0)