Skip to content
bibleforge edited this page May 15, 2013 · 10 revisions
# Set another default user than root for security reasons
# This can be removed or changed as necessary.
user       www www;

# As a thumb rule: One per CPU. If you are serving a large amount
# of static files, which requires blocking disk reads, you may want
# to increase this from the number of cpu_cores available on your
# system.
#
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes  4;

# Change these paths to somewhere that suits you!
error_log  /var/log/nginx/error.log  crit;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# Change these paths to somewhere that suits you!
pid        /var/log/nginx/logs/nginx.pid;

# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 8192;

events {
    # When you need > 8000 * cpu_cores connections, you start optimizing
    # your OS, and this is probably the point at where you hire people
    # who are smarter than you, this is *a lot* of requests.
    worker_connections  8000;
}


http {
    # Set the mime-types via the mime.types external file
    include       mime.types;
    # And the fallback mime-type
    default_type  application/octet-stream;
    
    # Format for our log files
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    # Click tracking!
    #access_log  logs/access.log  main;
    
    # You usually want to serve static files with Nginx
    sendfile        on;
    
    tcp_nopush on; # off may be better for Comet/long-poll stuff
    tcp_nodelay off; # on may be better for Comet/long-poll stuff
    
    # ~2 seconds is often enough for HTML/CSS, but connections in
    # Nginx are cheap, so generally it's safe to increase it
    keepalive_timeout  100;
    
    # Enable Gzip:
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_min_length 512;
    gzip_buffers 4 8k;
    gzip_proxied any;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        # application/font-woff fonts are already compressed.
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/rss+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;
    
    # This should be turned on if you are going to have pre-compressed copies (.gz) of
    # static files available. If not it should be left off as it will cause extra I/O
    # for the check. It would be better to enable this in a location {} block for
    # a specific directory:
    # gzip_static on;
    
    gzip_disable        "MSIE [1-6]\.";
    gzip_vary           on;
    
    upstream nodejs {
        # NOTE: Adding more servers creates a round robin.
        server 127.0.0.1:7777;
    }
    
    # Redirect www subdomains to the root.
    # THIS DOES NOT APPEAR TO WORK IN THE LATEST NGINX.
    # server {
    #      server_name www.bibleforge.com;
    #      rewrite ^(.*) http://bibleforge.com$1 permanent;
    #}
    
    server {
        # You can change the port if you want, but if you change it to port 80, you will have to run Nginx as root.
        listen       7070;
        # e.g. "localhost" to accept all connections, or "www.example.com"
        # to handle the requests for "example.com" (and www.example.com)
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;

        # Change this to the path where you downloaded BibleForge.
        # NOTE: Make sure that it ends with "/client;"
        root   /var/www/BibleForge/client;
        index  index.html;
        
        # Add a header to make IE use ChromeFrame and the latest engine.
        add_header "X-UA-Compatible" "IE=Edge,chrome=1";
        
        # Make sure it is UTF8.
        charset utf-8;
        
        # Make sure all URLs with _escaped_fragment_ are sent to the Node.js server to be pre-built.
        # NOTE: Googlebot converts #!... to ?_escaped_fragment_=....
        if ($args ~ _escaped_fragment_) {
            rewrite ^ /api;
        }
        
        # Send all requests ending with an exclamation point (!) to the Node.js server to be pre-built.
        if ($request_filename ~ !$) {
            rewrite ^ /api;
        }
        
        # All queries not to the api or to an existing file should be redirected to the default page.
        # E.g., a requst for /en/Romans/ will be sent to index.html.
        # NOTE: The equals sign followed by a number changes the status code from 404 to that number.
        # NOTE: A equals sign without a number allows the script to send the proper status code (i.e., 200 or 301).
        error_page  404 =200 /;
        
        # Redirect all bots to the non-JS version.
        location / {
            # Bounce back the accept-language header to detect the user's language.
            add_header "X-client-accept-lang" $http_accept_language;
            
            if ($http_user_agent ~* google(?:bot|\/)|yahoo\!|bingbot|baiduspider|iaskspider|ia_archiver|yandex) {
                rewrite ^ /api;
            }
        }
        
        location /api {
            # Send the original URI before any modifications.
            # This is needed because error_page changes the URI.
            proxy_set_header X-Request-URI   $request_uri;
            # Set client's IP address
            proxy_set_header X-Real-IP       $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # Set original host name
            proxy_set_header Host            $host;
            # Set original port
            proxy_set_header Port            $server_port;
            
            # Redirect to the Node.js server(s).
            # NOTE: This cannot end with a trailing slash.
            proxy_pass http://nodejs;
            proxy_redirect off;
        }
        
        location ~* \.(?:jpg|gif|png|ico|svg|svgz|mp4|ogg|ogv|webm|webp|css|js)$ {
            # Since we are using cache busting URLs, cache files for a long time (3 months).
            expires 3M;
            access_log off;
            # Make Firefox cache static SSL content on disk.
            add_header Cache-Control "public";
        }

        # Cross domain webfont access
        location ~* \.woff$ {
            add_header "Access-Control-Allow-Origin" "*";

            # Also, set cache rules for webfonts.
            expires 3M;
            add_header Cache-Control "public";
        }
    }
}