Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nginx configuration for rails on local machine?

Normally running rails app on localhost:3000 via rails s command on port 3000. For testing some specifics want to run it under a domain name on my OSX machine. I have nginx installed on my Mac and want to know how to configure it to use test.local to run localhost:3000

like image 593
Peter Van de Put Avatar asked Oct 27 '25 02:10

Peter Van de Put


1 Answers

  1. Install nginx from brew
brew install nginx
  1. Start nginx service
brew services start nginx
  1. Visit localhost:8080 to make sure nginx is running. You should see 'Welcome to nginx' page.

  2. Create a configuration for test.localhost default nginx config directory with brew install is /usr/local/etc/nginx/servers

cd /usr/local/etc/nginx/servers
nano test

and copy the following

server {
  listen 80;
  listen [::]:80;
  server_name test.localhost;
  root /<path to project>/public;
  access_log /<path to project>/log/nginx_access.log;
  error_log /<path to project>/log/nginx_error.log;
  location / {
    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_set_header X-NginX-Proxy true;
    proxy_pass http://tupstream;
  }
}

upstream tupstream {
  server 127.0.0.1:3000;
}

  1. Now we need to restart nginx with sudo since we will be serving from port 80.
sudo brew services restart nginx
  1. The next step is to make sure test.localhost points to 127.0.0.1 Install dnsmasq via brew
brew install dnsmaq
  1. Start the service with
sudo brew services start dnsmasq
  1. Copy dnsmasq configuration file
cp $(brew list dnsmasq | grep /dnsmasq.conf$) /usr/local/etc/dnsmasq.conf

## find the location of the file using
## brew list dnsmasq
## if the above doesn't work
  1. To make sure .localhost TLD points 127.0.0.1
echo 'address=/.localhost/127.0.0.1' >> /usr/local/etc/dnsmasq.conf
  1. Restart dnsmasq
sudo brew services restart dnsmasq
  1. Edit your DNS servers and make sure 127.0.0.1 is the first entry. Settings -> Network -> WiFi -> Advanced -> DNS

DNS server

Make sure you are able to ping .localhost sites

ping test.localhost
PING test.localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.062 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.066 ms
^C
--- test.localhost ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.062/0.064/0.066/0.002 ms

Since the DNS has changed, make sure you can access remote sites too (ping google.com)

  1. Start RoR server @ port 3000
rails s -p 3000 -b 0.0.0.0
  1. Take your browser and now enter test.localhost

enter image description here

like image 171
drtechie Avatar answered Oct 29 '25 17:10

drtechie