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 your Mac.
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
:
- create
sites-available/default
and paste thedefault
file content (sourced from here) - create
sites-available/default-ssl
and paste thedefault-ssl
file content (sourced from here)
Copy the default nginx settings into a new server block for your new site:
cd /usr/local/etc/nginx
cp sites-available/default.conf sites-available/my-new-site
⚠️ do not use the.conf
extension for
Change the port for your 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 your server to run without using sudo
.
# in my-new-site
...
listen 8080;
...
Enable your 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 your wish. It's pretty simple.
ln -s sites-enabled/ sites-available/my-new-site
Head up to your new site http://localhost:8080, you should see nginx
starting page ☕️
Now you need to point your server to where your web content actually is. In your new server block, define the location of your root html file
server {
...
location / {
root /ABSOLUTE/PATH/TO/YOUR/WEB_RESOURCES;
index index.html index.htm;
}
...
}