You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Kaushal Shriyan <ka...@gmail.com> on 2023/02/25 10:43:17 UTC

[users@httpd] Render custom status to both browser and REST API Client

Hi,

Is there a way to configure apache httpd 2.4.55 as per the below flow when
the below conditions occurs

User -> Apache Web Server -> PHP-FPM Upstream server -> MySQL DB

*Condition 1 when MySQL DB is down*
When MySQL DB is down, httpd to render JSON output when invoking
http://mydomain.com/apis by the user using postman rest api client.
JSON Output
{"status_code": 500, "status" : "MySQL DB Server is down"}

HTML Output
HTML output when invoking http://mydomain.com/apis using client browser.

*Condition 2 when PHP-FPM is down*
When PHP-FPM Upstream server is down, httpd to render JSON output when
invoking http://mydomain.com/apis by the user using postman rest api client.
JSON Output
{"status_code": 502, "status": "php-fpm server is down"}}';

HTML Output
HTML output when invoking http://mydomain.com/apis using client browser.

Please guide me.

Best Regards,

Kaushal

Re: [users@httpd] Render custom status to both browser and REST API Client

Posted by Antony Stone <An...@apache.open.source.it>.
On Saturday 25 February 2023 at 11:43:17, Kaushal Shriyan wrote:

> Hi,
> 
> Is there a way to configure apache httpd 2.4.55 as per the below flow when
> the below conditions occurs
> 
> User -> Apache Web Server -> PHP-FPM Upstream server -> MySQL DB
> 
> *Condition 1 when MySQL DB is down*
> When MySQL DB is down, httpd to render JSON output when invoking
> http://mydomain.com/apis by the user using postman rest api client.
> JSON Output
> {"status_code": 500, "status" : "MySQL DB Server is down"}
> 
> HTML Output
> HTML output when invoking http://mydomain.com/apis using client browser.

I just realised that you might be saying here "I want JSON output if the 
client sends a request from postman, and I want HTML output if it's from a 
browser, but in both cases the URL is the same".

To achieve that your CGI script simply needs to look at the User-Agent in the 
incoming request, and send back the appropriate format depending on whether 
it's postman or a browser.

I do not know offhand what the User-Agent string is for postman, but it can't 
be hard to find out.


Antony.

-- 
The GNU General Public Licence was first published on this day in 1989
https://www.gnu.org/licences/gpl.html

                                                   Please reply to the list;
                                                         please *don't* CC me.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Render custom status to both browser and REST API Client

Posted by Antony Stone <An...@apache.open.source.it>.
On Saturday 25 February 2023 at 11:43:17, Kaushal Shriyan wrote:

> Hi,
> 
> Is there a way to configure apache httpd 2.4.55 as per the below flow when
> the below conditions occurs
> 
> User -> Apache Web Server -> PHP-FPM Upstream server -> MySQL DB
> 
> *Condition 1 when MySQL DB is down*
> When MySQL DB is down, httpd to render JSON output when invoking
> http://mydomain.com/apis by the user using postman rest api client.
> JSON Output
> {"status_code": 500, "status" : "MySQL DB Server is down"}
> 
> HTML Output
> HTML output when invoking http://mydomain.com/apis using client browser.
> 
> *Condition 2 when PHP-FPM is down*
> When PHP-FPM Upstream server is down, httpd to render JSON output when
> invoking http://mydomain.com/apis by the user using postman rest api
> client. JSON Output
> {"status_code": 502, "status": "php-fpm server is down"}}';
> 
> HTML Output
> HTML output when invoking http://mydomain.com/apis using client browser.

I would say that this depends primarily on what API (CGI script?) you are 
running on the Apache server.  Yes, such a script can generate the JSON you 
specified, provided:

a) it has some way of detecting when the PHP-FPM upstream server is "down"

b) it has some way of asking the PHP-FPM upstream server whether the MySQL DB 
server is "down"

You also don't say what you want to happen when neither of those servers is 
down, however I suspect that in this case you simply want Apache to act as a 
reverse proxy and feed back to the client whatever it got from the PHP-FPM 
server.  That should be a simple case for any CGI script which can do the 
exceptions outlined above.

Without a CGI script, Apache can only be a reverse proxy in the above 
configuration, and pass back to the client whatever it got from the PHP-FPM 
server, so if this is genuinely "down", there won't be anything to create the 
JSON you want.


Antony.

-- 
The GNU General Public Licence was first published on this day in 1989
https://www.gnu.org/licences/gpl.html

                                                   Please reply to the list;
                                                         please *don't* CC me.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org