Skip to content

Instantly share code, notes, and snippets.

@jimothyGator
Last active March 21, 2024 10:08
Show Gist options
  • Save jimothyGator/5436538 to your computer and use it in GitHub Desktop.
Save jimothyGator/5436538 to your computer and use it in GitHub Desktop.
Nginx configuration for Mac OS X with Homebrew, using sites-enabled directory.
mkdir -p /usr/local/etc/nginx/sites-{enabled,available}
cd /usr/local/etc/nginx/sites-enabled
ln -s ../sites-available/default.conf
ln -s ../sites-available/default-ssl.conf

File locations:

  • nginx.conf to /usr/local/etc/nginx/
  • default.conf and default-ssl.conf to /usr/local/etc/nginx/sites-available
  • homebrew.mxcl.nginx.plist to /Library/LaunchDaemons/

Not documented yet:

  • How to create self-signed SSL certificates
  • How to start and stop Nginx
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 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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.nginx</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/nginx/sbin/nginx</string>
<string>-g</string>
<string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
#user nobody;
worker_processes 1;
error_log /Library/Logs/nginx/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /Library/Logs/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
index index.html index.php;
upstream www-upstream-pool{
server unix:/tmp/php-fpm.sock;
}
include /etc/nginx/conf.d/*.conf;
include /usr/local/etc/nginx/sites-enabled/*.conf;
}
@anthonybrown
Copy link

I can't seem to get nginx to load anything on port :8080
brew install nginx --with-passenger

followed brew's instructions but couldn't get localhost to work.
I have it running on my iMac but not on my MBP, is there some difference in configuration?

@wellington1993
Copy link

Thanks!

@andresnator
Copy link

Thanks

@iplus26
Copy link

iplus26 commented Jun 7, 2016

You saved my day!!!

Forgot to include /usr/local/etc/nginx/sites-enabled/*.conf;

@raulvillca
Copy link

include /etc/nginx/conf.d ??? i must make it, cause i didn't find it

@cr2121
Copy link

cr2121 commented Sep 20, 2016

You are right!

@craigiswayne
Copy link

Found this "create your own signed certificate for mac" tutorial
https://certsimple.com/blog/localhost-ssl-fix

@j0t3x
Copy link

j0t3x commented Dec 26, 2016

thanks dude!

@sivcan
Copy link

sivcan commented Jan 19, 2017

Thanks a lot dude!

@trainiac
Copy link

/usr/local/opt/nginx/sbin/nginx was /usr/local/opt/nginx/bin/nginx for me. Thanks!

@Oliboy50
Copy link

Oliboy50 commented Feb 1, 2017

👍 @trainiac

@nyxee
Copy link

nyxee commented Aug 25, 2017

people always fail to inform others to edit /private/etc/hosts and add entries for the servers.

for example:

 127.0.0.1 localhost.sec1
 127.0.0.1 mywebsite.local.com

@ihorvorotnov
Copy link

@nyxee or just let dnsmasq handle it

@efthemiosprime
Copy link

where can i find the /private/etc/hosts?

@agm1984
Copy link

agm1984 commented Oct 11, 2018

The hosts files is found at /etc/hosts in both Mac OS and Unix systems.

Typically, you can type

sudo nano /etc/hosts

and it will open the file, if you have nano installed. There is a good chance you have nano installed. Try it, if not, use your favourite text editor. Make sure you open the file with elevated privileges so you can save it. It is a system file.

As mentioned above, you can also use dnsmasq which auto forwards *.dev domains in your browser to localhost.

@tguduk42
Copy link

tguduk42 commented Dec 4, 2018

cd /usr/local/etc/nginx/sites-available
and you configured your setttings this command vi default.conf (or different editor)
finally you could use this command

sudo nginx -s stop && sudo nginx

@victorguerra95
Copy link

@Schizo
Copy link

Schizo commented Mar 1, 2020

Hi, very new to nginx, how should I lunch nginx after setting up the mentioned files? Thanks for this gist!

@ajithkranatunga
Copy link

ajithkranatunga commented Mar 24, 2021

I replaced

error_log  /Library/Logs/nginx/error.log;
access_log  /Library/Logs/nginx/access.log  main;

with

error_log  /usr/local/var/log/nginx/nginx_error.log;
access_log  /usr/local/var/log/nginx/nginx_access.log  main;

since I needed to have the logs in a centralized location. Btw, just make sure to set the proper access permission to the files when configuring the log files.

@ajithkranatunga
Copy link

Hi, very new to nginx, how should I lunch nginx after setting up the mentioned files? Thanks for this gist!

sudo nginx

@james0r
Copy link

james0r commented Apr 19, 2021

Be aware that your /Library/Logs is appeared to be destroyed and recreated during a Time Machine Backup and possibly other types of migrations.

Your log files created there for nginx will need to be recreated.

@daqarash
Copy link

people always fail to inform others to edit /private/etc/hosts and add entries for the servers.

for example:

 127.0.0.1 localhost.sec1
 127.0.0.1 mywebsite.local.com

I used mkcert for registering local domains. I thought that was enough. It never came to my mind to edit the host file.
I spent many hours figuring out why I couldn't serve the new local domain.

Thank you so much 🙏

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