我有一個Angular2/Angular應用程序在碼頭集裝箱內運行,並使用nginx來提供它。所以我的應用程序基地=/myapp /。使用基本URL即www.server.com/myapp或www.server.com/myapp/打應用程序時,一切工作正常Nginx的Angular2/Angular路線
events {
worker_connections 4096; ## Default: 1024
}
http {
include /etc/nginx/conf/*.conf;
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
underscores_in_headers on;
location /myapp {
# If you want to enable html5Mode(true) in your angularjs app for pretty URL
# then all request for your angularJS app will be through index.html
try_files $uri /myapp/index.html;
}
#Static File Caching. All static files with the following extension will be cached for 1 day
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1d;
}
## PROXIES ##
# location matcher for get requests. Any query params will be proxied using this location block
location = /myapp/api {
proxy_pass http://$hostname/api$is_args$query_string;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 120;
send_timeout 120;
}
# location matcher for post requests i.e. updateAsset. Any request with additional path params will be proxied using this location block
location ~ ^/myapp/api/(?<section>.*) {
proxy_pass http://$hostname/api/$section;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 120;
send_timeout 120;
}
}
}
我的應用程序有一些如其他路線/ myapp/page1或/ myapp/page2。在使用nodejs以開發模式提供應用程序時,可以擊中這些路線。但是,一旦我containerize它(集裝箱化不是問題),並使用nginx服務,然後我得到404時沒有找到當試圖訪問/ myapp/page1或/ myapp/page2。錯誤日誌輸出
2017/02/27 12:15:01 [error] 5#5: *3 open() "/usr/share/nginx/html/myapp/page1" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /myapp/page1 HTTP/1.1", host: "localhost"
我試圖在nginx的的conf文件映射我的所有的應用程序網址,但似乎沒有任何工作。我如何得到這個工作?
更新1條新增角路線
主要應用途徑:
import { Route } from '@angular/router';
import { MyAppComponent } from './index';
export const MyAppRoutes: Route[] = [
{
path: '',
component: MyAppComponent
}
];
1條路線:
import { Route } from '@angular/router';
import { Page1Component } from './index';
export const Page1Routes: Route[] = [
{
path:'page1',
component: Page1Component
}
];
不應該位置/ myapp位置/ myapp /? – MikeOne
以任一方式工作。我的問題是我不能讓/ myapp/page1映射到我的Angular2路由「page1」。這總是返回一個404 –
那麼..它應該重定向到index.html,然後你的路由器可以拿起它。它不會重定向到/myapp/index.html的事實表明該位置或try_files不正確。你嘗試添加關閉/? – MikeOne