user www;
worker_processes auto;
error_log stderr warn;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    # Threat files with a unknown filetype as binary
    default_type application/octet-stream;

    # Define custom log format to include reponse times
    log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '$request_time $upstream_response_time $pipe $upstream_cache_status';

    access_log /dev/stdout main_timed;
    error_log /dev/stderr crit;

    keepalive_timeout 65;

    server_tokens off;

    # Enable gzip compression by default
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    # Include server configs
    server {
        listen [::]:80 default_server;
        listen 80 default_server;
        server_name _;

        sendfile on;
        tcp_nodelay on;
        absolute_redirect off;

        root /app/www/public;
        index index.php;

        # Pass the PHP scripts to PHP-FPM listening on php-fpm.sock
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        #rewrite rule for pretty urls
        location / {
            if (!-e $request_filename){
                rewrite  ^(.*)$  /index.php?s=$1  last;   break;
            }
        }

        # Set the cache-control headers on assets to cache for 5 days
        location ~* \.(jpg|jpeg|gif|png|ico|bmp)$ {
            access_log off;
            expires 30d;
        }

        location ~* \.(css|js)$ {
            access_log off;
            expires 12h;
        }

        # Deny access to . files, for security
        location ~ /\. {
            log_not_found off;
            deny all;
        }

        # Allow fpm ping and status from localhost
        location ~ ^/(fpm-status|fpm-ping)$ {
            access_log off;
            allow 127.0.0.1;
            deny all;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/run/php-fpm.sock;
        }
    }
}
