Skip to content

Instantly share code, notes, and snippets.

@kml
Last active February 26, 2016 20:48
Show Gist options
  • Save kml/741b75f6de6be445e92a to your computer and use it in GitHub Desktop.
Save kml/741b75f6de6be445e92a to your computer and use it in GitHub Desktop.
Custom proxy cache server for assets speedup in *development* environment.
# Custom proxy cache server for assets speedup in *development* environment.
#
# Usage:
# Foreground (for tests):
# nginx -c $(pwd)/assets-nginx.conf -g 'daemon off; error_log stderr;'
#
# Background:
# nginx -c $(pwd)/assets-nginx.conf
# nginx -c $(pwd)/assets-nginx.conf -s <SIGNAL>
# signals: stop, quit, reopen, reload
#
# Cache cleanup:
# rm -rf /tmp/assets-nginx-cache
#
# Rails configuration:
# BEWARE: Configuration assumes thas that "rails server" runs at port 3000.
#
# To use this server it is required to configure app to use assets proxy:
# config.action_controller.asset_host = "http://<CUSTOM>.assets.vcap.me:3030"
# Everything before assets.vcap.me is custom cache key.
# Example url: "http://#{Rails.env}.assets.vcap.me:3030"
#
# Install:
# brew install nginx
# wget https://gist.githubusercontent.com/kml/741b75f6de6be445e92a/raw/assets-nginx.conf
#
# How it works:
# vcap.me and its subdomains resolves to 127.0.0.1
# $ ping -c 1 assets.vcap.me
# PING assets.vcap.me (127.0.0.1): 56 data bytes
#
# This config tells NGINX to listen on port 3030 on assets.vcap.me
# and subdomains of it (vhosts).
#
# Airplane mode:
# $ ping -c 1 assets.vcap.me
# ping: cannot resolve assets.vcap.me: Unknown host
#
# If not using many custom keys simplest solution may be adding *each*
# used subdomain to /etc/hosts (there is no support for wildcards).
# ...
# 127.0.0.1 <CUSTOM1>.assets.vcap.me
# 127.0.0.1 <CUSTOMN>.assets.vcap.me
# ...
# If many subdomains are in use it may be usefull to install
# local DNS server (for example dnsmasq).
#
# Source: https://gist.github.com/kml/741b75f6de6be445e92a
worker_processes 2;
#error_log /dev/null crit;
error_log /tmp/assets-nginx-error.log;
pid /tmp/assets-nginx.pid;
events {
}
http {
proxy_cache_path /tmp/assets-nginx-cache levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=1d use_temp_path=off;
server {
listen 127.0.0.1:3030 default_server;
server_name _;
return 444;
}
server {
listen 127.0.0.1:3030;
server_name .assets.vcap.me;
#access_log off;
#access_log /dev/stdout;
access_log /tmp/assets-nginx-access.log;
sendfile on;
server_tokens off;
location / {
return 200 "Hello Assets!";
}
location /ping {
return 200 "pong";
}
location /assets {
proxy_pass_header Server;
add_header Server "Ninja-Server";
add_header Access-Control-Allow-Origin *;
add_header X-Cache-Status $upstream_cache_status;
proxy_cache my_cache;
proxy_cache_key $host$scheme$proxy_host$request_uri;
proxy_cache_lock on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_403 http_404;
proxy_cache_valid 500 502 1s;
proxy_cache_valid any 1d;
proxy_ignore_headers Cache-Control;
proxy_set_header Accept-Encoding "";
proxy_pass http://127.0.0.1:3000;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment