upstream aurweb {
    server unix://{{ aurweb_socket }};
}

upstream cgit {
    server unix://{{ cgit_socket }};
}

upstream smartgit {
    server unix://{{ smartgit_socket }};
}

server {
    listen       80;
    listen       [::]:80;
    server_name  {{ aurweb_domain }};

    access_log   /var/log/nginx/{{ aurweb_domain }}/access.log main;
    access_log   /var/log/nginx/{{ aurweb_domain }}/access.log.json json_main;
    error_log    /var/log/nginx/{{ aurweb_domain }}/error.log;

    include snippets/letsencrypt.conf;

    location / {
        return 301 https://$server_name$request_uri;
    }
}

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  {{ aurweb_domain }};

    access_log   /var/log/nginx/{{ aurweb_domain }}/access.log main;
    access_log   /var/log/nginx/{{ aurweb_domain }}/access.log.json json_main;
    error_log    /var/log/nginx/{{ aurweb_domain }}/error.log;

    ssl_certificate      /etc/letsencrypt/live/{{ aurweb_domain }}/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/{{ aurweb_domain }}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{ aurweb_domain }}/chain.pem;

    root    {{ aurweb_dir }}/web/html;
    index   index.php;

    location = /trusted-user/TUbylaws.html {
	    return 301 https://tu-bylaws.aur.archlinux.org;
    }

    location ~ "^/([a-z0-9][a-z0-9.+_-]*?)(\.git)?/(git-(receive|upload)-pack|HEAD|info/refs|objects/(info/(http-)?alternates|packs)|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\.(pack|idx))$" {
        include      uwsgi_params;
        uwsgi_pass   smartgit;
        uwsgi_modifier1 9;
        uwsgi_param  SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        uwsgi_param  PATH_INFO /aur.git/$3;
        uwsgi_param  GIT_HTTP_EXPORT_ALL "";
        uwsgi_param  GIT_NAMESPACE $1;
        uwsgi_param  GIT_PROJECT_ROOT {{ aurweb_dir }};
    }

    location ~ ^/cgit {
        include uwsgi_params;
        rewrite ^/cgit/([^?/]+/[^?]*)?(?:\?(.*))?$ /cgit.cgi?url=$1&$2 last;
        uwsgi_modifier1 9;
        uwsgi_param CGIT_CONFIG {{ aurweb_conf_dir }}/cgitrc;
        uwsgi_pass cgit;
    }

    location ~ \.gz$ {
        default_type text/plain;
        add_header Content-Encoding gzip;
        expires 5m;
    }

    location ~ ^/[^/]+\.php($|/) {
        fastcgi_pass   aurweb;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(/[^/]+\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  HTTPS            on;
        include        fastcgi_params;

        # Cache PHP Requests
        #fastcgi_cache aur;
        #fastcgi_cache_valid 200 5m;
        #add_header X-Cache $upstream_cache_status;

        # Required for caching to work
        #fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";

        # Only apply cache when set
        #fastcgi_cache_bypass $no_cache;
        #fastcgi_no_cache $no_cache;
    }

    # directories for static assets
    location ~ ^/(?:css|js|images)/ {
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location ~ .* {
        rewrite ^/(.*)$ /index.php/$1 last;
    }
}
