Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - Nginx / Refresh shows bad page

Tags:

nginx

angular

I have an Angular 4 application deploy on a remote server with Nginx, and accessible with this address: http://xx.xx.xx.xx/app. The app works well, I can navigate in my website but when I refresh one page, for example http://xx.xx.xx.xx/app/page1, it displays the index.html page of nginx.

Nginx's pages are located in /usr/share/nginx/html, and my app's pages are located in /var/www/app.

nginx.conf

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;                                                */

        location /app {
            root /var/www;
            try_files $uri $uri/ /index.html;
        }
    }

It seems that the line root /var/www is not taken into account during a refresh.

I heard something about a bug with try_files, and it seems that I need to add a rewrite somewhere. Thanks for help.

like image 931
Benjamin Lucidarme Avatar asked Oct 18 '22 07:10

Benjamin Lucidarme


1 Answers

Try to add this in your AppModule:

import { HashLocationStrategy, LocationStrategy } from '@angular/common';

@NgModule({
    // ...
    providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
    // ...
})
export class AppModule {}
like image 130
Faly Avatar answered Oct 20 '22 23:10

Faly