使用Nginx proxy_pass在同一服务器上托管多个MEAN Stack应用

时间:2020-08-10 07:22:10

标签: node.js angular nginx angular-cli nginx-reverse-proxy

我有两个MEAN(angular-8)堆栈应用程序,需要将它们部署在一台服务器中。我在Windows Server 2012环境中使用Nginx服务器。这两个应用程序可以在两个端口上完美运行:servername:port1,servername:port2。需要使用URL servername / app1,servername / app2访问它们。 这是我的Nginx配置

       location /app1 {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_pass   http://servername:port1/;
        proxy_set_header Host $host;
}
       location /app2 {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_pass   http://servername:port2/;
        proxy_set_header Host $host;
}

这是我的节点js代码部分

app.use( express.static( path.join( __dirname, '../dist/' ) ) );
app.use( require( './routes' ) );
app.get( '*', function ( req, res ) {
  return res.sendFile( path.join( __dirname, '../dist/index.html' ) )
} )
app.listen( port, () => console.log( 'listening on port ' + port ) );

angular.json中的配置

"build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        }

当我尝试使用servername / app1访问端口时,它显示空白页。因为未加载js文件和CSS文件。他们正试图从servername / styles.e9d2ee815fd5ee75430e.css而不是servername / app1 / styles.e9d2ee815fd5ee75430e.css访问静态文件

在角度构建期间,我尝试在nginx conf,deployUrl --app1中使用try_files,并尝试使用其他服务器(apache),但无济于事。

该问题如何解决?

我该如何解决这个问题?

0 个答案:

没有答案