feat: serve third-party libs locally

This commit is contained in:
MDLeom 2020-04-08 01:02:42 +01:00
parent 17c1ad06a0
commit 713a6ef730
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
14 changed files with 4 additions and 65 deletions

View File

@ -2,6 +2,7 @@
title: "Setup Caddy as a reverse proxy on NixOS (Part 3: Caddy)"
excerpt: "Part 3: Configure Caddy"
date: 2020-03-14
lastUpdated: 2020-04-08
tags:
- web
- linux
@ -217,7 +218,6 @@ If you prefer to redirect apex to www,
Aside from reverse proxy to curben.netlify.com, I also configured my Netlify website to use Statically CDN for on-the-fly image processing. My current [config](https://gitlab.com/curben/blog) is:
``` plain source/_redirects https://gitlab.com/curben/blog/-/blob/master/source/_redirects _redirects
/libs/* https://cdn.statically.io/libs/:splat 200
/img/* https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site/:splat 200
/screenshot/* https://cdn.statically.io/screenshot/mdleom.com/:splat?mobile=true 200
```
@ -225,10 +225,6 @@ Aside from reverse proxy to curben.netlify.com, I also configured my Netlify web
In Caddyfile, the config can be expressed as:
``` plain
proxy /libs https://cdn.statically.io/libs {
without /libs
}
proxy /img https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site {
without /img
}
@ -254,11 +250,6 @@ For `/screenshot`, since the `proxy` doesn't support variable like the Netlify `
To make sure Caddy sends the correct `Host:` header to the upstream/backend locations, I use `header_upstream` option,
``` plain
proxy /libs https://cdn.statically.io/libs {
without /libs
header_upstream Host cdn.statically.io
}
proxy /img https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site {
without /img
header_upstream Host cdn.statically.io
@ -287,10 +278,6 @@ There are a few repetitions for rewriting the header for Statically. I can group
}
mdleom.com {
proxy /libs ... {
import staticallyCfg
}
proxy /img ... {
import staticallyCfg
}
@ -322,10 +309,6 @@ To prevent any unnecessary request headers from being sent to the upstreams, I u
}
mdleom.com {
proxy /libs ... {
import removeHeaders
}
proxy /img ... {
import removeHeaders
}
@ -359,28 +342,7 @@ I also add the `Cache-Control` and `Referrer-Policy` to the response header. Use
### header and header_downstream
`/lib` location is used to grab javascript libraries from Statically CDN. Since the library is usually requested by a specific version, we can safely assume that the response would remain the same. This means I can set long expiration and `immutable` on the response. [`immutable`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Revalidation_and_reloading) is to tell the browser that revalidation is not needed.
```
proxy /libs ... {
header_downstream Cache-Control "public, max-age=31536000, immutable"
}
```
However, one issue I noticed is that `header` directive takes precedent over `header_downstream` option in `proxy` directive.
```
header / {
Cache-Control "max-age=604800, public"
}
proxy /libs ... {
# Doesn't work
header_downstream Cache-Control "public, max-age=31536000, immutable"
}
```
To solve that, I specify another `header` directive,
`/libs` folder contains third-party libraries. Since the library is usually requested by a specific version, we can safely assume that the response would remain the same. This means I can set long expiration and `immutable` on the response. [`immutable`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Revalidation_and_reloading) is to tell the browser that revalidation is not needed.
```
header / {
@ -390,10 +352,6 @@ To solve that, I specify another `header` directive,
header /libs {
Cache-Control "public, max-age=31536000, immutable"
}
proxy /libs ... {
# header_downstream no longer needed here
}
```
### Complete Caddyfile
@ -454,12 +412,6 @@ mdleom.com:4430 www.mdleom.com:4430 {
Cache-Control "public, max-age=31536000, immutable"
}
proxy /libs https://cdn.statically.io/libs {
without /libs
import removeHeaders
import staticallyCfg
}
proxy /img https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site {
without /img
import removeHeaders

View File

@ -249,12 +249,6 @@ ggucqf2jmtfxcw7us5sts3x7u2qljseocfzlhzebfpihkyvhcqfa.b32.i2p:8081 mdleom.i2p:808
Cache-Control "public, max-age=31536000, immutable"
}
proxy /libs https://cdn.statically.io/libs {
without /libs
import removeHeaders
import staticallyCfg
}
proxy /img https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site {
without /img
import removeHeaders

View File

@ -224,12 +224,6 @@ xw226dvxac7jzcpsf4xb64r4epr6o5hgn46dxlqk7gnjptakik6xnzqd.onion:8080 {
Cache-Control "public, max-age=31536000, immutable"
}
proxy /libs https://cdn.statically.io/libs {
without /libs
import removeHeaders
import staticallyCfg
}
proxy /img https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site {
without /img
import removeHeaders

View File

@ -1,4 +1,3 @@
# Reverse proxy to cdn
/libs/* https://cdn.statically.io/libs/:splat 200
/img/* https://cdn.statically.io/img/gitlab.com/curben/blog/raw/site/:splat 200
/screenshot/* https://cdn.statically.io/screenshot/mdleom.com/:splat?mobile=true 200

View File

@ -1,6 +1,6 @@
<% if (is_post()) { %>
<%- js([
{ src: '/libs/clipboard.js/2.0.6/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-x6nRSkfSsKGBsvlLFHHNju+buS3zYUztVnTRz/0JKgOIk3ulS6bNce/vHOvYE2eY', crossorigin: 'anonymous', defer: true },
{ src: '/libs/clipboard-2.0.6.min.js', defer: true },
'/js/copy-button.js'
]) %>
<% } %>

View File

@ -53,6 +53,6 @@
<%- css('/css/chameleon.css') %>
<% if (theme.icons) { %>
<%- css({ href: '/libs/fork-awesome/1.1.7/css/fork-awesome.min.css', 'data-sri-fallback': '/forkawesome/css/fork-awesome.min.css', integrity: 'sha384-mByhW6NjnxyShh67P9+fepUvYSd7Uz/qV6e2u4kA2Fi4ZkjXxIP2mRkyK9dwK24W', crossorigin: 'anonymous' }) %>
<%- css('/libs/forkawesome/css/forkawesome-1.1.7.min.css') %>
<% } %>
</head>

View File

Before

Width:  |  Height:  |  Size: 470 KiB

After

Width:  |  Height:  |  Size: 470 KiB