1

I want to point sub.example.com to example.com/sub (without actually redirecting). I've achieved this with following nginx config:

server {
    listen 80;
    server_name ~^(.*)\.example\.com$ ;
    root /var/www/html/example.com/public_html/sub;
}

The problem is, I have an Angular app on sub.example.com and it tries to load its js resources from the below path and fails to do so (404 Not found):

http://sub.example.com/sub/inline.bundle.js
http://sub.example.com/sub/vendor.bundle.js
...

To make a build I'm using following command:

ng build --base-href /sub/

How to resolve this? Is this Angular issue or can I fix it with nginx config? I've tried the proxy_pass but with no success.

Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
  • Do you really need to specify a base href here? – David Feb 18 '18 at 18:35
  • I think so, at least I had to use it at the beginning when I was accessing the app through example.com/sub, its source location hasn't changed, I just want to access it through sub.example.com now – Adam Bardon Feb 18 '18 at 18:45
  • 1
    I'd say to try without. Since you've got nginx redirections, it should not be needed anymore. Otherwise, it if still does not work after removing it, have a look at the deploy-url option – David Feb 18 '18 at 18:47
  • It worked! Do you want to post it as an answer so I can accept it? – Adam Bardon Feb 18 '18 at 19:15
  • 1
    I added an answer just to explain a bit more. – David Feb 18 '18 at 20:42

2 Answers2

4

Specifying base-href is only needed for the router if your app is not served from the root location, e.g. http://example.com/sub

In your case, you added nginx rules so that content can be served from a sub folder, but using a "root" location (http://sub.example.com)

So from an angular point of view, everything is like it's served from a root folder, meaning there is no need to specify base-href

David
  • 33,444
  • 11
  • 80
  • 118
0

I had that problem when I was implementing internationalization of Angular application that needed to be served from different sub-directories for each language. Check my solution on the topic.

nyxz
  • 6,918
  • 9
  • 54
  • 67