##
# phpMyFAQ nginx.conf file
#
# this assumes you installed in /
#   if that is not the case,
#   sed 's,/,,g' _nginx.conf
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
#
# @author    Florian Anderiasch <florian@phpmyfaq.de>
# @copyright 2011-2025 phpMyFAQ Team
# @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
# @link      https://www.phpmyfaq.de
# @since     2011-01-14
#

server {
    listen      80;
    server_name example.org;
    root        /srv/www/default/public;
    index       index.php index.html index.htm;

    rewrite //  /           break;
    rewrite ^/$ /index.php  last;

    # Gzip Settings
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_proxied no-cache no-store private expired auth;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    location ~* \.(jpg|jpeg|gif|png|webp|svg|ico|mp4|webm|mpeg|ttf|otf|woff|woff2|css|js|pdf)$ {
        expires 1y;
        add_header Cache-Control "public";
    }

    # Additional configurations for specific file types
    location ~* \.(svg|eot|ttf|woff|woff2|json)$ {
        add_header Access-Control-Allow-Origin "*";
    }

    # Specific cache-control for javascript files
    location ~* \.(js)$ {
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # Specific cache-control for CSS files
    location ~* \.(css)$ {
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # Rewrite logging, should be turned off on production
    rewrite_log on;

    # X-Frame-Options to prevent clickjacking
    add_header X-Frame-Options SAMEORIGIN;

    location / {
        index index.php;

        if (!-f $request_filename) {
            # General pages
            rewrite ^/add-faq.html$                                 /index.php?action=add last;
            rewrite ^/add-question.html$                            /index.php?action=ask last;
            rewrite ^/show-categories.html$                         /index.php?action=show last;
            rewrite ^/(search|open-questions|help|contact|glossary|overview|login|privacy|index).html$ /index.php?action=$1 last;
            rewrite ^/(login)                                       /index.php?action=login last;

            # a solution id page
            rewrite ^/solution_id_([0-9]+).html$                    /index.php?solution_id=$1 last;

            # the bookmarks page
            rewrite ^/bookmarks.html$                               /index.php?action=bookmarks last;

            # PMF faq record page
            rewrite ^/content/([0-9]+)/([0-9]+)/([a-z\-_]+)/(.+)\.htm(l?)$ /index.php?action=faq&cat=$1&id=$2&artlang=$3 last;

            # PMF category page with page count
            rewrite ^/category/([0-9]+)/([0-9]+)/(.+).html$           /index.php?action=show&cat=$1&seite=$2 last;

            # PMF category page
            rewrite ^/category/([0-9]+)/(.+).html$                    /index.php?action=show&cat=$1 last;

            # PMF news page
            rewrite ^/news/([0-9]+)/([a-z\-_]+)/(.+).html$             /index.php?action=news&newsid=$1&newslang=$2 last;

            # PMF sitemap
            rewrite s^/itemap/([^\/]+)/([a-z\-_]+).htm(l?)$            /index.php?action=sitemap&letter=$1&lang=$2 last;

            # PMF Google sitemap
            rewrite ^/sitemap.xml$ /sitemap.xml.php last;
            rewrite ^/sitemap.gz$ /sitemap.xml.php?gz=1 last;
            rewrite ^/sitemap.xml.gz$ /sitemap.xml.php?gz=1 last;

            # robots.txt
            rewrite ^/robots.txt$ /robots.txt.php last;

            # llms.txt
            rewrite ^/llms.txt$ /llms.txt.php last;

            # PMF tags page with page count
            rewrite ^/tags/([0-9]+)/([0-9]+)/(.+).htm(l?)$           /index.php?action=search&tagging_id=$1&seite=$2 last;

            # PMF tags page
            rewrite tags/([0-9]+)/([^\/]+).htm(l?)$                  /index.php?action=search&tagging_id=$1 last;

            # Authentication services
            rewrite services/webauthn/(.*)                           /services/webauthn/index.php last;

            # User pages
            rewrite user/(ucp|bookmarks|request-removal|logout|register) /index.php?action=$1 last;

            # Administration API
            rewrite admin/api/(.*)                                   /admin/api/index.php last;

            # Administration pages
            rewrite ^/admin/(.*)                                     /admin/index.php last;

            # REST API v3.0 and v3.1
            rewrite ^/api/v3\.[01]/(.*)                              /api/index.php last;

            # Private APIs
            rewrite ^/api/(.*)                                       /api/index.php last;

            # Setup and update pages
            rewrite ^/setup/                                         /setup/index.php last;
            rewrite ^/update/                                        /update/index.php last;

            break;
        }
    }

    location /admin/assets {
        try_files $uri $uri/ =404;
    }

    location @php {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root/index.php;
        include         /etc/nginx/fastcgi_params; 
    }

    location ~ '/.+\.ph(p|tml)(/|$)' {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        include         /etc/nginx/fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 2d;
        add_header Cache-Control "no-store";
    }
}
