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

使用NGINX反向代理时Shopify感谢页未跳转至自定义域名

解决方案

1. 修复Host头传递问题

你的NGINX配置中注释掉了proxy_set_header Host $host;,这是核心问题——Shopify会根据请求的Host头生成跳转URL(包括感谢页),如果不传递自定义域名的Host,Shopify会默认使用自身的myshopify域名生成跳转链接。

取消该注释,让Shopify识别到请求来自你的自定义域名:

# 取消这行的注释
proxy_set_header Host $host;

2. 拦截并重写Shopify的重定向响应

即使传递了Host头,部分场景下Shopify仍可能返回带myshopify域名的重定向,需要用proxy_redirect指令拦截并替换:

# 在location / 块内添加
proxy_redirect ~*https://stagingstore.myshopify.com(.*) $scheme://$host$1;

这条规则会把所有来自Shopify的、指向stagingstore.myshopify.com的重定向,替换为你的自定义域名($host)。

3. 替换响应内容中的myshopify域名

感谢页的页面内容里可能还包含硬编码的myshopify域名链接,需要用sub_filter模块替换响应中的域名。Heroku官方NGINX buildpack默认支持该模块,在server块内添加以下配置:

# 在server块的最外层添加
sub_filter 'stagingstore.myshopify.com' '$host';
sub_filter_once off;
sub_filter_types text/html text/css text/javascript application/json;
  • sub_filter_once off:确保替换所有出现的域名,而非仅第一个
  • sub_filter_types:指定需要处理的响应类型,覆盖常见页面资源

修改后的完整NGINX配置

upstream app_server {
    server unix:/tmp/nginx.socket fail_timeout=0;
}

server {
    listen <%= ENV["PORT"] %>;
    server_name _;
    keepalive_timeout 5;
    port_in_redirect off;

    # 全局替换响应内容中的myshopify域名
    sub_filter 'stagingstore.myshopify.com' '$host';
    sub_filter_once off;
    sub_filter_types text/html text/css text/javascript application/json;

    location / {
        proxy_pass https://stagingstore.myshopify.com;

        client_max_body_size    10m;
        client_body_buffer_size     128k;
        proxy_connect_timeout 90;

        # 传递自定义域名的Host头给Shopify
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # 重写Shopify返回的myshopify域名重定向
        proxy_redirect ~*https://stagingstore.myshopify.com(.*) $scheme://$host$1;
    }

    location ~ ^/(search|category)/? {
        proxy_pass http://app_server;
        proxy_redirect off;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

额外注意事项

  • 若使用多个自定义域名,server_name _可正常工作;若需指定特定域名,替换为你的自定义域名即可(如server_name yourstore.com;)
  • 部署到Heroku后,执行heroku restart重启服务确保配置生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:35:26