Windows平台下Nginx安装http_sub_module模块及相对URL重定向问题咨询
Got it, let's break this down for Windows-specific Nginx setup with the http_sub_module to handle relative URL redirection/replacement. I've walked through this a few times, so here's what you need to do:
http_sub_module is already included in your Nginx build First off, check if your existing Nginx installation has the module enabled. Open Command Prompt (or PowerShell), navigate to your Nginx install directory (usually C:\nginx), and run:
nginx -V
Look for --with-http_sub_module in the configure arguments section of the output.
- If you see it: Great, skip straight to configuration.
- If you don't: The official precompiled Nginx builds for Windows do include this module by default, so I'd recommend grabbing the latest stable version and replacing your current installation. If you absolutely need a custom build, you'll have to compile Nginx from source using MSYS2, but that's way more work than necessary for most cases.
http_sub_module for relative URL replacement This module works by replacing matching strings in the response body—perfect for fixing relative URLs. Here's a practical configuration example you can drop into your nginx.conf (inside the server block):
server { listen 80; server_name your-local-domain.test; # Replace with your actual domain/localhost # Define which MIME types to process (default is only text/html) sub_filter_types text/html text/css text/javascript application/json; # Set to 'off' if you need to replace ALL instances of the string, not just the first sub_filter_once off; # Example 1: Fix relative asset paths # Replaces all "/assets/" in responses with "/static/assets/" sub_filter '/assets/' '/static/assets/'; # Example 2: Redirect specific relative links # Replaces links pointing to "/old-section/" with "/new-section/" sub_filter '<a href="/old-section/"' '<a href="/new-section/"'; # Optional: Enable case-insensitive matching (Nginx 1.9.4+) sub_filter_case_insensitive on; location / { root html; index index.html index.htm; } }
Key Notes on the Configuration:
sub_filter_types: Don't forget to add types liketext/cssortext/javascriptif your relative URLs are in those files—otherwise, the module will ignore them.sub_filter_once: Defaults toon, which only replaces the first match per response. Set tooffif you have multiple instances of the same relative URL to fix.sub_filter_case_insensitive: Useful if your URLs might have mixed case (e.g.,/Assets/vs/assets/) and you want consistent replacement.
Before applying changes, validate your config to avoid crashes:
nginx -t
If you see test is successful, reload Nginx to apply the new configuration:
nginx -s reload
If you installed Nginx as a Windows service, you can also restart it via the Services manager.
- Double-check the
nginx -Voutput to confirm the module is loaded. - Ensure your
sub_filterstrings are exact matches—even extra spaces can break the replacement. - Verify
sub_filter_typesincludes the MIME type of the content you're trying to modify (use browser dev tools to check the Content-Type header of the response). - If you're testing locally, clear your browser cache—old cached responses won't show the changes.
内容的提问来源于stack exchange,提问作者DevOps

