Homebrew’s Nginx is a surprisingly flexible web server, not just a static file host.

Let’s get it running.

First, make sure Homebrew is up to date:

brew update

Then, install Nginx:

brew install nginx

This downloads and installs the latest stable version of Nginx and its dependencies.

Now, to start Nginx:

brew services start nginx

This command registers Nginx as a background service managed by Homebrew. It will automatically start when you log in and stop when you log out.

You can check its status:

brew services list

You should see nginx with started next to it.

By default, Nginx serves files from /usr/local/var/www (or /opt/homebrew/var/www on Apple Silicon Macs). Create an index.html file there to test:

echo "<h1>Hello from Homebrew Nginx!</h1>" > /usr/local/var/www/index.html

(On Apple Silicon: echo "<h1>Hello from Homebrew Nginx!</h1>" > /opt/homebrew/var/www/index.html)

Now, open your web browser and go to http://localhost:8080. You should see your "Hello" message.

The port is 8080 by default because Homebrew’s Nginx is configured to avoid conflicts with any other web server that might be running on port 80.

The main configuration file is located at /usr/local/etc/nginx/nginx.conf (or /opt/homebrew/etc/nginx/nginx.conf on Apple Silicon). You can edit this file to change settings like the port, root directory, or add virtual hosts.

For example, to change the port to 80, you’d edit the listen directive within the server block in the nginx.conf file. Find this line:

listen 8080;

And change it to:

listen 80;

After saving the change, you need to reload Nginx for the configuration to take effect:

brew services reload nginx

If you want to stop Nginx entirely:

brew services stop nginx

And to uninstall it:

brew uninstall nginx

The most surprising thing about Homebrew’s Nginx is how its configuration is designed to be accessible and modifiable for local development without requiring root privileges or complex system-wide installations. The brew services command abstracts away the complexities of daemon management, making it incredibly easy to start, stop, and reload your web server.

Inside the nginx.conf file, you’ll find directives that define the behavior of your server. The http block contains global HTTP settings, and within that, server blocks define individual virtual hosts. The location block within a server block specifies how to handle requests for different URL paths. For instance, location / applies to all requests not matched by more specific location blocks.

Consider a common scenario: serving a static site from a different directory. You’d modify the root directive within the relevant server block. If your site files are in ~/my-static-site, you’d change:

root /usr/local/var/www;

to:

root ~/my-static-site;

Then, reload: brew services reload nginx. This allows you to quickly test local projects without altering system-wide web server configurations.

A detail that often trips people up is how Homebrew manages nginx.conf. It’s a symlink managed by Homebrew. If you brew install nginx and then brew upgrade nginx, Homebrew might overwrite your custom nginx.conf if you’ve made direct edits in the Homebrew-managed location. The best practice is to symlink your custom configuration into the Homebrew directory, or use include directives within the main nginx.conf to pull in your own configuration files. For example, in nginx.conf, you could add:

http {
    # ... other http settings ...
    include /usr/local/etc/nginx/sites-enabled/*;
}

And then create a symlink from your custom config to /usr/local/etc/nginx/sites-enabled/my-site.conf.

The next step is often delving into advanced Nginx features like reverse proxying or SSL configuration.

Want structured learning?

Take the full Homebrew course →