user  nginx;
worker_processes  1;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format main   '$http_x_forwarded_for $remote_user $status '
                      '"$request" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"' ;

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log  error;

    server_tokens   off;
    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "msie6";

    upstream backend {
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name _;

        root /app/www/;

        proxy_http_version 1.1;
        proxy_hide_header X-powered-by;
        proxy_cache_bypass $http_upgrade;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Host $host;

        location / {
            auth_basic "Admin area";
            auth_basic_user_file /auth/.htpasswd;
            log_not_found off;
            index index.html index.htm;
            try_files $uri $uri/ =404;
        }

        location ~^(/admin/api|/ota) {
            proxy_pass http://backend;
        }

        location ~ ^/firmwares/(.+\.(json|xml|zip|img|bin))$ {
            alias /firmwares/$1;
        }

    }
}
