Skip to content

Instantly share code, notes, and snippets.

@nicolasrouanne
Last active June 19, 2020 13:30
Show Gist options
  • Save nicolasrouanne/45efbf8163e809b9d5bec173e53d484e to your computer and use it in GitHub Desktop.
Save nicolasrouanne/45efbf8163e809b9d5bec173e53d484e to your computer and use it in GitHub Desktop.
nginx on Mac OS
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 8081;
server_name localhost;
location / {
root /ABSOLUTE/PATH/TO/YOUR/WEB_RESOURCES;
index index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

NGINX

Set up nginx on Mac OS

Installation

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.

Screen Shot 2020-06-19 at 14 03 34

Configuration

this content was greatly inspired by this gist

Create sites-{enabled,available}

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 default configuration

Create 2 default configurations, default and default-ssl:

  1. sites-available/default: paste the default file content
  2. sites-available/default-ssl: paste the default-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}

Create a server block for the new site

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 ☕️

Wiring it up

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 🚀

Troubleshooting

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment