Angular Universal部署IIS后服务端渲染失效问题求助
Hey there! Let's work through this Angular SSR problem you're facing—since everything runs smoothly locally on localhost:4000, the issue is almost definitely tied to IIS configuration or deployment setup. Here are the key areas to check, step by step:
Install the IIS URL Rewrite Module
Angular SSR relies on URL rewriting to route all requests to your Node.js server. First, confirm that the URL Rewrite module is installed on your IIS server:- Open IIS Manager
- Go to your server's "Modules" section
- Look for "URL Rewrite" in the list. If it's missing, install it from the IIS web platform installer.
Verify Your
web.configConfiguration
A misconfiguredweb.configis one of the most common culprits. Make sure yours includes reverse proxy rules for dynamic content and excludes static files. Here's a working example for Angular SSR:<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <!-- Point to your compiled server.js file --> <add name="iisnode" path="dist/server.js" verb="*" modules="iisnode"/> </handlers> <rewrite> <rules> <!-- Route all dynamic requests to server.js --> <rule name="DynamicContent"> <match url="/*" /> <action type="Rewrite" url="dist/server.js"/> </rule> <!-- Serve static files directly (no rewrite) --> <rule name="StaticContent" stopProcessing="true"> <match url="([\S]+[.](html|css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot))" /> <action type="None" /> </rule> </rules> </rewrite> <!-- Set production environment for Node --> <iisnode node_env="production" /> </system.webServer> </configuration>Double-check that the
pathin the handlers section matches where yourserver.jslives in your deployed directory.Ensure Node.js and iisnode Are Installed
IIS needs the iisnode module to run Node.js applications, and your server must have a Node.js version compatible with Angular 5 (stick to the same major version you used locally to avoid conflicts). To confirm:- Open Command Prompt on the server and run
node -vto check the installed version. - Verify iisnode is installed by checking IIS Modules (same as the URL Rewrite step).
- Open Command Prompt on the server and run
Check Folder Permissions
The IIS application pool identity (usuallyIIS_IUSRSandIUSR) needs read/write access to your deployment folder, especially thedistdirectory. Without these permissions, the Node process might fail to read files or write logs.- Right-click your deployment folder > Properties > Security tab
- Add
IIS_IUSRSandIUSRif they aren't already listed - Grant them Read & Execute, List folder contents, and Read permissions (add Write if your app needs to generate files)
Inspect iisnode Logs for Specific Errors
iisnode generates detailed logs that can pinpoint exactly what's breaking. Look for theiisnodefolder in your deployment directory—inside you'll find error logs and stdout/stderr logs from the Node process. Common issues here include missing dependencies, port conflicts, or invalid file paths.Confirm Your Angular SSR Build is Complete
Make sure you deployed the full output of your SSR build. You should have two subfolders indist:browser(client-side assets) andserver(server-side bundle), plus theserver.jsentry file. If you're missing any of these, re-run your build commands locally:ng build --prod ng run your-project-name:server:productionThen re-deploy the updated
distfolder to IIS.
Start with these checks—chances are one of them will resolve your SSR issue. If you find a specific error in the logs, feel free to share more details and we can dig deeper!
内容的提问来源于stack exchange,提问作者kamalav

