Skip to content

Fixes #17 - configures Rails allowed hosts via env variables#39

Merged
kerchner merged 3 commits into
mainfrom
t17-config-hosts
Mar 4, 2026
Merged

Fixes #17 - configures Rails allowed hosts via env variables#39
kerchner merged 3 commits into
mainfrom
t17-config-hosts

Conversation

@kerchner

@kerchner kerchner commented Jan 7, 2026

Copy link
Copy Markdown
Member

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Adds two .env parameters, ALLOWED_HOSTS_REGEX and HOST_NAME; their values are appended to (initially empty) Rails.application.config.hosts so that Rails allows requests from the host and/or regex. Note that both may be populated if desired.

QA Instructions, Screenshots, Recordings

  • Do not populate either .env parameter. Confirm that you get a Rails error when trying to access the application
  • Populate just HOST_NAME with your host name (e.g. ec2-1-2-3-4.us-west-2.compute.amazon.com) and confirm that you can access the application
  • Populate just ALLOWED_HOSTS_REGEX with a regex that should include your host name (e.g. .*\.compute\.amazonaws\.com/) and confirm that you can access the application
  • Populate both and confirm that you can access the application.
  • If desired, try non-blank values that should (both, if using both variables) exclude your host name.

Added/updated tests?

  • Yes
  • No, and this is why: See comment below
  • I need help with writing tests

[optional] Are there any post deployment tasks we need to perform?

Populate at least one of these parameters in .env

@kerchner kerchner changed the title Fixes #17 Fixes #17 - configures Rails allowed hosts via env variables Jan 7, 2026
@kerchner

Copy link
Copy Markdown
Member Author

I initially wrote spec tests, but learned (the hard way) that within the Github CI environment, config.hosts as set in config/environments/test.db was being overridden (or cached before setting in test.db) with a set of values that included localhost, 0.0.0.0 etc. Adding a before(:each) to try to override this and grab from test.db didn't seem to work; hard-coding them would essentially reduce the value of the test to simply testing Rails functionality, because it wouldn't be testing the effect of setting our environment variables. So... it seemed better to just skip the test in this case.

@dolsysmith dolsysmith left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I realized that it's necessary to restart nginx, too, as well as Rails, when making these changes, I was able to get it working as expected.

Note that in your instructions and the example ENV, you refer to the variable as ALLOWED_HOSTS_REGEX, but it should be (per your code) ALLOWED_HOST_REGEX.

@kerchner

kerchner commented Mar 4, 2026

Copy link
Copy Markdown
Member Author

@dolsysmith Thanks for catching that - I opted for ALLOWED_HOSTS_REGEX and updated the code accordingly (example.env and instructions remain as-is)

@kerchner kerchner merged commit 8a39b56 into main Mar 4, 2026
1 check passed
@kerchner kerchner deleted the t17-config-hosts branch March 4, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants