Return proper status codes (404, 302) from client-side Single Page Application

B.R. reallfqq-nginx at yahoo.fr
Mon Aug 8 10:03:24 UTC 2016


I find it strange you oppose HTTP 404 with 'a proper status code': 404 is a
'proper' status code.
I find it even stranger you want to lie to search engines crawlers about
the existence of your resource.

That said, you can craft/modify upstream requests in the proxy module with
directives such as:
- proxy_method
<http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_method>
- proxy_set_header
<http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header>
- proxy_set_body
<http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_body>
(basically RTFM, proxy module)

When dealing with upstream responses content, you can use variabels such as:
- $upstream_status
<http://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_status>
(basically RTFM, upstream module)

And for setting answer content conditionally, there is the map
<http://nginx.org/en/docs/http/ngx_http_map_module.html> module you will
find most helpful.
---
*B. R.*

On Mon, Aug 8, 2016 at 11:50 AM, msonntag <nginx-forum at forum.nginx.org>
wrote:

> Hello,
>
> I have the following scenario:
>
> - Client: AngularJS-based SPA running on www.example.com
> - Backend: API running on api.example.com
>
> Both live in one nginx instance in two separate "server" environments.
>
> - Browsing to www.example.com/items/1 launches the Angular app
> - App sends request to api.example.com/items/1
> - If item 1 does not exist, API returns 404 status code
> - Client app can now show soft 404 error page, all fine
> - But for crawlers/search engines, I want to return a proper HTTP status
> code. Same goes for redirect to item’s canonical URL if that is necessary.
>
> So my idea was to do sth like this:
>
> - If request URL matches www.example.com/items/<id>, check existence of
> item
> by sending a HEAD request to api.example.com/items/<id>
> - If request returns 404, return proper status code and error page
> - If request returns 200, do nothing and just serve the Angular app
>
> Is there any way to do this with (plain) nginx and if so–how could it be
> done specifically?
>
> Thanks for any hints :)
>
> Posted at Nginx Forum: https://forum.nginx.org/read.
> php?2,268827,268827#msg-268827
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20160808/de85558f/attachment.html>


More information about the nginx mailing list