Installation Image Proxy mit GO
Installation eines Image Proxies für die schnellere Auslieferung von externen Bildern. Der lokale Cache löst auch das Problem von mixed Content z.B. bei Foren.
Als erstes die Programmiersprache go installieren.
Installation
pkg install go
Nun Paket installieren
go get willnorris.com/go/imageproxy/cmd/imageproxy
Jetzt noch den $GOPATH exportieren
vi ~/.bashrc
und diese beiden Zeilen hinzufügen
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
damit der PATH auch gleich verfügbar ist, diese beiden Zeilen danach auch noch kurz im Shell ausführen.
Startup Script
cd /usr/local/etc/rc.d
Hier neues File erstellen mit folgendem Inhalt:
#!/bin/sh
# PROVIDE: imageproxy
#
# Füge folgende Lines zu /etc/rc.conf hinzu um imageproxy zu aktivieren:
#
# imageproxy_enable (bool): Default ist "NO".
# Setze es auf "YES" um imageproxy zu aktivieren
# imageproxy_flags (str): Default ist "".
# Hier kannst du extra Flags hinzufügen, z.B. -whitelist etc.
#
. /etc/rc.subr
name="imageproxy"
rcvar=`set_rcvar`
pidfile=/var/run/${name}.pid
start_cmd="imageproxy_start"
stop_cmd="imageproxy_stop"
load_rc_config $name
imageproxy_start()
{
if checkyesno ${rcvar}; then
echo "Starting Imageproxy..."
nohup imageproxy $imageproxy_flags 2>/dev/null &
sleep 2
pgrep imageproxy > $pidfile;
echo "Started"
fi
}
imageproxy_stop()
{
echo "Stopping Imageproxy..."
if [ -f $pidfile ]; then
PID=`cat $pidfile`
echo "pid: $PID"
kill $PID
rm -f $pidfile
else
printf "%s\n" "pidfile not found"
fi
}
run_rc_command "$1"
Jetzt noch das Script ausführbar machen:
chmod 555 imageproxy
Und jetzt noch im /etc/rc.conf aktivieren:
vi /etc/rc.conf
imageproxy_enable="YES"
imageproxy_flags="-referrers mydomain.com, *.mydomain.com"
Soooo… nun kann man den Imageproxy wie folgt starten:
/usr/local/etc/rc.d/imageproxy start
Konfiguration
Damit nur berechtigte Domains den Proxy nutzen können, kann man Referrer Domains whitelisten.
Alle Konfigurations- / Startup Options findest du hier.
imageproxy -referrers example.com, test.com, *.domain.com
Der Proxy läuft auf Port 8080
Nginx Config
Jetzt einfach noch den Cache im Nginx Server Bereich eintragen
location ~ /cache/(.*) {
proxy_pass http://127.0.0.1:8080/$1?$args;
}
Nginx neu starten, danach testen:
http://www.mydomain.com/cache/200/https://willnorris.com/logo.jpg
.