lifesign
9/17/2014 - 7:24 AM

example-nginx.conf

# This script compiles nginx + echo module on Ubuntu
# Tested on Ubuntu 10.04 LTS (Lucid Lynx), 32 bit

# Resources:
#  - http://extralogical.net/articles/howto-compile-nginx-passenger.html
#  - http://wiki.nginx.org/InstallOptions
#  - http://wiki.nginx.org/CommandLine
#  - http://superuser.com/questions/336275/find-out-if-user-name-exists - check if the user exists

# Tips:
#  - See the actual configuration with: sudo nginx -V 

# Create the temp structure
mkdir -p /tmp/nginx-installation; cd /tmp/nginx-installation

# Download the latest stable naginx
wget http://nginx.org/download/nginx-1.2.2.tar.gz # Get the actual version links from: http://nginx.org/en/download.html
tar -xzvf nginx-1.2.2.tar.gz && rm -f ./nginx-1.2.2.tar.gz

# Download the HttpEchoModule
wget https://github.com/agentzh/echo-nginx-module/tarball/v0.40rc1 # Check the latest stable version on https://github.com/agentzh/echo-nginx-module/tags
tar -xzvf v0.40rc1 && rm -f ./v0.40rc1

# Download PCRE
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.20.tar.gz
tar -xzvf pcre-8.20.tar.gz && rm -f ./pcre-8.20.tar.gz

# Debian Install make
sudo apt-get install make

# Install nginx
cd nginx-1.2.2

# check nginx compilation flags
# $ sudo nginx -V

./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --with-pcre=../pcre-8.20 \
  --with-ipv6 \
  --without-mail_imap_module \
  --without-mail_pop3_module \
  --without-mail_smtp_module \
  --add-module=../agentzh-echo-nginx-module-9259898

make
sudo make install

# Create subdirectories
mkdir -p /var/tmp/nginx/client
# mkdir -p /var/tmp/nginx/proxy
# mkdir -p /var/tmp/nginx/fcgi

# Create group and add it user.
if id -u nginx >/dev/null 2>nginx; then
  echo "The nginx user exists"
else
  sudo groupadd nginx
  sudo useradd -r -g nginx nginx
fi

# install the init script
wget 'https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx' -O /etc/init.d/nginx
# Nested locations
# Resources:
# https://xintron.se/code/nginx-nested-locations-and-pagination
server {
  ...

  location / {
    root /path/to/media/cdn/;

    set $cache false;

    if ( $uri ~ ^/assets ) {
      set $cache true;
    }

    # if it's the latest player disable the cache-control header
    if ( $uri ~ ^/assets/player/latest ) {
      set $cache false;
    }

    # cache if needed for 3 days
    if ( $cache = true ) {
      # time precisions: http://wiki.nginx.org/ConfigurationSyntax
      expires 3d;
    }

    # Configure caching settings for ilovevideo static assets
    # Enable aggressive (positive) cache
    location ~ ^/assets/ilovevideo/(.*)$ {
      # enable aggressive (positive) cache
      expires max;

      # Configure custom headers for ilovevideo fonts
      location ~ \.(eot|svg|ttf|woff)$ {
        # Add additional header for font files
        # Without this header firefox serves a cross-origin issue
        add_header Access-Control-Allow-Origin *;
      }
    }
  }
}

# Pagination for static files example
# Resources:
# https://xintron.se/code/nginx-nested-locations-and-pagination
server {
  ...

  location / {

    # Make the clips available under the /clips/... path
    # Example:
    # http://domain.net/clip/format_x405/xx/xx/file-name.mp4
    location ~ ^/clip/(?<clip_path>.*)$ {
      root /path/to/origin/live/clips/live;

      try_files $uri /$clip_path =404;
    }
  }
}