Skip to content

Commit 74041d6

Browse files
committed
Switch nginx for varnish
1 parent ad23214 commit 74041d6

File tree

10 files changed

+79
-182
lines changed

10 files changed

+79
-182
lines changed

.github/workflows/deploy.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ jobs:
8787
docker compose build
8888
docker image ls
8989
docker save -o payload.tar vector-portfolio-payload:latest
90-
docker save -o nginx.tar vector-portfolio-nginx:latest
90+
docker save -o varnish.tar vector-portfolio-varnish:latest
9191
pigz payload.tar
92-
pigz nginx.tar
92+
pigz varnish.tar
9393
rsync -aqc payload.tar.gz ${{ secrets.ssh_connection }}:~/vectorapp/vector-portfolio/
94-
rsync -aqc nginx.tar.gz ${{ secrets.ssh_connection }}:~/vectorapp/vector-portfolio/
94+
rsync -aqc varnish.tar.gz ${{ secrets.ssh_connection }}:~/vectorapp/vector-portfolio/
9595
- name: Pull changes
9696
run: |
9797
ssh -i ~/.ssh/id_rsa ${{ secrets.ssh_connection }} "
9898
cd ~/vectorapp/vector-portfolio
9999
100-
docker rmi -f vector-portfolio-nginx:latest
100+
docker rmi -f vector-portfolio-varnish:latest
101101
docker rmi -f vector-portfolio-payload:latest
102102
103103
docker load -q -i payload.tar.gz
104-
docker load -q -i nginx.tar.gz
104+
docker load -q -i varnish.tar.gz
105105
rm payload.tar.gz
106-
rm nginx.tar.gz
106+
rm varnish.tar.gz
107107
108108
export DOMAIN="${{ vars.DOMAIN }}"
109109
export EMAIL_USER="${{ vars.EMAIL_USER }}"

docker-compose.dev.yml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ services:
88
path: .
99
target: /app
1010
ignore:
11-
- node_modules
12-
- nginx
13-
stdin_open: true
14-
tty: true
11+
- node_modules/
12+
- varnish/
1513
environment:
1614
- NEXT_PUBLIC_PAYLOAD_URL=http://${DOMAIN:?Website domain required}
1715
postgres:
@@ -22,17 +20,14 @@ services:
2220
- 5432:5432
2321
volumes: !override
2422
- pgdata_dev:/var/lib/postgresql/data
25-
nginx:
23+
varnish:
24+
ports:
25+
- 1001:80
2626
develop:
2727
watch:
28-
- action: sync+restart
29-
path: ./nginx/nginx.conf
30-
target: /etc/nginx/nginx.conf
31-
- action: rebuild
32-
path: ./nginx/templates
33-
target: /etc/nginx/templates/
34-
ports:
35-
- 1001:443
28+
- path: ./varnish
29+
action: sync+restart
30+
target: /etc/varnish/
3631

3732
volumes:
3833
pgdata_dev:

docker-compose.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ services:
2525
- POSTGRES_PASSWORD_FILE=/run/secrets/postgres-password
2626
secrets:
2727
- postgres-password
28-
nginx:
29-
image: vector-portfolio-nginx
30-
build:
31-
context: ./nginx
32-
args:
33-
ENABLED_MODULES: cachepurge
28+
varnish:
29+
image: vector-portfolio-varnish
30+
build: ./varnish
3431
restart: unless-stopped
3532
depends_on:
3633
- payload
3734
ports:
38-
- 127.0.0.1:1000:443
39-
environment:
40-
- NGINX_HOST=${DOMAIN:?Website domain required}
35+
- 127.0.0.1:1000:80
4136
volumes:
4237
pgdata:
4338
images:

nginx/Dockerfile

Lines changed: 0 additions & 74 deletions
This file was deleted.

nginx/modules/cachepurge/source

Lines changed: 0 additions & 1 deletion
This file was deleted.

nginx/nginx.conf

Lines changed: 0 additions & 11 deletions
This file was deleted.

nginx/templates/servers.conf.template

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/lib/utils/purge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use server'
22

3-
const purgePath = 'http://nginx:1810/purge'
3+
const purgePath = 'http://varnish'
44

55
/**Purges the given path from the nginx cache*/
66
export default async function purgeRoute(path: string) {

varnish/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM varnish:7.7.1-alpine
2+
3+
COPY default.vcl /etc/varnish/

varnish/default.vcl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
vcl 4.1;
2+
3+
include "hit-miss.vcl";
4+
5+
backend default {
6+
.host = "payload";
7+
.port = "3000";
8+
}
9+
10+
acl purge {
11+
"localhost";
12+
"payload";
13+
}
14+
15+
sub vcl_recv {
16+
// Don't cache admin or api requests (unless it's for media)
17+
if (req.url ~ "^\/admin.*$" || req.url ~ "^\/api\/(?!media).*$") {
18+
return(pass);
19+
}
20+
21+
// Pipe websocket to the backend to enable hmr during development
22+
if (req.http.upgrade ~ "(?i)websocket") {
23+
return (pipe);
24+
}
25+
26+
if (req.method == "PURGE") {
27+
if (!client.ip ~ purge) {
28+
return(synth(405, "Not allowed"));
29+
}
30+
return(purge);
31+
} elseif (req.method == "POST") {
32+
// Don't cache post requests
33+
return(pass);
34+
}
35+
36+
return(hash);
37+
}
38+
39+
sub vcl_hash {
40+
// Cache all /_next/ requests
41+
if (req.url ~ "^\/_next\/?.*$") {
42+
hash_data(req.http.host);
43+
hash_data(req.url);
44+
} else {
45+
// Replace params for all base urls
46+
hash_data(regsub(req.url, "\?.*", ""));
47+
}
48+
49+
return(lookup);
50+
}
51+
52+
sub vcl_pipe {
53+
if (req.http.upgrade) {
54+
set bereq.http.upgrade = req.http.upgrade;
55+
set bereq.http.connection = req.http.connection;
56+
}
57+
}

0 commit comments

Comments
 (0)