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

msonntag nginx-forum at forum.nginx.org
Mon Aug 8 09:50:54 UTC 2016


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



More information about the nginx mailing list