You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Angular Universal至IIS时HTTPS访问遇Cloudflare 520错误

Angular Universal SSR部署到IIS后HTTPS访问出现Cloudflare 520错误
  • 部署状态:Angular Universal/SSR应用已构建并发布至IIS,通过localhost:120等本地端口访问完全正常。
  • 异常现象:配置域名并启用SSL证书后,访问https://example.com触发Cloudflare 520错误(提示“Web服务器返回未知错误”);禁用SSL证书后,http://example.com可正常访问。
  • 已做尝试:查阅大量资料,未找到可行解决方案,恳请社区提供帮助。

以下是我的server.ts文件:

import 'zone.js/node';

import { APP_BASE_HREF } from '@angular/common';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { existsSync } from 'fs';
import { join } from 'path';
import 'localstorage-polyfill'

import { AppServerModule } from './src/main.server';

// global['window'] = window;
// global['document'] = window.document;
// global['location'] = location;


// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  global['localStorage'] = localStorage;

  const server = express();

  let distFolder: string;
  const path1 = join(process.cwd(), 'projectName/browser');         // Running from the deployed version (production)
  const path2 = join(process.cwd(), 'dist/projectName/browser');    // Running from the source (local development)


  if (existsSync(path1)) {
    distFolder = path1;
  } else {
    distFolder = path2;
  } 

  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

function run(): void {
  const port = process.env['PORT'] || 4000;

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

内容的提问来源于stack exchange,提问作者Osama El-Masri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:07:47