Skip to content

Instantly share code, notes, and snippets.

@nicolasrouanne
Last active June 19, 2020 13:11
Show Gist options
  • Save nicolasrouanne/182d2225b5fca19a224353d91121698d to your computer and use it in GitHub Desktop.
Save nicolasrouanne/182d2225b5fca19a224353d91121698d to your computer and use it in GitHub Desktop.
NGINX configuration for Mac OS X
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 8080;
server_name localhost;
root /var/www/MichelAnge;
index index.html index.html;
#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 your Mac.

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 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:

  1. create sites-available/default and paste the default file content (sourced from here)
  2. create sites-available/default-ssl and paste the default-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 ☕️

Running

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