如何使用 Angular 2 PathLocationStrategy 配置 NGINX proxy_pass

如何使用 Angular 2 PathLocationStrategy 配置 NGINX proxy_pass

我目前使用 PM2 为我的 Angular 2 前端提供服务,运行在 IP 地址 上http://198.xxx.xxx.xx:8080/。使用 Angular 2 时HashLocationStrategy,非根路径可以正常重新加载。使用 时PathLocationStrategy,刷新不起作用。有没有办法将路径通过 中的 ,proxy_pass以便location /像 这样的路径/about解析为/aboutAngular 2 路由器提供的 ?

我当前的设置将成功地从应用程序转到//about但如果我刷新/about,它会返回一个空白页:

upstream app_server {
    server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
    listen   80;
    root http://198.xxx.xxx.xx:8080/;
    server_name _;
    index index.htm index.html;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
        try_files $uri @app;
    }

    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://198.xxx.xxx.xx:8080/;
    }

    location ^~ /api/ {
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server/api/;
    }
}

以下是我访问/,然后单击链接访问/about,然后刷新/about页面的 PM2 日志:

dailydownbeat-0 [Sat Mar 05 2016 14:59:38 GMT-0500 (EST)] "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 14:59:38 GMT-0500 (EST)] "GET /polyfills.c4ffdebfdb7d4e9ee964.bundle.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 14:59:38 GMT-0500 (EST)] "GET /main.4916a64a8641d387872b.bundle.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 14:59:39 GMT-0500 (EST)] "GET /assets/img/dailydownbeat_icon.svg" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 14:59:39 GMT-0500 (EST)] "GET /a35720c2fed2c7f043bc7e4ffb45e073.woff" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 15:00:02 GMT-0500 (EST)] "GET /about" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56"
dailydownbeat-0 [Sat Mar 05 2016 15:00:02 GMT-0500 (EST)] "GET /about" Error (404): "Not found"

相关内容