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
brew install nginx
brew services start nginx
Visit localhost:8080 to make sure nginx is running. You should see 'Welcome to nginx' page.
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;
}
sudo since we will be serving from port 80.sudo brew services restart nginx
test.localhost points to 127.0.0.1
Install dnsmasq via brewbrew install dnsmaq
sudo brew services start dnsmasq
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
echo 'address=/.localhost/127.0.0.1' >> /usr/local/etc/dnsmasq.conf
sudo brew services restart dnsmasq

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)
rails s -p 3000 -b 0.0.0.0
test.localhost
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With