NGIX config for AngularJS HTML5 Mode with template

2019-08-04 13:22发布

问题:

Building an AngularJS app and want to use HTML5 mode with NGINX. Did a rewrite to direct all traffic to index.html. Problem I'm running into is all my assets (images, template files for angular, etc). Those are getting written back to index.html as well so it breaks the app. Any ideas on what the nginx config should look like. I'm not seeing anything that handles assets when I search.

location / {
    try_files $uri $uri/ /index.html;
}

回答1:

It sounds like your server root is not properly defined. The location rule you specified tries the $uri before defaulting to index.html. Therefore if the file (css, js, jpg) exists in the configured root location, it will be served before index.html is called.



回答2:

This is a really dirty solution, so it's only for a temporary solution, i hope when migrate to Angular 1.4 this will not necesary

server {
    listen 0.0.0.0:12345;

    location / {
        root /home/zdwolfe/src/angularAWS/app;
        try_files $uri $uri/ /index.html =404;
    }
    error_page 404 /index.html
}


回答3:

test it


server {        
    server_name  xxxx.com

    index index.html;
    root /var/www;      

    location ~ ^/assets|templates|images/ {         
        break;
    }

    location / {            
        try_files $uri $uri/ /index.html;
    }

}