Skip to content

Commit f0007f9

Browse files
authored
Merge pull request #2595 from mroderick/feature/nginx-plausible-proxy-clean
feat: add nginx reverse proxy for Plausible analytics
2 parents 5c88388 + d15a4ce commit f0007f9

4 files changed

Lines changed: 87 additions & 5 deletions

File tree

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
release: bundle exec rake db:migrate
2-
web: bundle exec puma -C config/puma.rb
2+
web: bin/start-nginx bundle exec puma -C config/puma.rb

app/views/layouts/application.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
= csp_meta_tag
3232

3333
<!-- Privacy-friendly analytics by Plausible -->
34-
%script{ async: true, src: 'https://plausible.io/js/pa-PFruVsE_br97UUCRXE_6f.js' }
34+
%script{ async: true, src: '/js/script.js' }
3535
%script
3636
window.plausible = window.plausible || function() { (plausible.q = plausible.q || []).push(arguments) }, plausible.init = plausible.init || function(i) { plausible.o = i || {} };
37-
plausible.init()
37+
plausible.init({ endpoint: '/api/event' })
3838

3939
%body.no-js{ 'class': "#{params[:controller]}-#{params[:action]}", 'data-bs-no-jquery': 'true' }
4040
#top

config/nginx.conf.erb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
daemon off;
2+
worker_processes auto;
3+
4+
events {
5+
worker_connections 1024;
6+
}
7+
8+
http {
9+
charset utf-8;
10+
server_tokens off;
11+
12+
# DNS resolver for proxy_pass
13+
resolver 9.9.9.9 valid=30s;
14+
15+
# Proxy cache in /dev/shm (tmpfs, persists across dyno restarts)
16+
proxy_cache_path /dev/shm/nginx_cache levels=1:2 keys_zone=plausible_cache:1m max_size=100m inactive=5m use_temp_path=off;
17+
18+
# Logs to stdout/stderr for Heroku
19+
access_log /dev/stdout;
20+
error_log /dev/stderr;
21+
22+
# Proxy settings
23+
proxy_http_version 1.1;
24+
proxy_buffering on;
25+
26+
upstream app_server {
27+
server unix:/tmp/nginx.socket fail_timeout=0;
28+
}
29+
30+
server {
31+
listen <%= ENV["PORT"] %>;
32+
server_name _;
33+
keepalive_timeout 5;
34+
35+
# Plausible endpoints (set inside server context)
36+
set $plausible_script_url https://plausible.io/js/pa-PFruVsE_br97UUCRXE_6f.js;
37+
set $plausible_event_url https://plausible.io/api/event;
38+
39+
# Plausible: Proxy script.js (cached)
40+
location = /js/script.js {
41+
proxy_cache plausible_cache;
42+
proxy_cache_valid 200 5m;
43+
proxy_cache_key "$host$uri";
44+
proxy_pass $plausible_script_url;
45+
proxy_set_header Host plausible.io;
46+
proxy_buffering on;
47+
48+
# Cache response headers
49+
add_header X-Cache $upstream_cache_status;
50+
}
51+
52+
# Plausible: Proxy event API
53+
location = /api/event {
54+
proxy_pass $plausible_event_url;
55+
proxy_set_header Host plausible.io;
56+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
57+
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
58+
proxy_set_header X-Forwarded-Host $host;
59+
proxy_buffering on;
60+
}
61+
62+
# Rails: All other requests
63+
location / {
64+
proxy_pass http://app_server;
65+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
66+
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
67+
proxy_set_header Host $http_host;
68+
proxy_redirect off;
69+
}
70+
}
71+
}

config/puma.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# This configuration file will be evaluated by Puma. The top-level methods that
22
# are invoked here are part of Puma's configuration DSL. For more information
33
# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html.
4-
#
4+
5+
require 'fileutils'
6+
57
# Puma starts a configurable number of processes (workers) and each process
68
# serves each request in a thread from an internal thread pool.
79
#
@@ -29,7 +31,16 @@
2931
threads threads_count, threads_count
3032

3133
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
32-
port ENV.fetch("PORT", 3000)
34+
# Use Unix socket when nginx config exists (from heroku-community/nginx buildpack)
35+
# Falls back to port if nginx config not present
36+
if File.exist?("config/nginx.conf.erb")
37+
bind "unix:///tmp/nginx.socket?umask=0077" # Restrict socket permissions to owner only
38+
39+
# Signal to nginx buildpack that app is ready (required for nginx to start)
40+
FileUtils.touch("/tmp/app-initialized")
41+
else
42+
port ENV.fetch("PORT", 3000)
43+
end
3344

3445
# Allow puma to be restarted by `bin/rails restart` command.
3546
plugin :tmp_restart

0 commit comments

Comments
 (0)