From aa5f1cf641dbb7b2cef2454152892cd29274bcab Mon Sep 17 00:00:00 2001 From: Mkkbeydoun Date: Thu, 29 Oct 2026 16:48:20 -0400 Subject: [PATCH] add 404 page option --- README.md | 15 +++++++++++++++ egg-nginx-webhost.json | 12 +++++++++++- nginx/conf.d/default.conf | 5 ++++- start.sh | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a1212f..c89d75a 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ blank. | Wordpress (`WORDPRESS`) | `0` | Install WordPress into an empty `webroot/`. Finish setup at `/wp-admin` | | Composer modules (`COMPOSER_MODULES`) | | Space separated packages, e.g. `phpmailer/phpmailer vlucas/phpdotenv`. Pin a version with `vendor/package:^6.0` | | Root Folder (`ROOT_FOLDER`) | | Folder in `webroot/` to serve at your domain's root, see [below](#serving-a-folder-at-the-root). Blank serves `webroot/` as is | +| 404 Page (`ERROR_404_PAGE`) | | Page to show for missing pages, see [below](#custom-404-page). Blank keeps sending them to `index.php` | | User Uploaded Files (`USER_UPLOAD`) | `0` | Skip the Git setup, you upload the files yourself | | Update Check (`UPDATE_CHECK`) | `1` | Say in the console when the egg or the Docker image has an update | | Startup Command (`STARTUP_CMD`) | `./start.sh` | Leave as is unless you know you need to change it | @@ -88,6 +89,20 @@ work the same in all of them. - Servers installed before the egg had this option need a **Reinstall** first (see [Updates](#updates)) to get the new `nginx/conf.d/default.conf`. +### Custom 404 page + +Set **404 Page** to the path of a page in your site, e.g. `404.html` or `errors/404.php`, and +every page that doesn't exist shows it with a `404` status. HTML and PHP pages both work. The +path is from your site's root: `webroot/`, or the **Root Folder** if you set one. + +- Blank (default) does what the egg always did: missing pages go to `index.php`, which is + what WordPress and other apps that route through `index.php` need. Leave it blank for those, + they show their own 404 page. +- Links in the page should start with `/` (e.g. `/style.css`), it's shown at whatever address + was missing. +- If the page doesn't exist, visitors get nginx's plain 404 and the console says so. +- Needs a **Reinstall** first on servers installed before the egg had this option. + ### Git | Variable | Default | What it does | diff --git a/egg-nginx-webhost.json b/egg-nginx-webhost.json index 33d5c7d..5633d84 100644 --- a/egg-nginx-webhost.json +++ b/egg-nginx-webhost.json @@ -24,7 +24,7 @@ }, "scripts": { "installation": { - "script": "#!\/bin\/ash\n# Nginx Webhost installer: installs the nginx\/PHP config and sets up the website files.\n\n# Bump this whenever the egg or the repo's config files change. Servers compare it with\n# the egg on GitHub at startup and show an update notice in the console.\nEGG_VERSION=\"1.0.4\"\n\nREPO=\"https:\/\/github.com\/Rustix-Development\/pterodactyl-nginx-egg\"\n\nis_true() { [ \"$1\" = \"1\" ] || [ \"$1\" = \"true\" ]; }\n\n# Drop user:token@ from a git URL so a changed token still counts as the same repo\nstrip_auth() { printf '%s' \"$1\" | sed -E 's#^([a-z]+:\/\/)[^\/@]*@#\\1#'; }\n\ntrap 'rm -rf \/tmp\/egg' EXIT\n\n# The install container has a terminal nobody types into: never wait for a git password\nexport GIT_TERMINAL_PROMPT=0\n\ncd \/mnt\/server || exit 1\napk add --no-cache git curl || { echo \"Failed to install git and curl\"; exit 1; }\n# Server files belong to the server user, not root; let git work on them anyway\ngit config --global --add safe.directory '*'\n\necho \"Downloading config files from ${REPO}\"\nrm -rf \/tmp\/egg\ngit clone --depth 1 \"${REPO}\" \/tmp\/egg || { echo \"Failed to download config files\"; exit 1; }\nif [ ! -f \/tmp\/egg\/start.sh ] || [ ! -d \/tmp\/egg\/nginx ] || [ ! -d \/tmp\/egg\/php-fpm ]; then\n echo \"Config files are missing from ${REPO} (empty repo or wrong default branch?)\"\n exit 1\nfi\n\n# Config files are replaced on every (re)install\ncp -r \/tmp\/egg\/nginx \/tmp\/egg\/php-fpm \/mnt\/server\/ \\\n && cp \/tmp\/egg\/start.sh \/mnt\/server\/start.sh \\\n && chmod +x \/mnt\/server\/start.sh \\\n || { echo \"Failed to copy config files\"; exit 1; }\n# Left behind by older versions of this egg, it stops nginx from starting\nrm -f \/mnt\/server\/nginx\/conf.d\/nextcloud.conf\nmkdir -p \/mnt\/server\/webroot \/mnt\/server\/tmp \/mnt\/server\/logs\necho \"${EGG_VERSION}\" > \/mnt\/server\/.egg-version\necho \"Installed Nginx Webhost egg v${EGG_VERSION} config files\"\n\ncd \/mnt\/server\/webroot || exit 1\n\n# A default landing page (this egg's or an older egg's) doesn't count as a website\nif [ \"$(ls -A)\" = \"index.php\" ] && grep -qE 'Web server is running<\/h1>|Webserver Correctly Installed' index.php; then\n rm -f index.php\nfi\n\nseed_default_page() {\n if [ -z \"$(ls -A)\" ]; then\n cp \/tmp\/egg\/webroot\/index.php .\/\n fi\n}\n\n# WordPress\nif is_true \"${WORDPRESS}\"; then\n if [ -f wp-load.php ]; then\n echo \"WordPress is already installed, leaving it alone.\"\n elif [ -n \"$(ls -A)\" ]; then\n echo \"webroot already has files, not installing WordPress over them.\"\n else\n echo \"Installing WordPress...\"\n # Unpack outside webroot so a broken download never leaves files behind in it\n WP_TMP=\/mnt\/server\/.wordpress-download\n rm -rf \"${WP_TMP}\" && mkdir -p \"${WP_TMP}\"\n if ! curl -fsSL https:\/\/wordpress.org\/latest.tar.gz | tar xz -C \"${WP_TMP}\" || [ ! -f \"${WP_TMP}\/wordpress\/wp-load.php\" ]; then\n rm -rf \"${WP_TMP}\"\n echo \"Failed to download WordPress\"\n exit 1\n fi\n cp -a \"${WP_TMP}\/wordpress\/.\" . && rm -rf \"${WP_TMP}\"\n echo \"WordPress installed. Finish setup at http:\/\/ip:port\/wp-admin\"\n fi\n exit 0\nfi\n\nif is_true \"${USER_UPLOAD}\"; then\n seed_default_page\n echo \"User Uploaded Files is on, skipping website setup. Upload your files into webroot.\"\n exit 0\nfi\n\nif [ -z \"${GIT_ADDRESS}\" ]; then\n seed_default_page\n echo \"Install complete. No Git Repo Address set, upload your website into webroot.\"\n exit 0\nfi\n\n# Git\ncase \"${GIT_ADDRESS}\" in\n *.git) ;;\n *) GIT_ADDRESS=\"${GIT_ADDRESS}.git\" ;;\nesac\n\nif [ -n \"${USERNAME}\" ] && [ -n \"${ACCESS_TOKEN}\" ]; then\n GIT_ADDRESS=\"https:\/\/${USERNAME}:${ACCESS_TOKEN}@${GIT_ADDRESS#*:\/\/}\"\nfi\n\nif [ -n \"$(ls -A)\" ]; then\n if [ -d .git ]; then\n if [ \"$(strip_auth \"$(git config --get remote.origin.url)\")\" = \"$(strip_auth \"${GIT_ADDRESS}\")\" ]; then\n echo \"Pulling latest website files from git\"\n # Picks up a changed Git Username\/Access Token\n git remote set-url origin \"${GIT_ADDRESS}\"\n git pull --ff-only || echo \"git pull failed, keeping the current files.\"\n else\n echo \"webroot is a clone of a different git repo, leaving it alone.\"\n fi\n else\n echo \"webroot already has files that aren't from git, leaving them alone.\"\n fi\nelse\n echo \"Cloning website files from git\"\n if [ -n \"${BRANCH}\" ]; then\n git clone --single-branch --branch \"${BRANCH}\" \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address, Git Branch and credentials\"; exit 1; }\n else\n git clone \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address and credentials\"; exit 1; }\n fi\nfi\n\necho \"Install complete. Visit http:\/\/ip:port\/\"\n", + "script": "#!\/bin\/ash\n# Nginx Webhost installer: installs the nginx\/PHP config and sets up the website files.\n\n# Bump this whenever the egg or the repo's config files change. Servers compare it with\n# the egg on GitHub at startup and show an update notice in the console.\nEGG_VERSION=\"1.0.5\"\n\nREPO=\"https:\/\/github.com\/Rustix-Development\/pterodactyl-nginx-egg\"\n\nis_true() { [ \"$1\" = \"1\" ] || [ \"$1\" = \"true\" ]; }\n\n# Drop user:token@ from a git URL so a changed token still counts as the same repo\nstrip_auth() { printf '%s' \"$1\" | sed -E 's#^([a-z]+:\/\/)[^\/@]*@#\\1#'; }\n\ntrap 'rm -rf \/tmp\/egg' EXIT\n\n# The install container has a terminal nobody types into: never wait for a git password\nexport GIT_TERMINAL_PROMPT=0\n\ncd \/mnt\/server || exit 1\napk add --no-cache git curl || { echo \"Failed to install git and curl\"; exit 1; }\n# Server files belong to the server user, not root; let git work on them anyway\ngit config --global --add safe.directory '*'\n\necho \"Downloading config files from ${REPO}\"\nrm -rf \/tmp\/egg\ngit clone --depth 1 \"${REPO}\" \/tmp\/egg || { echo \"Failed to download config files\"; exit 1; }\nif [ ! -f \/tmp\/egg\/start.sh ] || [ ! -d \/tmp\/egg\/nginx ] || [ ! -d \/tmp\/egg\/php-fpm ]; then\n echo \"Config files are missing from ${REPO} (empty repo or wrong default branch?)\"\n exit 1\nfi\n\n# Config files are replaced on every (re)install\ncp -r \/tmp\/egg\/nginx \/tmp\/egg\/php-fpm \/mnt\/server\/ \\\n && cp \/tmp\/egg\/start.sh \/mnt\/server\/start.sh \\\n && chmod +x \/mnt\/server\/start.sh \\\n || { echo \"Failed to copy config files\"; exit 1; }\n# Left behind by older versions of this egg, it stops nginx from starting\nrm -f \/mnt\/server\/nginx\/conf.d\/nextcloud.conf\nmkdir -p \/mnt\/server\/webroot \/mnt\/server\/tmp \/mnt\/server\/logs\necho \"${EGG_VERSION}\" > \/mnt\/server\/.egg-version\necho \"Installed Nginx Webhost egg v${EGG_VERSION} config files\"\n\ncd \/mnt\/server\/webroot || exit 1\n\n# A default landing page (this egg's or an older egg's) doesn't count as a website\nif [ \"$(ls -A)\" = \"index.php\" ] && grep -qE 'Web server is running<\/h1>|Webserver Correctly Installed' index.php; then\n rm -f index.php\nfi\n\nseed_default_page() {\n if [ -z \"$(ls -A)\" ]; then\n cp \/tmp\/egg\/webroot\/index.php .\/\n fi\n}\n\n# WordPress\nif is_true \"${WORDPRESS}\"; then\n if [ -f wp-load.php ]; then\n echo \"WordPress is already installed, leaving it alone.\"\n elif [ -n \"$(ls -A)\" ]; then\n echo \"webroot already has files, not installing WordPress over them.\"\n else\n echo \"Installing WordPress...\"\n # Unpack outside webroot so a broken download never leaves files behind in it\n WP_TMP=\/mnt\/server\/.wordpress-download\n rm -rf \"${WP_TMP}\" && mkdir -p \"${WP_TMP}\"\n if ! curl -fsSL https:\/\/wordpress.org\/latest.tar.gz | tar xz -C \"${WP_TMP}\" || [ ! -f \"${WP_TMP}\/wordpress\/wp-load.php\" ]; then\n rm -rf \"${WP_TMP}\"\n echo \"Failed to download WordPress\"\n exit 1\n fi\n cp -a \"${WP_TMP}\/wordpress\/.\" . && rm -rf \"${WP_TMP}\"\n echo \"WordPress installed. Finish setup at http:\/\/ip:port\/wp-admin\"\n fi\n exit 0\nfi\n\nif is_true \"${USER_UPLOAD}\"; then\n seed_default_page\n echo \"User Uploaded Files is on, skipping website setup. Upload your files into webroot.\"\n exit 0\nfi\n\nif [ -z \"${GIT_ADDRESS}\" ]; then\n seed_default_page\n echo \"Install complete. No Git Repo Address set, upload your website into webroot.\"\n exit 0\nfi\n\n# Git\ncase \"${GIT_ADDRESS}\" in\n *.git) ;;\n *) GIT_ADDRESS=\"${GIT_ADDRESS}.git\" ;;\nesac\n\nif [ -n \"${USERNAME}\" ] && [ -n \"${ACCESS_TOKEN}\" ]; then\n GIT_ADDRESS=\"https:\/\/${USERNAME}:${ACCESS_TOKEN}@${GIT_ADDRESS#*:\/\/}\"\nfi\n\nif [ -n \"$(ls -A)\" ]; then\n if [ -d .git ]; then\n if [ \"$(strip_auth \"$(git config --get remote.origin.url)\")\" = \"$(strip_auth \"${GIT_ADDRESS}\")\" ]; then\n echo \"Pulling latest website files from git\"\n # Picks up a changed Git Username\/Access Token\n git remote set-url origin \"${GIT_ADDRESS}\"\n git pull --ff-only || echo \"git pull failed, keeping the current files.\"\n else\n echo \"webroot is a clone of a different git repo, leaving it alone.\"\n fi\n else\n echo \"webroot already has files that aren't from git, leaving them alone.\"\n fi\nelse\n echo \"Cloning website files from git\"\n if [ -n \"${BRANCH}\" ]; then\n git clone --single-branch --branch \"${BRANCH}\" \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address, Git Branch and credentials\"; exit 1; }\n else\n git clone \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address and credentials\"; exit 1; }\n fi\nfi\n\necho \"Install complete. Visit http:\/\/ip:port\/\"\n", "container": "alpine:3.24", "entrypoint": "ash" } @@ -110,6 +110,16 @@ "rules": "nullable|string|max:255|regex:\/^\\\/?[A-Za-z0-9][A-Za-z0-9._-]*(\\\/[A-Za-z0-9][A-Za-z0-9._-]*)*\\\/?$\/", "field_type": "text" }, + { + "name": "404 Page", + "description": "Page to show when a page doesn't exist, e.g. 404.html or errors\/404.php (HTML or PHP), from your site's root (webroot, or the Root Folder if set). Visitors get it with a 404 status.\n\nLeave blank to send missing pages to index.php as before (default, needed for WordPress and other apps that show their own 404).", + "env_variable": "ERROR_404_PAGE", + "default_value": "", + "user_viewable": true, + "user_editable": true, + "rules": "nullable|string|max:255|regex:\/^\\\/?[A-Za-z0-9][A-Za-z0-9._-]*(\\\/[A-Za-z0-9][A-Za-z0-9._-]*)*$\/", + "field_type": "text" + }, { "name": "Update Check", "description": "Check GitHub on startup and say in the console when the egg or the Docker image has an update.\n\n0 = false\n1 = true (default)", diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index 1f4bf19..a705b1b 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -17,8 +17,11 @@ server { client_body_timeout 120s; sendfile off; + # Also written by start.sh: what missing pages get, index.php or the 404 Page variable's page + include /home/container/tmp/nginx-errors.conf; + location / { - try_files $uri $uri/ /index.php?$query_string; + try_files $uri $uri/ @missing; } # Falls back to PHP so WordPress etc. can generate them diff --git a/start.sh b/start.sh index f6abd8f..bc4ccaf 100755 --- a/start.sh +++ b/start.sh @@ -242,6 +242,46 @@ EOF } write_root_conf +# What missing pages get (default.conf includes the file written here). Blank: index.php, like +# before, so WordPress and other apps that route through it keep working. A path: that page, +# with a 404 status. It's looked up like any other page, so with a Root Folder set it's in there. +write_errors_conf() { + conf=/home/container/tmp/nginx-errors.conf + page=$(printf '%s' "${ERROR_404_PAGE}" | sed 's#//*#/#g; s#^/##') + + # Same rules as Root Folder: every part starts with a letter or digit, so no "..", dotfiles or spaces + if [ -n "${page}" ] && ! printf '%s' "${page}" | grep -qE '^[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)*$'; then + log_warning "404 Page \"${ERROR_404_PAGE}\" isn't a file path in your site, using the default 404." + page="" + fi + + if [ -z "${page}" ]; then + cat > "${conf}" <<'EOF' +# Written by start.sh, edits are lost on restart. 404 Page is blank: missing pages go to index.php. +location @missing { + rewrite ^ /index.php last; +} +EOF + return + fi + + cat > "${conf}" < Reinstall Server) to get the new config." + fi +} +write_errors_conf + log_info "Starting nginx..." set -- -e stderr -c /home/container/nginx/nginx.conf -p /home/container/ if ! grep -qE '^[[:space:]]*worker_processes' /home/container/nginx/nginx.conf; then