Jack Moore

Email: jack(at)jmoore53.com
Project Updates

Nginx Subdirectory Configuration

09 Jul 2024 » code, infrastructure, docker

The following solution involves making modifications to a Dockerfile and adjusting URL settings to ensure the site is served correctly from different paths.

The goal was to configure an Nginx blog from a dockerfile to be testable and runnable from both the root directory and any subdirectory on the site. This flexibility allows for testing different versions of the site or hosting multiple versions simultaneously on different subfolders without interfering with each other.

How to Implement the Configuration

To achieve this, several changes were made to the Dockerfile and the configuration files:

  1. Dockerfile Setup: The Dockerfile was modified to build the site with a specified FULLBASEURL and SUBFOLDER argument. This setup allows for dynamically setting the base URL and the subdirectory where the site should be served. The Dockerfile is divided into two stages: the build stage and the final stage.

    Dockerfile:

    FROM ruby:3.3-alpine AS builder
    
    ARG FULLBASEURL="https://jmoore53.com"
    ARG SUBFOLDER=""
    ARG BUILT="Wed 03 Jul 2024 02:01:53 PM EDT"
    
    RUN apk update && apk add \
        git \
        build-base
    
    COPY ./ /app
    
    WORKDIR /app
    
    RUN rm Gemfile.lock
    
    RUN bundle install
    
    RUN sed -i "s|BASEURLHERE|$FULLBASEURL|" _config.yml
    RUN sed -i "s|BUILTDATE|$BUILT|" _config.yml
    RUN sed -i "s|SUBFOLDERHERE|$SUBFOLDER|" nginx/default.conf
    
    RUN bundle exec jekyll build -d /public
    
    FROM nginx:latest
    ARG FULLBASEURL="https://jmoore53.com"
    ARG SUBFOLDER=""
    RUN if [ ! -d "/usr/share/nginx/html/$SUBFOLDER" ]; then mkdir /usr/share/nginx/html/$SUBFOLDER ; fi
    COPY --from=builder /public/ /usr/share/nginx/html/$SUBFOLDER/
    COPY --from=builder /app/nginx/default.conf /etc/nginx/conf.d/default.conf
    RUN sed -i "s|SUBFOLDERHERE|$SUBFOLDER|" /etc/nginx/conf.d/default.conf
    COPY --from=builder /app/nginx/nginx.conf /etc/nginx/nginx.conf
    CMD ["nginx", "-g", "daemon off;"]
    

    This Dockerfile first builds the site using Ruby and Jekyll, then copies the built static files to the Nginx container. It also makes sure that URLs and paths in the configuration files are correctly set up based on the provided build arguments.

  2. Running the Blog: The blog can be run with specific arguments to test it from different subdirectories. This is done by passing FULLBASEURL and SUBFOLDER as build arguments when creating the Docker image.

    # Remove the current blog image
    docker image rm jmoore53-blog
    
    # Build Docker Image for testing
    docker build -t jmoore53-blog:testa --build-arg FULLBASEURL="http://10.100.0.20:8888/a" --build-arg SUBFOLDER="a" .
    

    These commands first remove any existing Docker image for the blog to prevent conflicts. Then, a new Docker image is built with the specified base URL and subfolder. This setup allows for testing the site under different conditions.

  3. Configuration Adjustments: After these changes to the Dockerfile, modifications were made to the config.yml file. The baseurl and url settings were updated, and static hrefs were converted to absolute links. These adjustments ensure that static resources are correctly referenced, regardless of whether the site is served from the root or a subdirectory.

For more details on the specific changes made to the repository, see the link to commit.

Conclusion

By modifying the Dockerfile and adjusting configuration settings, the Nginx blog can now be served from any specified subdirectory on the site. This setup provides flexibility in managing different versions of the site and ensures that all resources are correctly loaded regardless of the path from which the site is accessed.

© Jack Moore - This site was last built Fri 30 Aug 2024 12:31:24 PM EDT