Dalius's blog

Thursday, January 30, 2025

Kamal: Nginx + redirect

Little tutorial how to make redirects with nginx.

Here is first result I have found how to redirect to www: https://github.com/basecamp/kamal/discussions/1214

While it is marked as unanswered it is completely answered IMHO. There is suggestion to do redirect at app level. With static sites I use nginx and here is example how to do redirect:

redirect.conf file:

server {
  return 301 https://atvirukas.ffff.lt$request_uri;
  server_name atvirukas.eu www.atvirukas.eu;
  listen 80;
  listen [::]:80;
}

Adapt this to your needs. Now you need to copy this file in Docker to /etc/nginx/conf.d/ like this.

Dockerfile file:

FROM nginx:alpine-slim AS production
COPY ./redirect.conf /etc/nginx/conf.d/redirect.conf
COPY . /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]