NoiseByNorthwest / php-spx

A simple & straight-to-the-point PHP profiling extension with its built-in web UI
GNU General Public License v3.0
2.13k stars 83 forks source link

Bad Gateway when accessing /_spx?SPX_KEY=dev #38

Open gamerlv opened 6 years ago

gamerlv commented 6 years ago

I'm trying to access the web ui to read out some CLI profiles created but I'm having trouble accessing it. I've followed the steps as laid out in the readme but after a few attempts, I'm not really getting anywhere.

When I try to access http://localhost/_spx?SPX_KEY=dev I get a bad gateway error. With both Nginx and Caddy.

The PHP error logs reports the following:

==> /var/log/php/fpm-www.log <==
[25-Mar-2018 01:19:27 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:19:27 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:19:40 Europe/Berlin] PHP Notice:  SPX: access granted: "127.0.0.1" IP with "dev" key in Unknown on line 0
[25-Mar-2018 01:19:40 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:19:40 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:19:40 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:29:03 Europe/Berlin] PHP Notice:  SPX: access granted: "127.0.0.1" IP with "dev" key in Unknown on line 0
[25-Mar-2018 01:29:03 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:29:03 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0
[25-Mar-2018 01:29:03 Europe/Berlin] PHP Warning:  Cannot modify header information - headers already sent in Unknown on line 0

==> /var/log/php/www.access.log <==
127.0.0.1 -  25/Mar/2018:01:15:05 +0100 "GET /_spx/index.html" 404
127.0.0.1 -  25/Mar/2018:01:15:11 +0100 "GET /_spx/index.html" 404
127.0.0.1 -  25/Mar/2018:01:15:11 +0100 "GET /_spx/index.html" 404
127.0.0.1 -  25/Mar/2018:01:15:43 +0100 "GET /_spx/" 404
127.0.0.1 -  25/Mar/2018:01:15:50 +0100 "GET /_spx" 404
127.0.0.1 -  25/Mar/2018:01:18:34 +0100 "GET /_spx/index.html" 404
127.0.0.1 -  25/Mar/2018:01:19:09 +0100 "GET /_spx/index.html" 404
127.0.0.1 -  25/Mar/2018:01:19:27 +0100 "GET /_spx" 404
127.0.0.1 -  25/Mar/2018:01:19:40 +0100 "GET /_spx/" 404
127.0.0.1 -  25/Mar/2018:01:29:03 +0100 "GET /_spx/" 404

When I try to access http://localhost/_spx/?SPX_KEY=dev or http://localhost/_spx/index.html?SPX_KEY=dev through nginx I get weird malformed html presented as text. See spx_responses.txt

Versions

I'm using the following software: PHP 7.1.15-1+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Mar 6 2018 11:10:13) ( NTS ) PHP is used as php-fpm through tcp socket to Nginx.

nginx version: nginx/1.13.9 Built by gcc 6.4.0 (Alpine 6.4.0) Built with OpenSSL 1.0.2n 7 Dec 2017 TLS SNI support enabled Through docker: nginx:alpine (537527661905)

I just build the extension from source, commit 91ea8d6b75ac97603f0d9ad08568315482b60aef.

NoiseByNorthwest commented 6 years ago

Thanks for this detailed issue.

Can you also show me the php script (aka front controller if you're using a framework) which serves any url like /_spx ?

gamerlv commented 6 years ago

So far I've not needed any web server other than the built-in one from php for my projects. The web server does not serve any content at this point in time. I've been trying to profile a Laravel artisan task, trying to find bottlenecks. My nginx config reflects that. Everything is passed through to php-fpm. The /srv folder just contains a phpinfo() file to check if everything was being loaded.

server {
listen 80 default_server;
    root /srv;
    location / {
        include "fastcgi.conf";
        fastcgi_pass 127.0.0.1:9000;
    }
}
NoiseByNorthwest commented 6 years ago

To have SPX web ui working you need to have a php script called on any unexisting URL (like a framework front controllers) including those starting with /_spx. See try_files for nginx.

aftabnaveed commented 6 years ago

I tried to configure it with nginx and laravel I get the same error: here is the error:

| 172.19.0.1 - - [30/May/2018:16:49:08 +0000] "GET /_spx/?SPX_KEY=dev HTTP/1.1" 500 1470 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"

And my config

server {
    listen 80;
    listen [::]:80;

    server_name spx.local;
    root /srv/site/public;
    index index.php index.html;

    location @handler {
        rewrite / /index.php;
    }
    location ~* _spx {
        try_files $uri $uri/ @handler;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass ppm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}
NoiseByNorthwest commented 6 years ago

@aftabnaveed you do not need a dedicated vhost. Can you show me your application vhost config ? The only required thing is PHP being triggered on /_spx* request URIs, as long as _spx is the configured prefix (which is the case by default).

aftabnaveed commented 6 years ago

@NoiseByNorthwest

This is my vhost which points to a Laravel Application:

server {
    listen 80;
    listen [::]:80;

    server_name spx.local;
    root /srv/site/public;
    index index.php index.html;

    location @handler {
        rewrite / /index.php;
    }
    location ~* _spx {
        try_files $uri $uri/ @handler;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass ppm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

And my spx.ini

extension=spx.so
spx.http_enabled=1
spx.http_key="dev"
spx.http_ip_whitelist="172.19.0.1,10.0.75.1"

I tried both IPs separately but still it won't work.

SPX from my php.ini


SPX Support | enabled
-- | --
SPX Version | 0.2.4

SPX Support | enabled
-- | --
SPX Version | 0.2.4
aftabnaveed commented 6 years ago

OK it finally worked for my by fixing the location for _spx in my vhost. Here is the version which worked

server {
    listen 80;
    listen [::]:80;

    server_name spx.local;
    root /srv/laraveltest/public;
    index index.php index.html;

    location @handler {
        rewrite / /index.php;
    }

    location ~ /_spx {
        try_files $uri $uri/ @handler;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass ppm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

Now its time to play with it.

NoiseByNorthwest commented 6 years ago

@aftabnaveed thanks for your feedback.