10

New to Angular. App works fine if deployed in nginx /var/www/mydomain.com/html. But I want to deploy it in /var/www/mydomain.com/html/myapp folder. I setup nginx available sites to this folder and index.html works fine. But relative paths in Angular app (e.g., images/mypic.png) being attempted to be retrieved from /var/www/mydomain.com/html/images folder (hence 404 error code) instead of /var/www/mydomain.com/html/myapp/images folder. How do I set a url prefix /myapp globally in Angular so all relative paths have this prefix. I have seen some answers here but they require changes in the component code. Isn't there a way to made this setting at deployment time so the same dist can be deployed in any path?

5
  • You can configure the path in angular.json or angular-cli.json file. Give your path for key "outputPath". eg: "outputPath": "myapp" Commented Jun 16, 2018 at 15:13
  • Take a look at this answer. Commented Jun 16, 2018 at 15:40
  • Yes that seems like the only choice. I was hoping I could make that change in the dist after build. Thanks. Commented Jun 16, 2018 at 15:43
  • If you use Webpack, this plugin appears to work. Commented Jun 16, 2018 at 16:31
  • Did you try setting proper baseRef in index.html or angular.json? It has to be set properly in order for assets to load. Commented Jun 16, 2018 at 16:32

2 Answers 2

9

###On angular.json > build > options configuration add this line with target sub directory

        "baseHref" : "/v2/",

**like this **

    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "baseHref" : "/v2/",
Sign up to request clarification or add additional context in comments.

1 Comment

Option deployUrl is now deprecated. (See stackoverflow.com/questions/71695674/…)
7

Use the following command, here /myapp/ is the relative path to the root site.

ng build --prod --base-href /myapp/

1 Comment

Great - make sure to include the leading and lagging '/' around your subfolder otherwise it may lead to weird routing behaviours

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.