如何使用反向代理在 Nginx 中处理应用程序级别重定向

Ghu*_*han 6 nginx crystal-lang alpine-linux

我正在为基于 Crystal-Lang 的应用程序编写 Nginx 配置,以通过反向代理http://example.com/videos/发送所有流量。http://0.0.0.0:3000

我已经编写了以下配置,该配置不起作用,我正在互联网上但没有运气。

当我转到 时,应用程序在内部执行从问题所在的http://example.com/videos/重定向,它重定向到错误的位置,我需要像往常一样始终将部分与 URL 连接起来,但在重定向后,该部分将被切掉。所以应用程序应该考虑作为主机。http://example.com/http://example.com/feed/popularhttp://example.com/feed/popular/videos/http://example.com/videos/xxxx/videos/http://example.com/videos/

这是我的配置:

server {
    listen 80;
    server_name elearn.com.pk default_server localhost;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    location /videos/ {
        proxy_pass http://0.0.0.0:3000/;
        proxy_http_version 1.1;
        rewrite /videos/(.*) /$1 break;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host/videos/;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
   }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?谢谢。

Joh*_*ler 0

假设重定向来自代理应用程序,您需要重写Location标头以匹配代理服务器上的前缀路径。

proxy_redirect off导致此错误。您需要将其设置为default或配置自定义重写(default不过应该可以很好地满足您的目的)。