Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active September 28, 2023 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/709c8c97ef37284146c7e36815c1c254 to your computer and use it in GitHub Desktop.
Save ericboehs/709c8c97ef37284146c7e36815c1c254 to your computer and use it in GitHub Desktop.
Testing nginx locally with Rails
# frozen_string_literal: true
# This file is used by Rack-based servers to start the application.
require_relative 'config/environment'
map Rails.application.config.relative_url_root || '/' do
run Rails.application
Rails.application.load_server
end
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /atlas {
proxy_set_header Host $host:8082;
proxy_set_header X-Forwarded-Host localhost:3000;
proxy_pass http://host.docker.internal:3000/atlas;
}
}
include /etc/nginx/conf.d/*.conf;
}
@ericboehs
Copy link
Author

ericboehs commented Sep 28, 2023

I was working on a little Rails project called Atlas and we wanted to mount it at https://our.domain.com/atlas via our nginx rev proxy.

Rails had some interesting issues with being mounted at a subdirectory. To test this out, I temporarily added an nginx.conf file to my Rails root and ran:

docker run -p 8082:80 --network=bridge -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf nginx

For local testing, I ran:

RAILS_SERVE_STATIC_FILES=true RAILS_RELATIVE_URL_ROOT=/atlas rails s

In prod, make sure you set RAILS_SERVE_STATIC_FILES=true and RAILS_RELATIVE_URL_ROOT=/atlas in your ENV.

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