From 42a44f710643d9240115a556728f9ef21e881e0c Mon Sep 17 00:00:00 2001 From: Kenneth Kalmer Date: Fri, 18 Nov 2022 10:45:26 +0000 Subject: [PATCH] feat: basic support in nginx for supporting redirects We can generate a list of redirects in `config/nginx-redirects.conf` during build and nginx will use this to perform server-side redirects for us --- .gitignore | 3 ++- config/.tool-versions | 1 + config/nginx-redirects.conf.example | 16 ++++++++++++++++ config/nginx.conf.erb | 21 ++++++++++++++++++--- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 config/.tool-versions create mode 100644 config/nginx-redirects.conf.example diff --git a/.gitignore b/.gitignore index f361e77263..1d9be3350e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ public !.env.example graphql-types.ts cli -dist \ No newline at end of file +dist +config/nginx-redirects.conf diff --git a/config/.tool-versions b/config/.tool-versions new file mode 100644 index 0000000000..2329e81a1e --- /dev/null +++ b/config/.tool-versions @@ -0,0 +1 @@ +nodejs 16.18.1 diff --git a/config/nginx-redirects.conf.example b/config/nginx-redirects.conf.example new file mode 100644 index 0000000000..733350ce4f --- /dev/null +++ b/config/nginx-redirects.conf.example @@ -0,0 +1,16 @@ +# This is an example of how the nginx redirects should look like. +# +# When building the site, part of the output should be this file, +# populated with the data from the redirect metadata from the pages +# +# Nginx has a limit on how big the redirects-map.conf can be, which is controlled +# via map_hash_bucket_size variable. If you get the following error: +# +# [emerg]: could not build the map_hash, +# +# you should increase the map_hash_bucket_size to account for the filesize. Say your +# nginx-redirects.conf is 30Kb, you set the map_hash_bucket_size variable in the http +# block of nginx.conf to 30720; + +# Redirect from /old_page to /new_page +/old_page /new_page; diff --git a/config/nginx.conf.erb b/config/nginx.conf.erb index cb6fd808d6..2fc32f46c0 100644 --- a/config/nginx.conf.erb +++ b/config/nginx.conf.erb @@ -22,8 +22,7 @@ http { 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' %>; - + error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %> error; include mime.types; default_type application/octet-stream; @@ -32,6 +31,17 @@ http { # Must read the body in 5 seconds. client_body_timeout <%= ENV['NGINX_CLIENT_BODY_TIMEOUT'] || 5 %>; + map_hash_max_size 8192; + map_hash_bucket_size 8192; + + # Creates a map of redirects for us + map $uri $redirected_url { + default "none"; + <% if File.exists?(File.dirname(__FILE__) + '/nginx-redirects.conf') %> + include nginx-redirects.conf; + <% end %> + } + server { listen <%= ENV["PORT"] %> reuseport; server_name _; @@ -48,6 +58,11 @@ http { return 301 https://$host$request_uri; } - root /app/output; # path to your app + # Apply our redirects + if ($redirected_url != "none") { + rewrite ^ $redirected_url permanent; + } + + root /app/public; # path to your app } }