Welcome! Log In Create A New Profile

Advanced

Re: nginx returns html instead of json response

All files from this thread

File Name File Size   Posted by Date  
nginxtest.conf 3.6 KB open | download kaushalshriyan 11/07/2022 Read message
nginxtest.conf 3.6 KB open | download kaushalshriyan 11/08/2022 Read message
nginxtest.conf 3.6 KB open | download kaushalshriyan 11/09/2022 Read message
nginxtest.conf 3.6 KB open | download kaushalshriyan 11/14/2022 Read message
nginxtest.conf 3.6 KB open | download kaushalshriyan 11/22/2022 Read message
November 08, 2022 02:56AM
On Tue, Nov 8, 2022 at 11:20 AM Maxim Dounin <mdounin@mdounin.ru> wrote:

> Hello!
>
> On Mon, Nov 07, 2022 at 08:59:51PM +0530, Kaushal Shriyan wrote:
>
> > I am running the nginx version: nginx/1.22 as a reverse proxy server on
> > CentOS Linux release 7.9.2009 (Core). Is there a way to return json
> > response when i hit http://mydomain.com/api/v1/* instead of the html
> > response.
> >
> > location /api/v1/* {
> > internal;
> > add_header 'Content-Type' 'application/json charset=UTF-8';
> >
> > error_page 502 '{"error": {"status_code": 502,"status": "Bad
> > Gateway"}}';
> > }
> >
> > But whenever I try to send a request to /api/v1/users via curl I get the
> > HTML source code in response instead of JSON response.
> >
> > Please guide me. Thanks in advance. I look forward to hearing from you.
>
> First of all, the "/api/v1/*" does not look like a valid prefix.
> Note that "*" is not special in prefix locations, so you need just
> "/api/v1/" to match relevant requests. See
> http://nginx.org/r/location for details.
>
> Further, trying to overwrite the Content-Type returned with the
> add_header directive will fail, as nginx will still send the
> Content-Type as set by the "types" and "default_type" directive.
> See http://nginx.org/r/types for details. To set charset you can
> use the "charset" directive, see http://nginx.org/r/charset.
>
> Additionally, the "error_page" directive does not accept strings.
> If you want to return a string directly from nginx in case of an error,
> you can use error_page with an internal URI, and then use the
> "return" directive to return the text. See
> http://nginx.org/r/error_page and http://nginx.org/r/return for
> details.
>
> And, as already pointed out in another response, the "internal"
> directive implies that the location won't be accessible from the
> outside, so you have to remove it. See
> http://nginx.org/r/internal for details.
>
> Further, your location does not contain actual proxying - so you
> probably want to add some, or all matched requests will be
> considered requests to static files.
>
> Summing the above, valid configuration will look like:
>
> location /api/v1/ {
> error_page 502 /error502;
> proxy_pass ...
> }
>
> location = /error502 {
> default_type text/json;
> charset UTF-8;
> charset_types text/json;
> return 200 '{"error": {"status_code": 502,"status": "Bad Gateway"}}';
> }
>
> It might be a good idea to start with reading some introductory
> articles at http://nginx.org/en/docs/, notably:
>
> Beginner’s Guide
> http://nginx.org/en/docs/beginners_guide.html
>
> and
>
> How nginx processes a request
> http://nginx.org/en/docs/http/request_processing.html
>
> Hope this helps.
>
> --
> Maxim Dounin
> http://mdounin.ru/
> _______________________________________________
> nginx mailing list -- nginx@nginx.org
> To unsubscribe send an email to nginx-leave@nginx.org


Thanks Maxim for a detailed explanation and I am currently reading the
documentation.
I have a follow up question, when the user invokes ->
http://mydomain.com/apis http://mydomain.com/api/v1/* -> Nginx Webserver
-> Drupal 9 Core CMS -> PHP-FPM backend server.

Nginx should present the below info on 500 ISE error conditions for /apis
and /apis/* The below message sends back the response to Nginx web server
to render it to the client browser instead of the /error-500.html file
contents.

"type" => "/problems/API-saving-error",
"title" => $this->t("Issue occured while saving the
API."),
"detail" => $this->t("There are some wrong inputs passed
to DB which caused this issue."),

I have the below settings in nginx conf file

error_page 500 /error-500.html;
location = /error-500.html {
root
/var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;
}

error_page 503 /error-503.html;
location = /error-503.html {
root
/var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;
}

I am trying to set the below location and try_files directive block in
nginx.conf file
location /apis {
try_files $uri $uri/ /path/to/api/handler; (This part is not
clear with me)
}

to send back the below response to the client browser instead of rendering
the /error-500.html file contents.

"type" => "/problems/API-saving-error",
"title" => $this->t("Issue occured while saving the
API."),
"detail" => $this->t("There are some wrong inputs passed
to DB which caused this issue."),

Please guide me. Thanks in advance. I look forward to hearing from you.

Best Regards,

Kaushal
_______________________________________________
nginx mailing list -- nginx@nginx.org
To unsubscribe send an email to nginx-leave@nginx.org
Attachments:
open | download - nginxtest.conf (3.6 KB)
Subject Author Posted

nginx returns html instead of json response

kaushalshriyan November 07, 2022 10:32AM

Re: nginx returns html instead of json response

Dan G. Switzer, II November 07, 2022 11:10AM

Re: nginx returns html instead of json response Attachments

kaushalshriyan November 07, 2022 12:48PM

Re: nginx returns html instead of json response

Dan G. Switzer, II November 07, 2022 01:22PM

Re: nginx returns html instead of json response

Maxim Dounin November 08, 2022 12:50AM

Re: nginx returns html instead of json response Attachments

kaushalshriyan November 08, 2022 02:56AM

Re: nginx returns html instead of json response Attachments

kaushalshriyan November 09, 2022 01:16PM

Re: nginx returns html instead of json response

Francis Daly November 11, 2022 04:08AM

Re: nginx returns html instead of json response

kaushalshriyan November 11, 2022 05:52AM

Re: nginx returns html instead of json response Attachments

kaushalshriyan November 14, 2022 09:56AM

Re: nginx returns html instead of json response

kaushalshriyan November 16, 2022 12:00PM

Re: nginx returns html instead of json response

Sergey A. Osokin November 16, 2022 04:42PM

Re: nginx returns html instead of json response

kaushalshriyan November 16, 2022 08:20PM

Re: nginx returns html instead of json response

Francis Daly November 17, 2022 12:28PM

Re: nginx returns html instead of json response

kaushalshriyan November 18, 2022 08:40AM

Re: nginx returns html instead of json response

Francis Daly November 18, 2022 11:06AM

Re: nginx returns html instead of json response

kaushalshriyan November 18, 2022 12:42PM

Re: nginx returns html instead of json response

Francis Daly November 18, 2022 01:32PM

Re: nginx returns html instead of json response

kaushalshriyan November 19, 2022 10:42AM

Re: nginx returns html instead of json response

Francis Daly November 21, 2022 02:22PM

Re: nginx returns html instead of json response Attachments

kaushalshriyan November 22, 2022 09:24AM

Re: nginx returns html instead of json response

Francis Daly November 23, 2022 12:50PM

Re: nginx returns html instead of json response

kaushalshriyan November 23, 2022 12:58PM

Re: nginx returns html instead of json response

Francis Daly November 23, 2022 01:10PM

Re: nginx returns html instead of json response

kaushalshriyan November 29, 2022 11:30AM

Re: nginx returns html instead of json response

kaushalshriyan November 30, 2022 12:48PM

Re: nginx returns html instead of json response

Francis Daly December 01, 2022 02:48PM

Re: nginx returns html instead of json response

kaushalshriyan November 23, 2022 12:52PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 255
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready