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

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:

1. Verify if 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.
2. Configure 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 like text/css or text/javascript if your relative URLs are in those files—otherwise, the module will ignore them.
  • sub_filter_once: Defaults to on, which only replaces the first match per response. Set to off if 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.
3. Test and reload Nginx

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.

Troubleshooting if It's Not Working
  • Double-check the nginx -V output to confirm the module is loaded.
  • Ensure your sub_filter strings are exact matches—even extra spaces can break the replacement.
  • Verify sub_filter_types includes 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 22:53:16