NGinx

Nginx installation

brew install nginx

Check the help info after installation:

nginx -h
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : help
-v : version number
-V : version and configuration info
-t : test the configuration
-q : suppressing the none-error outputs when testing the configuration
-s signal : signal: stop, quit, reopen, reload
-p prefix : nginx path prefix (defaults: /usr/local/Cellar/nginx/1.8.0/)
-c filename : nginx configuration file path (defaults: /usr/local/etc/nginx/nginx.conf)
-g directives : global directives

Start nginx

sudo nginx

Verify at http://localhost:8080, nginx was started properly if you see the welcome information.

nginx use 8080 port by default, your can change the port number at /usr/local/etc/nginx/nginx.conf.

sudo chown root:wheel /usr/local/Cellar/nginx/1.8.0/bin/nginx (nginx's installation path)
sudo chmod u+s /usr/local/Cellar/nginx/1.8.0/bin/nginx

restart the server after editting the configurations,

nginx -t && nginx -s reopen

Start Nginx at system bootup

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

nginx configuration sample

user www www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
# http server settings
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# server settings
server {
listen 8080; #ports
server_name localhost; # server name
charset utf-8;
access_log logs/host.access.log main;
# location settings
location / {
root html;
index index.php index.shtml index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

misc

  1. multiple websites settings

    following the steps below to host multiple websites at nginx server:

    A. create two folders under /usr/local/etc/nginx/sites-available and sites-enabled

    B. create the file default.conf under site-available,copy and paste the default server settings from nginx.conf, and comment the raw content.

    C. add the following line in nginx.conf

    include /usr/local/etc/nginx/sites-enabled/*;

    D. create symlinks from site-available to site-enabled

    ln -sfv /usr/local/etc/nginx/sites-available/default.conf /usr/local/etc/nginx/sites-enabled/default.conf

    E. restart the nginx service

    # restart
    sudo nginx -s stop
    sudo nginx
    `
  2. change the default server root

    the default root path was located at:

    /usr/local/Cellar/nginx/1.2.3/html

    the 1.2.3 will be different according the version of your copy of nginx.

    You can change the server root path at nginx.conf:

    server {
    listen 80;
    server_name localhost;
    #access_log logs/host.access.log main;
    location / {
    root html;
    index index.html index.htm;
    }