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

如何配置Nginx,使Hetzner的Django代理指定路由到Hostinger的WordPress

问题分析与修复方案

你的Nginx配置存在几个关键问题,导致反向代理到Hostinger的WordPress站点无法正常工作,以下是具体修复步骤:

1. 修正反向代理核心配置

原location /route-name/的代理规则存在Host头匹配错误、缺少HTTPS转发头等问题,修改后的完整配置如下:

server {
    location = /favicon.ico { access_log off; log_not_found off; }
    
    location /static/ {
        alias /var/www/example.com/static/;
    }

    location /media/ {
        alias /var/www/example.com/media/;
    }

    # 修正后的反向代理规则
    location /route-name/ {
        proxy_pass http://<HOSTINGER-IP>/;
        # 替换为Hostinger服务器上WordPress绑定的域名或IP
        proxy_set_header Host <HOSTINGER-WORDPRESS-DOMAIN-OR-IP>;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 告诉WordPress当前使用HTTPS协议
        proxy_set_header X-Forwarded-Proto $scheme;
        # 修正WordPress返回的跳转链接路径
        proxy_redirect http://<HOSTINGER-WORDPRESS-DOMAIN-OR-IP>/ https://$host/route-name/;
        proxy_redirect http://<HOSTINGER-IP>/ https://$host/route-name/;
    }
    

    location / {
        include proxy_params;
        proxy_redirect off;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 404; # managed by Certbot
}

2. 调整WordPress站点配置

登录Hostinger上的WordPress后台,修改站点URL设置:

  • 站点地址(URL):设置为 https://example.com/route-name
  • WordPress地址(URL):若WordPress安装在Hostinger服务器根目录,可保持为 http://<HOSTINGER-IP> 或Hostinger绑定的域名,也可统一设置为 https://example.com/route-name

或者直接修改WordPress根目录下的wp-config.php文件,添加以下代码:

define('WP_HOME', 'https://example.com/route-name');
define('WP_SITEURL', 'http://<HOSTINGER-IP>');

关键修改说明

  • Host头修正:将Host设为Hostinger服务器上Apache绑定的域名或IP,确保Apache能正确匹配到WordPress虚拟主机。
  • X-Forwarded-Proto:告诉WordPress当前用户通过HTTPS访问,避免生成HTTP链接导致混合内容错误。
  • proxy_redirect:将WordPress返回的跳转链接(指向Hostinger IP/域名)替换为https://example.com/route-name/,保证前端跳转路径正确。
  • WordPress URL配置:让WordPress知晓自身通过/route-name路径对外服务,生成的链接会自动带上该前缀,避免跳转异常。

修改完成后重启Nginx服务:

sudo systemctl restart nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:03:14