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

将Azure容器Web应用置于Nginx反向代理后遇问题求助

问题描述

我在Azure中部署了一个容器Web应用(Web App for Containers),同时在Azure虚拟机上搭建了Nginx作为反向代理,想把这个Web应用放到代理后面。目前Jenkins和Keycloak已经通过这个反向代理正常提供服务(二者运行在独立虚拟机中)。

Nginx配置完成后能正常启动,但访问时会出现Azure错误页面(Azure错误提示页面),不过直接访问Web应用的资源链接是可以正常加载的。

我的Nginx配置如下:

# Proxy another application running in an Azure Container Web App
location / {
    proxy_pass https://mywebsiteinazure.azurewebsites.net/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_ssl_server_name on;
    proxy_http_version 1.1;
    proxy_redirect https://mywebsiteinazure.azurewebsites.net/ /;
}

我试过把proxy_set_header Host $host;改成proxy_set_header Host $proxy_host;,但这时请求会直接重定向到https://mywebsiteinazure.azurewebsites.net/。查了不少教程都没解决,求帮忙。

解决方案

针对Azure Web App的反向代理配置,核心问题在于Azure会验证请求的Host头是否匹配Web App的默认域名,同时要处理重定向和路径传递的问题,试试以下调整:

  • 修正Host头与添加必要转发头
    Azure Web App需要请求的Host头是它自己的域名,但要保留代理域名的访问,需同时配置X-Forwarded-Host让Web App知晓原始请求的Host:
location / {
    proxy_pass https://mywebsiteinazure.azurewebsites.net/;
    # 给Azure Web App传递正确的Host头
    proxy_set_header Host mywebsiteinazure.azurewebsites.net;
    # 传递原始请求的Host,方便Web App生成正确链接
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_ssl_server_name on;
    proxy_http_version 1.1;
    # 替换Web App返回的重定向地址为代理域名
    proxy_redirect https://mywebsiteinazure.azurewebsites.net/ https://$host/;
}
  • 检查Azure Web App配置

    1. 进入Azure门户的Web App管理页面,在配置 > 常规设置中,确保HTTP版本设置为1.1,与Nginx的proxy_http_version保持一致。
    2. 在配置 > 应用程序设置中,添加WEBSITE_USE_REMOTE_ADDR并设为True,确保Web App能正确识别客户端IP。
  • 处理静态资源路径
    如果Web App生成的静态资源链接仍指向原始域名,在Web App的应用程序设置中添加WEBSITE_HOSTNAME,值为你的代理域名(如your-proxy-domain.com),让Web App生成基于代理域名的资源链接。

  • 调试日志定位问题
    若仍有重定向问题,可在Nginx的server块中添加日志配置,查看请求头和响应状态码:

access_log /var/log/nginx/proxy_access.log;
error_log /var/log/nginx/proxy_error.log debug;

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 23:38:21