daemon off;
worker_processes auto;

events {}

http {
    include           mime.types;
    default_type      application/octet-stream;
    sendfile          on;
    tcp_nopush        on;
    tcp_nodelay       on;
    keepalive_timeout 65;

    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m inactive=600s max_size=100m;

    # Limit post requests
    map $request_method $limit {
        default "";
        POST $binary_remote_addr;
    }

    # Creates 10mb zone in memory for storing binary IPs
    limit_req_zone $limit zone=post_limit:10m rate=11r/m;

    upstream http_backend {
        keepalive 20;
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name _; # Put your own domain here
        set $prefix "/opt/schemebbs";

        # Site root, a static page
        location = / {
            rewrite ^ /static/index.html;
        }
        location = /favicon.ico {
            rewrite ^ /static/favicon.ico;
        }
        location /static/ {
            alias $prefix/static/;
        }

        # Serve S-expressions as static files
        location /sexp {
            alias $prefix/data/sexp/;
            autoindex on;
            default_type text/x-scheme;
            fancyindex on;
            fancyindex_time_format "%F %R";
            fancyindex_footer "/static/lisp.html";
        }

        location / {
            root   $prefix/data/html;
            default_type text/html;
            index  index;
            try_files $uri $uri/index @schemebbs;
        }

        # Replace CSS and add query string to all internal links
        if ( $query_string = "" ) {
            set $bypass "1";
        }
        subs_filter_bypass $bypass;
        subs_filter '<LINK href="/static/styles/(.*?).css"' '<LINK href="/static/styles/$arg_css.css"' or;
        subs_filter '<A href="((?!http).*?)(#.*?)?"' '<A href="$1$is_args$args$2"' gr;
        subs_filter '<FORM action="(.*?post)"' '<FORM action="$1$is_args$args"' gr;

        location @schemebbs {
            proxy_intercept_errors on;
            proxy_http_version 1.1; 
            proxy_set_header Connection "";
            proxy_set_header Accept-Encoding "";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Clacks-Overhead "GNU John McCarthy";
            proxy_cache cache;
            proxy_cache_key $scheme$host$request_method$request_uri;
            proxy_cache_valid 200 30s;
            proxy_pass http://http_backend;
        }

        error_page 400 /400.html;
        error_page 403 /403.html;
        error_page 404 /404.html;
        error_page 405 /405.html;
        error_page 429 /429.html;
        error_page 500 /500.html;
        error_page 502 /502.html;
        error_page 503 /503.html;
        error_page 504 /504.html;

        location ~ ^/(400|403|404|405|429|500|502|503|504)\.html {
            root $prefix/static/errors;
        }
    }
}

# Local Variables:
# indent-tabs-mode: nil
# tab-width: 4
# End:
