Nginx php behind npm

From clowlove/Hermes-House · View interactive version

Nginx php behind npm is an AI agent skill from the GitHub repository clowlove/Hermes-House. Bind a PHP website served by the host nginx behind Nginx Proxy Manager when the NPM container already occupies port 80/. Covers the exact conflict pattern: default site → nginx can't start → proxy never reaches PHP-FPM, and the known good resolution order.

How to use this skill

Skill file

Nginx + PHP behind Nginx Proxy Manager

Use this when the host nginx and the NPM container both want port 80/443

and your PHP site must be reached through a domain managed by NPM.

Trigger

Required belief before proceeding

Nginx on the host and NPM cannot both bind 80. The host nginx

must use a non-overlapping port (e.g. 8087+). After this, NPM forwards

traffic to the host nginx, which talks to php-fpm.

Fixed order

  1. Delete the default site when it collides with NPM:
rm -f /etc/nginx/sites-enabled/default
nginx -t && nginx -c /etc/nginx/nginx.conf -g "daemon on;" || true
ss -tlnp | grep ':8086\|:8087\|:80\|nginx'
  1. Ubuntu 22.04 php-fpm8.1 socket path is /run/php/php8.1-fpm.sock.

Do not guess the path.

  1. Server block for the site listens on a free high port:
server {
    listen 8087 default_server;
    server_name _;
    root /path/to/site;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
  1. Restart host nginx. Then curl -I http://127.0.0.1:8087 must return 200 OK.
  1. Update the NPM proxy host to forward_scheme=http, forward_host=154.41.135.101,

forward_port=<chosen port>, ssl_forced=false, http2_support=false.

  1. Verify from outside: curl -I http://<domain> and expect 200 OK.

Pitfalls

Restart is the calmer choice.

is the unlock pattern.

run chmod o+rx /root and chmod -R o+rx /path/to/site before testing.

discover with find /run /var/run -name 'php*-fpm*.sock' instead of hardcoding.

Extra/missing fields yield 400 {code:400, message:'data must NOT have additional properties'}.

Accept the current certificate state instead of forcing ssl_forced=false.

nginx -c /etc/nginx/nginx.conf -g "daemon on;" to restart cleanly instead of reload.

Pretty URL rewrites after binding

Once the site is live, internal pretty URLs like /product/<slug> and /news/<slug> still 404 if Nginx doesn't rewrite them to the PHP router. Add these blocks before the generic location ~ \.php$ block:

location ~ ^/product/(.+)$ {
    rewrite ^/product/(.+)$ /product.php?slug=$1 last;
}
location ~ ^/news/(.+)\.html$ {
    rewrite ^/news/(.+)\.html$ /news_detail.php?slug=$1 last;
}
location ~ ^/news/(.+)$ {
    rewrite ^/news/(.+)$ /news_detail.php?slug=$1 last;
}

product.php already strips a leading /product/, and news_detail.php strips /news/ and .html, so the router itself usually does not need code changes — the rewrite alone is enough.

Link verification expectations

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.