NGINX配置AngularJS HTML5模式,包含模板和资源

时间:2014-11-07 21:10:09

标签: angularjs nginx

构建AngularJS应用程序并希望在NGINX中使用HTML5模式。是否重写了将所有流量定向到index.html。我遇到的问题是我的所有资产(图像,角度的模板文件等)。这些也被写回index.html,所以它打破了应用程序。有关nginx配置应该是什么样子的任何想法。我搜索时没有看到处理资产的任何内容。

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

3 个答案:

答案 0 :(得分:0)

听起来您的服务器根目录未正确定义。 您指定的位置规则会在默认为index.html之前尝试$ uri。因此,如果文件(css,js,jpg)存在于配置的根位置,则会在调用index.html之前提供该文件。

答案 1 :(得分:0)

这是一个非常脏的解决方案,所以它只适用于临时解决方案,我希望在迁移到Angular 1.4时这不是必需的

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
}

答案 2 :(得分:-1)

测试它

server {        
    server_name  xxxx.com

    index index.html;
    root /var/www;      

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

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

}