# vim: set sw=4 ts=4 sts=4 expandtab:
server {
    listen 80;

    # Redirect HTTP to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;

    server_name {{ domain_name }};

    root /var/www/html;
    index index.php index.html index.htm;
    autoindex on;

    # All of these separate location blocks are.. messy.
    # But trying to nest them and keep everything
    # working is an exercise in frustration.
    #
    # nginx will fight you every step of the way.

    # userdir php config
    location ~ ^/~(.+?)/(.*\.php)$ {
        alias /home/$1/public_html;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME $document_root/$2;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }

    # userdir regular config
    location ~ ^/~(.+?)(/.*)?$ {
        alias /home/$1/public_html$2;
        index index.php index.html index.htm;
        autoindex on;
    }

    # phpmyadmin php config
    location ~ /phpmyadmin/(.*\.php)$ {
        alias /var/www/phpMyAdmin;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;

        include fastcgi.conf;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$1;

        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }

    # phpmyadmin regular config
    location /phpmyadmin {
        alias /var/www/phpMyAdmin;
        index index.php index.html index.htm;
    }

    # other php config
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }
}
