#
# This configuration is written for nginx built in a special way, when all 
# directories which are not absolute are relative to THE CURRENT DIRECTORY.
# Therefore, it is important where you are when you are launching nginx.
# This config assumes you stand in the root of adserver deployment directory.
#

# User to run workers as. Notice all access is performed by workers.

user  webuser;

# Number of worker proceses.

worker_processes  4;

# Error log, shared by all 

error_log   logs/nginx.error.log;

# File where nginx writes its process ID when it starts
# This file is deleted by nginx if nginx shut down  gracefully 

pid  logs/nginx.pid;

# number of simultaneous connections

events {
    worker_connections  8192;
}

http {
    
    log_format main   '$remote_addr $http_x_forwarded_for - $remote_user [$time_local] '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent"';

    # unlike default build, this is for nginx which takes all locations
    # as relative to the CURRENT DIRECTORY, UNLESS the location is absolute.
    # Therefore we shall specify config/mime.types.
    # All the same applies to "access_log", "root" and other directives
    # below if they specify locations in the filesystem (not Web locations).

    include            conf/nginx.mime.types;
    
    default_type       application/octet-stream;

    access_log         logs/nginx.access.log main;

    # place temporary directories nginx wants
    proxy_temp_path        webdata/temp/proxy;
    fastcgi_temp_path      webdata/temp/fastcgi;
    client_body_temp_path  webdata/temp/client_body;
    
    ############ FASTCGI TUNING PARAMS #####################
    fastcgi_connect_timeout           60;
    fastcgi_send_timeout             180;
    fastcgi_read_timeout             180;
    fastcgi_buffer_size             128k;
    fastcgi_buffers               4 256k;
    fastcgi_busy_buffers_size       256k;
    fastcgi_temp_file_write_size    256k;
    fastcgi_intercept_errors          on;
    
    # some tuning        
    sendfile           on;
    tcp_nopush         on;
    tcp_nodelay        on;
    gzip               off;

    # this prevents server version to be reported in headers and other places
    server_tokens      off;

    keepalive_timeout  0;

    upstream backend {
        server 127.0.0.1:10000;
        keepalive 5;
    }

    # define fastcgi backed via cluster even if one
    # process

    #============================================================

    server {

        listen       10001;
        server_name  main;
        root         webdata/root;

        # generic setup of nginx on this port

        location / {

            index  index.html index.htm;

            limit_except GET {
                deny all;
            } 
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root    webdata/root;
        }

        # internal hidden directory used by beacons
        # this is not accessible by users, only via
        # internal redirects done by fastcgi adserver

        location /i/ {
            internal;
            alias     webdata/hidden/;
        }

        # internal redirect for static page
       
        location = /status.html {
            limit_except GET {
                deny all;
            }
            include        conf/fastcgi_common.conf;
            fastcgi_param  BRANDNET_INTERNAL    /i/checkstatus.html;
            fastcgi_pass   localhost:10000;
        }

        #
        # This sets up beacon serving from root
        # location via /1x1.gif
        #
        
        location = /1x1.gif {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            include       conf/fastcgi_beacon.conf;
            #fastcgi_pass  localhost:10000;
            fastcgi_pass backend;
            fastcgi_keep_conn on;
        }

        #
        # This is for a 20k image that Yahoo! wants for 3rd-party ad certification
        #

        location = /adc_buzzed_drinks_300x250.jpg {
            limit_except GET {
                deny all;
            }
        }

        #
        # The path sends a "OPT OUT" command to adserver.
        # Adserver will then create a cookie that tells the
        # adserver not to store any user data for subsequent
        # requests.
        #
        
        location = /optout {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            fastcgi_param BRANDNET_OPTOUT true;
            fastcgi_pass  localhost:10000;
        }


        #
        # This sets up beacon serving from another logical
        # location "/b/1x1.gif", you can add any other
        # locations like shown below, it does not require
        # any modifications to the adserver
        #

        location = /b/1x1.gif {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            include       conf/fastcgi_beacon.conf;
            fastcgi_pass  localhost:10000;
        }

        #
        # Defines location from which ad redirects happen.
        # 

        location = /charon/adrequest {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            #fastcgi_pass  localhost:10000;
            fastcgi_pass  backend;
            fastcgi_keep_conn on;
        }

        #
        # Defines location from which our ad script is served.
        #

        location = /scripts/ads.js {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            fastcgi_param BRANDNET_SCRIPT true;
            fastcgi_pass  localhost:10000;
        }

        #
        # Real-time bidder location 
        #    - AdX is /r/gbid, 
        #    - others could be /r/foo, etc.
        #

        location ^~ /r {
            include       conf/fastcgi_common.conf;
            fastcgi_pass  localhost:10000;
        }

        # Appnexus configuration
        # - ready is just to let appnexus know we're ready.
        # - bid is for bid requests
        # - notify is for notify requests

        location = /ready {
            limit_except GET {
                deny all;
            }
            include        conf/fastcgi_common.conf;
            fastcgi_param  BRANDNET_APN_READY true;
            fastcgi_pass   localhost:10000;
        }

        location = /bid {
            include        conf/fastcgi_common.conf;
            fastcgi_param  BRANDNET_APN_BID true;
            fastcgi_pass   localhost:10000;
        }

        location = /notify {
            include        conf/fastcgi_common.conf;
            fastcgi_param  BRANDNET_APN_NOTIFY true;
            fastcgi_pass   localhost:10000;
        }

        location = /dp/get_cat {
           include        conf/fastcgi_common.conf;
           fastcgi_param  BRANDNET_APN_DATA true;
           fastcgi_pass   localhost:10000;
        }

        # Adap.tv configuration
        # - adptvbid is for bid requests

        location = /atvbid {
           include        conf/fastcgi_common.conf;
           fastcgi_param  BRANDNET_ADAPTV_BID true;
           fastcgi_pass   localhost:10000;
        }

        #
        # This is just for Exelate right now
        #
        location = /s2s {
           include        conf/fastcgi_common.conf;
           fastcgi_param  BRANDNET_EXELATE_PIXEL true;
           fastcgi_pass   localhost:10000;
        }


        #
        # another ad serving location, just as a demo but it
        # can be used -- because adserver is not aware of all
        # this (i.e. not aware of logical location), so you
        # create any number of any logical locations as shown below
        #

        location = /a {
            limit_except GET {
                deny all;
            }
            include       conf/fastcgi_common.conf;
            fastcgi_pass  localhost:10000;
        }
    }

}
