如何正确配置nginx以与NG Serve和Angular CLI一起使用?

梅林

我一直在尝试研究Nginx,以在localhost:4200上配置Angular 5 ng服务的代理,但是只能得出为已构建项目提供服务的结果。我从这项研究中发现的配置“有些”有效,但是导致白页未加载任何数据:

dev:12 GET http://192.168.1.84/inline.bundle.js net::ERR_ABORTED
dev:12 GET http://192.168.1.84/polyfills.bundle.js net::ERR_ABORTED
dev:12 GET http://192.168.1.84/styles.bundle.js net::ERR_ABORTED
dev:12 GET http://192.168.1.84/vendor.bundle.js net::ERR_ABORTED
dev:12 GET http://192.168.1.84/main.bundle.js net::ERR_ABORTED

看来看不到ng serve提供的文件,但至少到达了该项目的index.html页面。这是我当前正在使用的配置:

location /dev {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        rewrite ^/(.*)$ /$1 break;
        proxy_set_header Host localhost;
        proxy_pass http://localhost:4200;
}

我应该在/ dev配置中添加什么?

内森·朋友

首先,检查Angular应用的<base>标签-它需要与应用的新位置匹配。因此,例如,如果您要通过Nginx在托管应用程序https://localhost/dev/,则<base>标签必须为:

<base href="/dev/">

您可以在应用程序的中找到此标签index.html

其次,nginx不会自动代理ng serve用于实时重载功能的所有流量您可以将这些额外的流量添加到您的代理中nginx.conf

location ^~ /sockjs-node/ {

    proxy_pass http://127.0.0.1:4200;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;

    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;
}

假设ng serve您的应用程序托管在端口4200(默认设置)上。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将ng-bootstrap与angular-cli 1.0.0-beta.18一起使用

angular-cli如何与ES6模块一起使用?

Vue CLI和Vuetify如何与本地Roboto字体一起使用

如何使Typescript与SystemJS和Angular一起使用?

如何正确配置Spring Boot项目以与Quartz和MySQL一起使用?

如何使uwsgi与nginx和Django 1.7一起使用?

Angular `ng serve --prod` 和 `ng serve -c production` 一样吗?

如何在angular 1中一起使用ng-if和ng-options

如何在没有构建“错误”的情况下将Angular-CLI与Visual Studio 2017一起使用

如何更改与Angular CLI一起使用的软件包管理器(例如,使用ngpm代替npm进行`ng add`)?

如何创建可以与“ ng serve”一起运行的Angular组件库?

为什么 HHVM 和 NGINX 一起使用?

将Laravel和Ionic与Nginx路由一起使用

意外令牌<与React Router和Nginx一起使用

将VuePress与docker和NGINX传递一起使用

配置IntelliJ以与JDK 14和记录一起使用

配置Spark以与Jupyter Notebook和Anaconda一起使用

ESLint与Arbnb配置和Facebook Flow一起使用

配置phantomjs以与webpack和karma nwb一起使用

配置Aptana 3以与PHP和XAMPP一起使用

配置npgsql以与.NET 3.5和EF一起使用

IDisposable如何与使用和退货一起使用?

如何使用lldb和VSCode使调试器保持活动状态,以将其与bitcoind和bitcoin-cli一起使用?

无法将ng-bootstrap与Webpack和Angular2一起使用

如何在 Angular 中将 patchValue 与 FormArray 一起使用?

如何使lodash与Angular JS一起使用?

如何在 Angular 中将 ngFor 与 Observable 一起使用?

什么是fileAPI?如何将其与angular一起使用?

angular 2如何与jquery一起使用?