首页 > 解决方案 > 如何使用反向代理在 Nginx 中处理应用程序级重定向

问题描述

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

我已经编写了以下无法正常工作的配置,我正在上网,但没有运气。

当我转到时http://example.com/videos/,应用程序在内部执行重定向http://example.com/http://example.com/feed/popular哪个是问题,它重定向到http://example.com/feed/popular哪个是错误的,我需要像往常/videos/一样始终将部分与 URL 连接,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;
   }
}

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

标签: nginxalpinecrystal-lang

解决方案


您应该upstreamhttp指令中定义。

http {
# ...

  upstream myapp {
    server 0.0.0.0:3000;
  }

# ...

}

然后在这样的server范围内使用它location

location ^~ /video/ {
  proxy_pass http://myapp;
}

推荐阅读