Nginx对于代理请求返回404,但是在直接访问时返回200

问题描述 投票:0回答:1

目标是支持mydomain.tld / blog,以便与托管mydomain.tld的netlify一起使用。

我正在从netlify _redirects文件进行代理,如下/blog/* http://BLOG_IP/blog/:splat 200

我在路径BLOG_IP/blog上有一个幽灵博客。当我访问此URL时,博客可以与以下Nginx访问日志一起正常工作

// Directly accessed
MY_IP - - [21/May/2020:15:10:29 +0000] "GET /blog/ HTTP/1.1" 200 4935 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"

[当我尝试浏览mydomain.tld / blog时,出现以下访问日志,显示404 Nginx错误。

// Proxied through Netlify
NETLIFY_IP - - [21/May/2020:15:10:25 +0000] "GET /blog/ HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"

Netlify代理运作良好。这是完全相同的请求,但是不知何故一个是200,另一个是404。如您所见,两者都在访问"GET /blog/ HTTP/1.1"路由。

这是我的Nginx配置

server {
    server_name MY_DOMAIN;
    root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)

    location ~ ^/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }

    location ~ /.well-known {
        allow all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/MY_DOMAIN/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/MY_DOMAIN/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

    client_max_body_size 50m;
}
server {
    if ($host = MY_DOMAIN) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name MY_DOMAIN;
    return 404; # managed by Certbot
}

我的幽灵配置是

{
  "url": "https://MY_DOMAIN/blog",
  "server": {
    "port": 2368,
    "host": "127.0.0.1"
  },
  ...
}

我有2个问题:1)为什么代理请求与直接请求之间存在不一致?2)在将Netlify用作幻影服务器时如何实现MY_DOMAIN/blog

nginx proxy reverse-proxy netlify ghost-blog
1个回答
0
投票

根据您在Netlify /blog/* http://BLOG_IP/blog/:splat 200上的代理规则您正在传递BLOG_IP(直接IP地址)而不是HOST标头。因此,此规则return 404; # managed by Certbot首先生效,这就是为什么要获取404。

可能的解决方案是将重定向规则从:return 301 https://$host$request_uri;至:return 301 https://$server_name$request_uri;

或注释掉return 404;

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.