文章目录
业务场景
生产环境DMZ区有一台nginx代理服务器,唯一可以链接外网的服务器,生产服务器集群不可以访问外网。但业务需要访问微信小程序API地址:https://api.weixin.qq.com ,只有通过DMZ去nginx来代理,因为只有一个可用端口来代理,网络组是不同意再开新端口来代理。所以需要添加文根来代理外网地址,配置过程中遇到了很多问题,记录下来
1. 检查Nginx路径转发逻辑
1.1 确认proxy_pass
末尾斜杠
在路径代理中,proxy_pass
末尾的斜杠(/
)决定了路径的拼接方式:
-
错误配置(路径拼接错误):
location /wxapi/ { proxy_pass https://api.weixin.qq.com; # 末尾无斜杠,路径变为 /wxapi/xxx → https://api.weixin.qq.com/wxapi/xxx }
这会导致请求被转发到微信API的
/wxapi/xxx
路径,而微信API的真实路径是/xxx
,从而引发404。 -
正确配置(去除前缀):
location /wxapi/ { proxy_pass https://api.weixin.qq.com/; # 末尾有斜杠,路径变为 /wxapi/xxx → https://api.weixin.qq.com/xxx }
1.2 检查实际转发路径
通过Nginx日志验证请求是否被正确转发:
- 查看Nginx访问日志:
tail -f /var/log/nginx/access.log
- 发起一个测试请求:
curl http://example.com/wxapi/cgi-bin/token
- 观察日志中的转发路径:
- 正确情况:
GET /wxapi/cgi-bin/token
→ 转发为GET /cgi-bin/token
- 错误情况:
GET /wxapi/cgi-bin/token
→ 转发为GET /wxapi/cgi-bin/token
- 正确情况:
2. 修复proxy_redirect
重定向规则
微信API返回的响应头可能包含重定向(如 Location: https://api.weixin.qq.com/new-path
),若未正确替换为代理路径,客户端会直接请求微信服务器,导致404。
2.1 调整proxy_redirect
配置
location /wxapi/ {
proxy_pass https://api.weixin.qq.com/;
proxy_set_header Host api.weixin.qq.com;
# 修正后的重定向规则(支持HTTP/HTTPS和端口)
proxy_redirect ~^(https?://)(api\.weixin\.qq\.com)(:\d+)?/(.*)$ $1$host/wxapi/$4;
}
- 正则解释:
~^(https?://)
: 匹配http://
或https://
(api\.weixin\.qq\.com)
: 匹配微信域名(:\d+)?
: 匹配可选的端口(如:443
)/(.*)$
: 匹配路径
- 替换结果:将
https://api.weixin.qq.com/new-path
替换为http://example.com/wxapi/new-path
3. 验证微信API的请求参数
部分微信API需要特定参数(如 appid
和 secret
),若缺失或错误会返回404或400错误。
3.1 测试带参数的请求
curl "http://example.com/wxapi/cgi-bin/token?grant_type=client_credential&appid=YOUR_APPID&secret=YOUR_SECRET"
- 预期成功响应:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
- 错误响应:
{"errcode":40013,"errmsg":"invalid appid"}
→ 参数错误404 Not Found
→ 路径或代理配置错误
4. 强制检查Nginx配置
4.1 检查配置语法
sudo nginx -t
- 如果输出
syntax is ok
,继续下一步。 - 如果报错,根据提示修复配置文件。
4.2 重新加载配置
sudo nginx -s reload
5. 完整配置示例(已验证)
server {
listen 80;
server_name example.com;
location /wxapi/ {
proxy_pass https://api.weixin.qq.com/; # 末尾有斜杠
proxy_set_header Host api.weixin.qq.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 重定向规则(精确匹配域名和路径)
proxy_redirect ~^(https?://)(api\.weixin\.qq\.com)(:\d+)?/(.*)$ $1$host/wxapi/$4;
# 日志记录(调试时启用)
access_log /var/log/nginx/wxapi-access.log;
error_log /var/log/nginx/wxapi-error.log;
}
location / {
root /var/www/html;
index index.html;
}
}
6. 高级调试技巧
6.1 使用CURL跟踪重定向
curl -v -L "http://example.com/wxapi/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET"
- 观察输出:
- 检查
Location
头是否被替换为http://example.com/wxapi/xxx
- 确认最终请求的URL是否为微信API的正确路径
- 检查
6.2 检查微信API文档
确认你调用的接口路径和参数是否符合微信官方要求:
- 示例:获取Access Token的接口为
/cgi-bin/token
,需传递grant_type
,appid
,secret
。
7. 常见问题总结
问题现象 | 解决方案 |
---|---|
返回404且日志显示转发路径含/wxapi | 检查 proxy_pass 是否以 / 结尾,确保路径前缀被正确去除。 |
重定向后仍指向微信域名 | 调整 proxy_redirect 正则表达式,确保域名和路径替换正确。 |
微信API返回400错误 | 检查请求参数(如 appid , secret )是否正确,或Host头是否设置为 api.weixin.qq.com 。 |
通过以上步骤,你可以定位到是路径拼接、重定向替换还是参数错误导致的404问题,针对性修复后即可正常代理微信API。
关注公众号「原宏Cloud运维栈」,带你学习更多实战经验!