Set up nginx
on Mac OS
Install nginx
with Homebrew:
brew install nginx
Start nginx
as a brew service:
brew services start nginx
nginx
will now run in background and seemlessly start with Mac OS.
Head up to http://localhost:8080, you should see the default nginx
start page.
this content was greatly inspired by this gist
Create the sites-enabled
and sites-available
directories
mkdir -p /usr/local/etc/nginx/sites-{enabled,available}
Tell nginx
to use all servers defined in the sites-enabled
directory by adding this line to nginx.conf
http {
# a lot of default content set by nginx
...
# add the following line
include sites-enabled/*;
}
Create 2 default configurations, default
and default-ssl
:
sites-available/default
: paste thedefault
file contentsites-available/default-ssl
: paste thedefault-ssl
file content
⚠️ do not use the.conf
extension for server block files; for some reason when symlinking, nginx can't find the files.
Enable these configurations (optional):
cd /usr/local/etc/nginx
ln -s sites-enabled/ sites-available/{default, default-ssl}
Copy the default nginx settings into a new server block for the new site:
cp sites-available/default sites-available/my-new-site
Change the port for the new server block.
Make sure to choose a non-root port; root ports are ranging from 1 to 1024. It's a good practice since it will allow the server to run without using sudo
.
# in my-new-site
...
listen 8081;
...
Enable the new server by symlinking the conf in sites-available/
to sites-enabled/
.
This "symlink" convention is basically how all nginx
configuration works. Enable/disable servers at wish. It's pretty simple.:
ln -s sites-enabled/ sites-available/my-new-site
Head up to the new site http://localhost:8081, you should see nginx
starting page ☕️
Now you need to point the server to where the web content actually is. In the new server block, define the location of the root html file
server {
...
location / {
root /ABSOLUTE/PATH/TO/NEW_SITE/WEB_RESOURCES;
index index.html index.htm;
}
...
}
Now head up to http://localhost:8081, you should see the real site 🚀
Normally you don't need to restart nginx
when modifying a server block. However, if you do need to restart nginx:
brew services restart nginx
Check that nginx.conf
(and enabled server blocks, since they are included in the conf) are correctly configured:
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful