On my previous post Synology virtual sites for FileStation and others we could see how to create new sub domains under the main DNS name for your Synology to access the DSM applications like FileStation, Notes Station, and so on.
The configuration that is done creates a new full virtual site, on other words, all URL paths under the new sub domain are proxied to the redirected application. By other words, supposing that http://fs.domain.myds.me points to FileStation, all URL paths below to that URL are redirected to the FileStation site (for example http://fs.domain.myds.me/path1).
The following configuration is a bit different and it’s purpose is to redirect a URL path from the main site http://domain.myds.me to something else serving that path. For example, redirecting http://domain.myds.me/api to a backend Node.js REST server. The main reason to this is to allow Synology to host a site that uses, for example JQuery, or AngularJS, to also host the REST API that needs to be on the same domain due to the browser CORS protection. CORS protection means that a loading page from a site can only request data, by REST for example, from the origin web site. For example, my pages on http://domain.myds.me can call only REST services also on the same domain. It needs to be the same FQDN and PORT to the REST request be able to work. See more info at the Wikipedia page: Same-origin policy
And that’s because the above reason (CORS) that we need be able to proxy URL paths, and not full virtual sites, as the previous post as pointed out above.
To do this is quite simple, and the conditions are that the redirected path will be redirected from the main Synology user site.
So let’s redirect the path /api to a back end server:
Create a file named (for example) httpd-api-redirect.conf with the following content, replacing the backend_address and backend_port with the right information:
<Location /api> ProxyPass http://backend_address:backend_port/api </Location>
Save this file at /etc/httpd/sites-enabled-user, and stop and restart the Apache server:
(Edit: In DSM 6.0 the path is /usr/local/etc/httpd/sites-enabled )
To stop the Web Station: /sbin/initctl stop httpd-user To start the Web Station: /sbin/initctl start httpd-user
And that’s it, we can now host JQuery and/or Angular sites that use REST services hosted on the Synology Apache server.
There also some caveats regarding this configuration, if serving another site, not a REST api, like the full/relative paths issue with the proxied site, and also, regarding REST, the use of the Accept-origin header. But for the simplest purpose of hosting the site and the REST api under the same domain name, this works fine.