Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Last active November 13, 2024 17:24
Show Gist options
  • Save netpoetica/5879685 to your computer and use it in GitHub Desktop.
Save netpoetica/5879685 to your computer and use it in GitHub Desktop.
Setting up Nginx on Your Local System

#Setting up Nginx on Your Local System ###by Keith Rosenberg

##Step 1 - Homebrew The first thing to do, if you're on a Mac, is to install homebrew from http://mxcl.github.io/homebrew/

The command to type into terminal to install homebrew is:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Following the installation of homebrew, type

brew cleanup

and then

brew doctor

Running brew doctor will inform you if you have any problems with your install or your machine in general. If you run into troubles, Stack Overflow is your best friend.

But I'm on Windows

I can't help you :'(

##Step 2 - nginx With homebrew installed, you can run (note: without sudo - do not run 'sudo brew' - it's evil)

brew install nginx

directly following install, run

sudo nginx

this will start nginx on port 8080. Open your web browser and go to http://localhost:8080

If this works, run

sudo nginx -s stop

to stop nginx.

##Step 3 - Set up nginx.conf Your nginx.conf file lives at

/usr/local/etc/nginx/nginx.conf

The absolute first thing you should do is make a backup copy of this file in case you royally destroy everything:

mv /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.bak && cp /usr/local/etc/nginx/nginx.conf.bak /usr/local/etc/nginx/nginx.conf

you now have a fresh copy of the nginx.conf file to bastardize, and a backup copy in case you get in too deep.

##Step 4 - Modularizing Nginx for Serving Multiple Local Websites The last thing you want to do is create a directory where you can store individual site nginx.conf files. At the bottom of your nginx.conf file, before your last closing bracket, you should include the following line:

http {
  # ... ...
  # ... ... nginx stuff
  # ... ...
  
  # include all server conf files
  include conf.d/*.conf;
}

Then, make a conf.d directory in your nginx folder:

mkdir /usr/local/etc/nginx/conf.d

and now whenever you create a new server, just place them into your conf.d directory:

vim /usr/local/etc/nginx/conf.d/myWebSite.conf

you can then put a specific server config into that file like so:

server {
    listen       8080;
    server_name  mywebsite.local.com;

    location / {
        root   /Users/myusername/Desktop/Projects/mywebsite/public/;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

Breaking Down a Server Configuration

####listen The port number you want nginx to listen on for this site

####server_name Points to a domain configured in your Mac OSX host file (note: be sure to edit with "sudo", as super user):

> sudo vim /private/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##

127.0.0.1 localhost

# mywebsite
127.0.0.1 mywebsite.local.com

####location Sets up a route and describes where it should point. The root sub-attribute should be a path from / (root) to your project on your local machine.

@sebastiancarlsson
Copy link

Thank you for this guide! However regarding step 4, there was already an empty servers/ folder in /usr/local/etc/nginx/ and when I opened my nginx.conf file this line was already at the bottom: include servers/*;. So this step was not required for me. Perhaps you want to update your guide to reflect this (new?) behaviour. Thanks again!

@leeyspaul
Copy link

Thanks for the guide!

@pedrobritto
Copy link

Awesome guide, thank you!

@rhoboat
Copy link

rhoboat commented Jan 27, 2017

With brew you don't need sudo nginx

@e-fundora
Copy link

Folks, I does any one have any idea of how to use nginx on windows 2012 r2 as a reverse proxy for kibana 5.2.2. I can't get the paths right no matter what I do. Here is an excerpt from the error log with my last configuration. I have been at this for 3 straight days trying out different approaches, but I just don't know enough.

2017/03/08 16:22:35 [error] 50976#50196: *382 "C:\nginx/html/app/kibana/index.html" is not found (3: The system cannot find the path specified), client: 10.16.9.61, server: umdatadeidapp2p.cgcent.miami.edu, request: "GET /app/kibana/ HTTP/1.1", host: "umdatadeidapp2p.cgcent.miami.edu"
2017/03/08 16:22:39 [error] 50976#50196: *382 "C:\nginx/html/app/kibana/index.html" is not found (3: The system cannot find the path specified), client:

@kheengz
Copy link

kheengz commented Mar 22, 2017

thanks

@kikicova
Copy link

A bit late to the game here but thanks for the guide! I want to reiterate @sebastiancarlsson's comment regarding the preexisting /servers directory located in /usr/local/etc/nginx/ (when installed with brew on macOS at least). I think using the /servers directory is a clearer description for what a user is trying to accomplish with additional server blocks in separate .conf files.

@nyxee
Copy link

nyxee commented Aug 24, 2017

nice complete guide.

@nyxee
Copy link

nyxee commented Aug 24, 2017

nice and clear here. I had ran into a case where i had like five nginx servers running in apps like xcode, the Apple Server was jijacking my pages when i tried localhost:XXXXX for whichever ports i tried, so this help. left with trying brew services start nginx

@nyxee
Copy link

nyxee commented Aug 24, 2017

nice and clear here. I had ran into a case where i had like five nginx servers running in apps like xcode, the Apple Server was hijacking my pages when i tried localhost:XXXXX for whichever ports i tried, so this help. left with trying brew services start nginx

@quincykwende
Copy link

Great; It works
I agree with @stuartsoorholtz and @sebastiancarlsson

@humancatfood
Copy link

humancatfood commented May 31, 2018

I can't get this to work :(

I followed the guide, but when I go to http://localhost:8080/ in the browser it keeps displaying the standard nginx greeting page.

How do I make my included .conf files take precedence?

@BaibhavVishal123
Copy link

@humancatfood I also could not. Here is the probable solution which worked for me.

  1. Every line should be ending with semicolon in .conf, in the configuration files.
  2. https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block . Root should be outside not inside.
    As for discussion regarding conf.d or server, it is just a logical separtor. Anyone is fine, just make sure you have the same name in nginx.conf

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