##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

#proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=assets:768m use_temp_path=off;

#add_header X-Cache-Status $upstream_cache_status;
#add_header X-Runtime 42;
more_clear_headers 'X-Runtime';
more_clear_headers 'X-Powered-By';
more_clear_headers 'Server';
server_tokens off;

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

log_format proxy '[$time_local] Cache: $upstream_cache_status $upstream_addr $upstream_response_time $status $bytes_sent $proxy_add_x_forwarded_for $request_uri "$http_referer" "$http_user_agent"';

#passenger_show_version_in_header off;

#log_format proxy '[$time_local] Cache: $upstream_cache_status $upstream_addr $upstream_response_time $status $bytes_sent $proxy_add_x_forwarded_for $request_uri "$http_referer" "$http_user_agent"';

# Default server configuration
#
#passenger_log_level 5;

# Performance
passenger_max_pool_size 8;
passenger_max_request_queue_size 200;

#limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;

# Frontend caching and static asset delivery
server {
    listen 85.25.83.22:80 default_server;
    listen 85.25.83.22:443 ssl;
    ssl_certificate     ssl/screenshots.debian.net.crt;
    ssl_certificate_key ssl/screenshots.debian.net.key;

	root /home/debshots/debshots/public;
	server_name _;
    access_log /var/log/nginx/cache-access.log proxy;

    add_header X-Cache-Status $upstream_cache_status;

    location / {
        try_files      /maintenance.html @backend;
    }

    location /json/package/0ad {
        #try_files      /maintenance.html @backend;

        proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
        expires     1d;
        proxy_cache my_cache;
        proxy_pass http://127.0.0.1:8888;
        proxy_cache_valid 200 1d;
        #proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    }

    location /json/ {
        #try_files      /maintenance.html @backend;
        try_files      /_maintenance.html /foo.html;
    }

    location /favicon.ico {
        try_files      /maintenance.html @backend;
    }

    location @backend {
        proxy_ignore_headers "Cache-Control";
        proxy_hide_header "Cache-Control";
        proxy_ignore_headers "Set-Cookie";
        proxy_hide_header "Set-Cookie";
        proxy_cache my_cache;
        proxy_pass http://127.0.0.1:8888;

        # Avoid cache stampede - rather update once and deliver stale content
        proxy_cache_lock on;
        proxy_cache_use_stale updating;

        expires     1d;
        more_clear_headers 'Set-Cookie';
        more_clear_headers 'Cache-Control';
        more_clear_headers 'ETag';
        add_header X-Coffee json;
    }
  
    location /assets/ {
        alias /home/debshots/debshots/public/assets/;
        expires     1h;
        add_header X-Coffee assets;
    }
    location /logo/ {
        alias /home/debshots/debshots/public/logo/;
        expires     1h;
        add_header X-Coffee logo;
    }
    location /screenshots/ {
        alias /home/debshots/debshots/public/screenshots/;
        expires     max;
        add_header X-Coffee screenshots;
    }
    location /images/ {
        alias /home/debshots/debshots/public/images/;
        expires     1h;
        add_header X-Coffee images;
    }
}

# Backend rails application
server {
    #listen 85.25.83.22:80 default_server;
    #listen 85.25.83.22:443 ssl;
    #ssl_certificate     ssl/screenshots.debian.net.crt;
    #ssl_certificate_key ssl/screenshots.debian.net.key;
    listen 127.0.0.1:8888;

	root /home/debshots/debshots/public;

	server_name _;
    access_log /var/log/nginx/rails-access.log;

    passenger_enabled on;
    passenger_ruby /home/debshots/.rbenv/versions/2.3.1/bin/ruby;
    #expires     1d;
    #limit_req zone=one burst=5;

    #location / {
    #    try_files      /maintenance.html @passenger;
    #}
#
#    location @passenger {
#        #passenger_enabled      on;
#        #rails_env              production;
#        passenger_enabled on;
#        passenger_ruby /home/debshots/.rbenv/versions/2.3.1/bin/ruby;
#        #limit_req zone=one burst=5;
#        add_header X-Coffee app;
##    }


    # Send thumbnails using X-Sendfile / X-Accel-Redirect
    # The correct thumbnail is computed by the Rails application so it cannot be served directly.
    location /thumbnail/ {
        expires     1h;
        add_header X-Coffee thumbnail;

        passenger_set_header X-Sendfile-Type "X-Accel-Redirect";
        passenger_env_var HTTP_X_ACCEL_MAPPING /home/debshots/debshots/public/=/__send_file_accel/;
        passenger_pass_header X-Accel-Redirect;
    }

    # Send public assets using X-Sendfile / X-Accel-Redirect (e.g. public/images/dummy/...)
    location /public/ {
        expires     1h;
        add_header X-Coffee public;

        passenger_set_header X-Sendfile-Type "X-Accel-Redirect";
        passenger_env_var HTTP_X_ACCEL_MAPPING /home/debshots/debshots/public/=/__send_file_accel/;
        passenger_pass_header X-Accel-Redirect;
    }

    location /__send_file_accel/ {
        internal;
        alias /home/debshots/debshots/public/;
        add_header X-Coffee accel;
        expires     1d;
        more_clear_headers 'Set-Cookie';
    }

    location /secretstatus {
        stub_status;
        access_log   off;
        allow all;
        add_header X-Coffee status;
        #deny all;
    }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}
