upstream php-fpm-internal {
    server unix:/var/run/php-fpm-internal.sock;
}

upstream php-fpm-www {
    server unix:/var/run/php-fpm-www.sock;
}

server {
    listen 127.0.0.1:6010;

    root {{ app_base }}/www/web;
    index index.php;

    # Default clean URL routing
    location / {
        try_files $uri @clean_url;
    }

    location @clean_url {
        rewrite ^(.*)$ /index.php last;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_pass php-fpm-internal;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include fastcgi_params;

        fastcgi_read_timeout 600;
        fastcgi_buffering off;

        internal;
    }
}

server {
    listen 80;
    listen 443 default_server ssl;
    listen [::]:80;
    listen [::]:443 default_server ssl;

    ssl_certificate    {{ app_base }}/acme/ssl.crt;
    ssl_certificate_key    {{ app_base }}/acme/ssl.key;

    root {{ app_base }}/www/web;
    index index.php;

    server_name localhost;

    add_header X-XSS-Protection 1;
    add_header X-Content-Type-Options nosniff;
    add_header Referrer-Policy no-referrer-when-downgrade;

    access_log {{ app_base }}/www_tmp/access.log;
    error_log {{ app_base }}/www_tmp/error.log;

    # LetsEncrypt handling
    location /.well-known/acme-challenge {
        alias {{ app_base }}/acme/challenges;
        try_files $uri =404;
    }

    # Serve a static version of the nowplaying data for non-PHP-blocking delivery.
    location /api/nowplaying_static {
        expires 10s;
        add_header Access-Control-Allow-Origin *;

        alias {{ app_base }}/www_tmp/nowplaying;
        try_files $uri =404;
    }

    location / {
        try_files $uri @clean_url;
    }

    location @clean_url {
        rewrite ^(.*)$ /index.php last;
    }

    # Set up caching for static assets.
    location /static {
        add_header Access-Control-Allow-Origin *;
    }

    location /static/uploads {
        rewrite ^(.+)\.(?:\w+)\.(js|css|png|jpg)$ $1.$2 last;

        alias /var/azuracast/uploads;
        try_files $uri =404;
    }

    location /static/dist {
        expires 365d;
    }
    location /static/webpack_dist {
        expires 365d;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_pass php-fpm-www;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include fastcgi_params;

        fastcgi_read_timeout 1800;
        fastcgi_buffering off;

        internal;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
        deny all;
    }

    # Internal handlers used by the application to perform X-Accel-Redirect's for higher performance.
    location /internal/backups/ {
        internal;
        alias {{ app_base }}/backups/;
    }

    location /internal/stations/ {
        internal;
        alias {{ app_base }}/stations/;
    }

    include {{ app_base }}/stations/*/config/nginx.conf;
}
