我有一个angular2应用程序。如果我使用本地webpack dev server
,一切正常。
当我在nginx
后面的服务器上部署应用程序时,我可以使用应用程序链接进行导航。但是,如果我输入了浏览器网址栏的网址,我就会 404 Not Found
错误。
以下是网站的Nginx配置:
server {
listen 80;
server_name mydomain;
location /api {
proxy_pass http://mydomain:4000;
}
location /token-auth {
proxy_pass http://mydomain:4000;
}
location / {
root /www;
}
}
以下是我的申请详情:
<base href="/">
@NgModule({
imports: [
RouterModule.forRoot(appRoutes),
export const appRoutes:Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'rss', component: RssComponent, data: { section: 1 }, canActivate: [AuthGuard] },
{ path: '', redirectTo: '/login', pathMatch: 'full' }
];
@Component({
selector: 'my-app',
template: `
<div id="application">
<app-navigation-tabs></app-navigation-tabs>
</div>
<div>
<router-outlet></router-outlet>
</div>
`,
styleUrls: ['app.component.css']
})
<ul class="nav nav-tabs">
<li role="presentation" [ngClass]="{active: currentSection === 3}"><a [routerLink]="['/rss']" (click)="toggleSection(3)">RSS</a></li>
我不确定是Nginx配置错误,还是我的应用程序错误。我该如何解决?
答案 0 :(得分:3)
我通过将(function () {
angular.module('RoslpApp').controller('ResetPassword', ['$ResetPasswordService','$scope', '$translatePartialLoader', '$translate', function ($ResetPasswordService, $scope, $translatePartialLoader, $translate) {
alert("Works");
$scope.ResetPassword = function () {
var sub = {
mobilenumber: $scope.updateID,
dob: $scope.updateName
};
alert("does not works");
var servCall = ResetPasswordService.ResetPassword(sub);
servCall.then(function (data) {
}, function (data) {
alert(JSON.stringify(data.data));
});
}
}]);
})();
添加到路由器来解决了我的问题:
useHash
答案 1 :(得分:0)
我是一个阿帕奇家伙(不要怪我,这不是我的错,而是我的系统管理员)。但对于使用History
API的每个单页应用,您需要将请求重写为index.html
。我发现this用于nginx pushstate的Google搜索似乎与nginx一起使用。所以我想如果你改变它,它应该工作。
server {
listen 80;
server_name mydomain;
location /api {
proxy_pass http://mydomain:4000;
}
location /token-auth {
proxy_pass http://mydomain:4000;
}
location ~ ^/.+/$ {
root /www;
rewrite .* /index.html last;
}
}
编辑:这个解决方案似乎不起作用,但如果我发现某些内容,我会稍微挖掘一下并进行更新。否则删除。