« Back to Index

nginx and ruby

View original Gist on GitHub

docker-compose.yml

frontend:
  image: ./front-end
  ports:
    - "8080:5000"

proxy:
  image: ./proxy
  ports:
    - "80:80"
  links:
    - frontend

front-end Dockerfile

FROM ruby
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile /app/
RUN bundle install --quiet
COPY . /app
EXPOSE 5000
ENTRYPOINT ["rackup"]
CMD ["--host", "0.0.0.0", "-p", "5000"]

front-end Gemfile

source "https://rubygems.org/"

gem "puma"
gem "sinatra"

front-end app.rb

require "sinatra/base"

class App < Sinatra::Base
  set :bind, "0.0.0.0"

  get "/" do
    "<p>This is <i>dynamic</i> content served via puma: #{rand(36**6).to_s(36)}"
  end
end

front-end config.ru

require "sinatra"
require "./app.rb"

run App

proxy Dockerfile

FROM nginx
COPY nginx.conf /etc/nginx/
COPY public /static
EXPOSE 80

proxy nginx.conf

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 5;

    gzip on;
    gzip_vary on;
    gzip_min_length 500;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_types text/plain text/css text/javascript;

    upstream app {
        # fail_timeout=0 means we always retry an upstream even if it failed
        # frontend is an alias set-up by Docker inside /etc/hosts
        server frontend:8080 fail_timeout=0;
    }

    server {
        listen 80;
        server_name localhost;

        access_log /static/var/log/nginx_access.log;
        error_log /static/var/log/nginx_error.log;

        root /static;

        # Attempt to serve files via nginx first
        # If match not found then pass request to upstream app (via @app location)
        try_files $uri/index.html $uri @app;

        location @app {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            # enable this if you forward HTTPS traffic to unicorn,
            # this helps Rack set the proper URL scheme for doing redirects:
            # proxy_set_header X-Forwarded-Proto $scheme;

            proxy_pass http://app;
        }

        error_page 500 502 503 504 /static/500.html;
        client_max_body_size 4G;
        keepalive_timeout 10;
    }
}

proxy public 500.html

<h1>500 Error Page!?!</h1>

proxy public index.html

<p>This is <i>static</i> content served via nginx: abc123</p>