Skip to content

Instantly share code, notes, and snippets.

@onekiloparsec
Last active August 18, 2022 20:12
Show Gist options
  • Save onekiloparsec/f9c9e3d6eef4439c9f5f3adc9412b7b5 to your computer and use it in GitHub Desktop.
Save onekiloparsec/f9c9e3d6eef4439c9f5f3adc9412b7b5 to your computer and use it in GitHub Desktop.
heroku-22 buildpack migration for a SPA from static to nginx
daemon off;
# Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections <%= ENV['NGINX_WORKER_CONNECTIONS'] || 1024 %>;
}
http {
gzip on;
gzip_comp_level 2;
gzip_min_length 512;
gzip_proxied any; # Heroku router sends Via header
server_tokens off;
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> l2met;
error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %>;
include mime.types;
default_type application/octet-stream;
sendfile on;
# Must read the body in 5 seconds.
client_body_timeout <%= ENV['NGINX_CLIENT_BODY_TIMEOUT'] || 5 %>;
server {
listen <%= ENV["PORT"] %>;
server_name _;
keepalive_timeout 5;
client_max_body_size <%= ENV['NGINX_CLIENT_MAX_BODY_SIZE'] || 1 %>M;
root dist/;
add_header "Cache-Control" "no-store, no-cache";
add_header "Access-Control-Allow-Origin" "<first origin>";
add_header "Access-Control-Allow-Origin" "<second origin>";
location / {
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
try_files $uri.html $uri $uri/ =404;
}
location ~ \.html$ {
try_files $uri =404;
}
location ~ ^/assets/.*$ {
add_header "Cache-Control" "public, max-age=512000";
}
location ~ ^/.*$ {
set $route /.*;
try_files $uri.html $uri $uri/ /index.html =404;
}
}
}
@onekiloparsec
Copy link
Author

For the record, after some trial and errors, here is my working config file when migrating a SPA served with the static buildpack using heroku-20 stack to the nginx buildpack on heroku-22 stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment