请求协助将IIS Rewrite规则转换为Nginx规则
对应Nginx配置规则
没问题,我帮你把这些IIS重写规则转换成等价的Nginx配置,逻辑完全对齐:
第一个规则(对应IIS的wfq2020规则)
这个规则会匹配以auth、platform-admin、product或substation-admin开头的请求路径,将请求转发到https://google.com的对应路径上。
在Nginx里可以这样写:
location ~ ^/(auth|platform-admin|product|substation-admin)/ { proxy_pass https://google.com; proxy_set_header Host google.com; # 如需保留原请求的客户端IP等信息,可添加以下头配置 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
第二个规则(对应IIS的api.wfq2020规则)
这个规则匹配以wuneng-platform-web、wuneng-channel-web、wuneng-web、wuneng-user-web或mini-program开头的请求路径,转发到https://api.google.com的对应路径。
对应的Nginx配置:
location ~ ^/(wuneng-platform-web|wuneng-channel-web|wuneng-web|wuneng-user-web|mini-program)/ { proxy_pass https://api.google.com; proxy_set_header Host api.google.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
补充说明:重定向版本(如果需要浏览器跳转)
如果你的需求是让浏览器地址栏直接跳转到目标域名(而非服务器内部转发),可以用rewrite指令替代location,示例如下:
# 302临时重定向,换成`permanent`即为301永久重定向 rewrite ^/(auth|platform-admin|product|substation-admin)/(.*)$ https://google.com/$1/$2 redirect; rewrite ^/(wuneng-platform-web|wuneng-channel-web|wuneng-web|wuneng-user-web|mini-program)/(.*)$ https://api.google.com/$1/$2 redirect;
两者的核心区别:
proxy_pass是服务器后台转发请求,浏览器地址栏不会变化;rewrite ... redirect会触发浏览器跳转,地址栏显示目标域名。
你可以根据实际业务需求选择对应写法。
内容的提问来源于stack exchange,提问作者李昊天




