NGIX config for AngularJS HTML5 Mode with template

2019-08-04 13:13发布

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;
}

3条回答
神经病院院长
2楼-- · 2019-08-04 13:34

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.

查看更多
神经病院院长
3楼-- · 2019-08-04 13:51

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
}
查看更多
仙女界的扛把子
4楼-- · 2019-08-04 13:55

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;
    }

}
查看更多
登录 后发表回答