Our domain is foobar.com
so the one we want for local development is dev.foobar.com
.
We need to make sure that our browser will redirect all the requests to the local machine instead of asking a DNS.
sudo vim /etc/hosts
Then we need to add our dns entry at the end. π§
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
0.0.0.0 dev.foobar.com
After pointing the domain to local machine we need to setup a local web server that will proxy all the requests to our domain to our app. We are going to nginx for this.
Assuming we already have homebrew installed, simply run:
brew install nginx
Now go to our projects' directory and create a directory to host all our configuration files.
cd /Users/USER_NAME/PROJECTS/PATH
mkdir nginx_local_confs
And then we create the configuration file in that directory.
cd nginx_local_confs
vim foobar.com.conf
Our local app is running on port 3000
so the configuration will be the following. π§
# nginx local configuration
upstream awesomeAppServer {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name dev.foobar.com;
location / {
proxy_pass http://awesomeAppServer;
}
}
Next we need to edit our server main configuration. We assume that homebrew installed nginx in /usr/local
.
By convention UNIX configuration files are located in /etc/
, /usr/etc/
, /usr/local/etc/
and so on.
sudo vim /usr/local/etc/nginx/nginx.conf
And include the individual configuration files for our apps. π§
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include servers/*;
include /Users/USER_NAME/PROJECTS/PATH/nginx_local_confs/*.conf;
}
Finally we need to start our server:
sudo nginx
Done! π