Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Super-simple Nginx reverse proxy with Homebrew on OS X

Installation

1)

brew install nginx
sudo cp /usr/local/Cellar/nginx/1.8.0/homebrew.mxcl.nginx.plist /Library/LaunchAgents

2)

Replace /usr/local/etc/nginx/nginx.conf with the nginx.conf in this gist. I'm using port 5000 for my current project. Obviously, change server_name as well, and probably the name of its access log.

3)

sudo launchctl load /Library/LaunchAgents/homebrew.mxcl.nginx.plist

# Replace /usr/local/etc/nginx/nginx.conf with this. This is the
# default location for Nginx according to 'nginx -h'
worker_processes 1;
error_log /usr/local/var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
# This should be in the same directory as this conf
# e.g. /usr/local/etc/nginx
include mime.types;
default_type application/octet-stream;
# Note this log_format is named 'main', and is used with the access log below
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
# Without this I got this error: 'upstream sent too big header
# while reading response header from upstream'
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
server {
listen 80;
server_name dev.copypastafarianism.org;
access_log /usr/local/var/log/nginx/copypastafarianism.access.log main;
location / {
proxy_pass http://0.0.0.0:5000;
}
}
}
@bbss

This comment has been minimized.

Copy link

@bbss bbss commented Feb 27, 2016

Great! Thanks 👍

@kesarion

This comment has been minimized.

Copy link

@kesarion kesarion commented Jul 4, 2016

For those wondering why this doesn't immediately work, your domain needs to be in /etc/hosts as well, like:
127.0.0.1 dev.copypastafarianism.org

And you can use localhost instead of 0.0.0.0, but it should work regardless.

@yanjost

This comment has been minimized.

Copy link

@yanjost yanjost commented Dec 9, 2016

@kesarion Or use DNSMASQ

@lan3jur

This comment has been minimized.

Copy link

@lan3jur lan3jur commented Feb 27, 2017

I copied this exactly, yet I'm always getting 502: Bad Gateway.
Any ideas what I might be doing wrong?

error.log:
2017/02/27 21:21:26 [error] 12186#0: *1 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: dev.mydevdomain.tv, request: "GET /favicon.ico HTTP/1.1", upstream: "http://0.0.0.0:5000/favicon.ico", host: "dev.mydevdomain.tv", referrer: "http://dev.mydevdomain.tv/"

@pcmac77

This comment has been minimized.

Copy link

@pcmac77 pcmac77 commented Jan 27, 2018

@lan3jur, do you have anything running at localhost:5000? That proxy_pass is where nginx will proxy requests for http://localhost:80 to. As an example, set it to http://www.simplesite.com/, and assuming that you have
127.0.0.1 dev.copypastafarianism.org in your /etc/hosts, when you point your browser to http://dev.copypastafarianism.org, you'll see a page from http://www.simplesite.com/.

@ronvillalon

This comment has been minimized.

Copy link

@ronvillalon ronvillalon commented Mar 16, 2018

How would the https config look like for this?

@dioncodes

This comment has been minimized.

Copy link

@dioncodes dioncodes commented Dec 18, 2019

You should inclued proxy_set_header Host $host; in your location block to make apache-vhosts work.

@ale180192

This comment has been minimized.

Copy link

@ale180192 ale180192 commented Apr 1, 2020

only work running the server using sudo command, in case it serves to someone.

@ithustle

This comment has been minimized.

Copy link

@ithustle ithustle commented May 30, 2020

Marvelous!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment