<div dir="ltr">Hi,<div><br></div><div>Checking in again if someone can help me with my earlier post to this mailing list? </div><div><br></div><div><div class="gmail_quote"><div>I have a follow up question, when the user invokes -> <a href="http://mydomain.com/api/v1/*" target="_blank">http://mydomain.com/apis</a> -> Nginx Webserver -> Drupal 9 Core CMS -> PHP-FPM backend server. <br></div><div><br>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. <br> <br> "type" => "/problems/API-saving-error",<br> "title" => $this->t("Issue occured while saving the API."),<br> "detail" => $this->t("There are some wrong inputs passed to DB which caused this issue."), </div><div><br></div><div>I have the below settings in nginx conf file</div><div><br></div><div> error_page 500 /error-500.html;<br> location = /error-500.html {<br> root /var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;<br> }<br> </div><div> error_page 503 /error-503.html;<br> location = /error-503.html {<br> root /var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;<br> }</div><div><br></div><div>I am trying to set the below location and try_files directive block in nginx.conf file <br> location /apis {</div> try_files $uri $uri/ /path/to/api/handler; (This part is not clear with me)<br> } </div><div class="gmail_quote"><br></div><div class="gmail_quote">to send back the below response to the client browser instead of rendering the /error-500.html file contents. <br> </div><div class="gmail_quote"><div> "type" => "/problems/API-saving-error",<br> "title" => $this->t("Issue occured while saving the API."),<br> "detail" => $this->t("There are some wrong inputs passed to DB which caused this issue."), </div><div><br></div><div><span class="gmail-im" style="color:rgb(80,0,80)"><div>Please guide me. Thanks in advance. I look forward to hearing from you. </div><div><br></div></span><div>Best Regards,</div><div><br></div><div>Kaushal</div></div></div></div><div><br></div></div><div dir="ltr"><div dir="ltr"><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 8, 2022 at 1:25 PM Kaushal Shriyan <<a href="mailto:kaushalshriyan@gmail.com" target="_blank">kaushalshriyan@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 8, 2022 at 11:20 AM Maxim Dounin <<a href="mailto:mdounin@mdounin.ru" target="_blank">mdounin@mdounin.ru</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello!<br>
<br>
On Mon, Nov 07, 2022 at 08:59:51PM +0530, Kaushal Shriyan wrote:<br>
<br>
> I am running the nginx version: nginx/1.22 as a reverse proxy server on<br>
> CentOS Linux release 7.9.2009 (Core). Is there a way to return json<br>
> response when i hit <a href="http://mydomain.com/api/v1/*" rel="noreferrer" target="_blank">http://mydomain.com/api/v1/*</a> instead of the html<br>
> response.<br>
> <br>
> location /api/v1/* {<br>
> internal;<br>
> add_header 'Content-Type' 'application/json charset=UTF-8';<br>
> <br>
> error_page 502 '{"error": {"status_code": 502,"status": "Bad<br>
> Gateway"}}';<br>
> }<br>
> <br>
> But whenever I try to send a request to /api/v1/users via curl I get the<br>
> HTML source code in response instead of JSON response.<br>
> <br>
> Please guide me. Thanks in advance. I look forward to hearing from you.<br>
<br>
First of all, the "/api/v1/*" does not look like a valid prefix. <br>
Note that "*" is not special in prefix locations, so you need just <br>
"/api/v1/" to match relevant requests. See <br>
<a href="http://nginx.org/r/location" rel="noreferrer" target="_blank">http://nginx.org/r/location</a> for details.<br>
<br>
Further, trying to overwrite the Content-Type returned with the <br>
add_header directive will fail, as nginx will still send the <br>
Content-Type as set by the "types" and "default_type" directive. <br>
See <a href="http://nginx.org/r/types" rel="noreferrer" target="_blank">http://nginx.org/r/types</a> for details. To set charset you can <br>
use the "charset" directive, see <a href="http://nginx.org/r/charset" rel="noreferrer" target="_blank">http://nginx.org/r/charset</a>.<br>
<br>
Additionally, the "error_page" directive does not accept strings. <br>
If you want to return a string directly from nginx in case of an error, <br>
you can use error_page with an internal URI, and then use the <br>
"return" directive to return the text. See <br>
<a href="http://nginx.org/r/error_page" rel="noreferrer" target="_blank">http://nginx.org/r/error_page</a> and <a href="http://nginx.org/r/return" rel="noreferrer" target="_blank">http://nginx.org/r/return</a> for <br>
details.<br>
<br>
And, as already pointed out in another response, the "internal" <br>
directive implies that the location won't be accessible from the <br>
outside, so you have to remove it. See <br>
<a href="http://nginx.org/r/internal" rel="noreferrer" target="_blank">http://nginx.org/r/internal</a> for details.<br>
<br>
Further, your location does not contain actual proxying - so you <br>
probably want to add some, or all matched requests will be <br>
considered requests to static files.<br>
<br>
Summing the above, valid configuration will look like:<br>
<br>
location /api/v1/ {<br>
error_page 502 /error502;<br>
proxy_pass ...<br>
}<br>
<br>
location = /error502 {<br>
default_type text/json;<br>
charset UTF-8;<br>
charset_types text/json;<br>
return 200 '{"error": {"status_code": 502,"status": "Bad Gateway"}}';<br>
}<br>
<br>
It might be a good idea to start with reading some introductory <br>
articles at <a href="http://nginx.org/en/docs/" rel="noreferrer" target="_blank">http://nginx.org/en/docs/</a>, notably:<br>
<br>
Beginner’s Guide<br>
<a href="http://nginx.org/en/docs/beginners_guide.html" rel="noreferrer" target="_blank">http://nginx.org/en/docs/beginners_guide.html</a><br>
<br>
and<br>
<br>
How nginx processes a request<br>
<a href="http://nginx.org/en/docs/http/request_processing.html" rel="noreferrer" target="_blank">http://nginx.org/en/docs/http/request_processing.html</a><br>
<br>
Hope this helps.<br>
<br>
-- <br>
Maxim Dounin<br>
<a href="http://mdounin.ru/" rel="noreferrer" target="_blank">http://mdounin.ru/</a><br>
_______________________________________________<br>
nginx mailing list -- <a href="mailto:nginx@nginx.org" target="_blank">nginx@nginx.org</a><br>
To unsubscribe send an email to <a href="mailto:nginx-leave@nginx.org" target="_blank">nginx-leave@nginx.org</a></blockquote><div><br></div><div>Thanks Maxim for a detailed explanation and I am currently reading the documentation. </div><div>I have a follow up question, when the user invokes -> <a href="http://mydomain.com/api/v1/*" target="_blank">http://mydomain.com/apis</a> -> Nginx Webserver -> Drupal 9 Core CMS -> PHP-FPM backend server. <br></div><div><br>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. <br> <br> "type" => "/problems/API-saving-error",<br> "title" => $this->t("Issue occured while saving the API."),<br> "detail" => $this->t("There are some wrong inputs passed to DB which caused this issue."), </div><div><br></div><div>I have the below settings in nginx conf file</div><div><br></div><div> error_page 500 /error-500.html;<br> location = /error-500.html {<br> root /var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;<br> }<br> </div><div> error_page 503 /error-503.html;<br> location = /error-503.html {<br> root /var/www/html/gsmamarketplace/web/servererrorpages/error-pages-500-503/html;<br> }</div><div><br></div><div>I am trying to set the below location and try_files directive block in nginx.conf file <br> location /apis {</div> try_files $uri $uri/ /path/to/api/handler; (This part is not clear with me)<br> } </div><div class="gmail_quote"><br></div><div class="gmail_quote">to send back the below response to the client browser instead of rendering the /error-500.html file contents. <br> </div><div class="gmail_quote"><div> "type" => "/problems/API-saving-error",<br> "title" => $this->t("Issue occured while saving the API."),<br> "detail" => $this->t("There are some wrong inputs passed to DB which caused this issue."), </div><div><br></div><div><div>Please guide me. Thanks in advance. I look forward to hearing from you. </div><div><br></div><div>Best Regards,</div><div><br></div><div>Kaushal</div></div><div></div></div></div>
</blockquote></div></div>