Paul Guichon
Paul Guichon

Reputation: 31

config angular-cli nginx subdirectory /example

I want to serve a angular application at the url my_url/exemple.

this my nginx config :

location /example/ {
    alias /pwd/example/dist/;

    try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;

}

and this is my build command : ng build -bh example

when I go to the my_url/example I have a redirect to my_url/example/example and of course angular said me : Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'example'

If the location is / is work : I can go to my_url/example and the application work. But in production / is taken by a django application.

What can I do ?

Upvotes: 3

Views: 4850

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

To allow this location to handle the URI /example without a trailing /, you could remove the trailing / from the location and alias statement.

Also, your try_files statement contains a number of strange terms, and two default actions where only one is permitted. See this document for details.

If the index.html file is at /pwd/example/dist/index.html, it needs to be represented by the URI /example/index.html.

Suggested configuration:

location /example {
    alias /pwd/example/dist;
    try_files $uri $uri/ /example/index.html;
}

If your application works at / at the moment, but not at /example/, you may need to look at the application, and ensure that it pulls its resource files (css, js) with the correct URI prefix also.

Upvotes: 5

Related Questions