2

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.

5
  • Do you really need to specify a base href here? Commented Feb 18, 2018 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 Commented Feb 18, 2018 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 Commented Feb 18, 2018 at 18:47
  • It worked! Do you want to post it as an answer so I can accept it? Commented Feb 18, 2018 at 19:15
  • 1
    I added an answer just to explain a bit more. Commented Feb 18, 2018 at 20:42

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Using --base-href /sub/ argument was causing my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.