简体   繁体   中英

nginx-unit with PHP generates Server 500 error on any PHP file not found

This is specific to nginx-unit the somewhat new mini web server from NGINX team, NOT the typical nginx + php-fpm combination.

I followed the nginx-unit symfony example down to the exact byte https://unit.nginx.org/howto/symfony/ and it works, except when I try hitting a PHP file that doesn't exist, example /foo/bar.php , I get a Server 500 error with this message in logs:

PHP Fatal error: Unknown: Failed opening required '/app/public/foor/bar.php' (include_path='.:/usr/share/php') in Unknown on line 0

If I try accessing an aesthetic URL that doesn't exist such as /foo/bar my app gets hit and the expected 404 logic is used. I'd like for non-existent PHP files to generate proper 404s but it isn't clear to me how to do this with nginx-unit.

My config is basically the exact same as the example, which I'll repeat here:

{
    "listeners": {
        "*:80": {
            "pass": "routes/symfony"
        }
    },

    "routes": {
        "symfony": [
            {
                "match": {
                    "uri": [
                        "*.php",
                        "*.php/*"
                    ]
                },

                "action": {
                    "pass": "applications/symfony/direct"
                }
            },
            {
                "action": {
                    "share": "/path/to/app/public/",
                    "fallback": {
                        "pass": "applications/symfony/index"
                    }
                }
            }
        ]
    },

    "applications": {
        "symfony": {
            "type": "php",
            "user": "app_user",
            "group": "app_group",
            "targets": {
                "direct": {
                    "root": "/path/to/app/public/"
                },

                "index": {
                    "root": "/path/to/app/public/",
                    "script": "index.php"
                }
            }
        }
    }
}

If I had to guess I would say that due to the match part, *.php is trying to do a direct PHP include operation internally (before my app script is ever hit). When errors say Unknown on Line 0 that usually means internal code that you cannot control except via configuration.

I have found a solution to this. I changed my match->uri section to this:

        "match": {
          "uri": [
            "/index.php"
          ]
        }

What that did was, allow /index.php to load index.php as a PHP script, but pass any other routes, .php or not, also to index.php if the file didn't already exist. Thus, images, CSS, and other static assets will still deliver properly, and anything that doesn't exist will go to index.php so a 404 can be generated by my app.

My app only has one entrypoint, index.php , but if I wanted to add another custom php entrypoint such as health.php I could add it explicitly to this URI list and it will also be allowed to execute as PHP.

Now I have / and /index.php going to the same place but I can make my app do a redirect for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM