nginx - 如何使用反向代理在 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;
}
}
有人可以帮我解决这个问题吗?谢谢。
解决方案
您应该upstream
在http
指令中定义。
http {
# ...
upstream myapp {
server 0.0.0.0:3000;
}
# ...
}
然后在这样的server
范围内使用它location
:
location ^~ /video/ {
proxy_pass http://myapp;
}
推荐阅读
- ruby-on-rails - Rails 中的嵌套表单。无法创建和关联父级
- excel - 在嵌套循环中仅检查一次复选框值
- google-apps-script - Apps 脚本 - onEdit 结合时间戳和用户名/电子邮件地址不起作用
- java - Java 识别延迟源
- c# - 使用 AutoMapper,如何为所有地图泛化自定义类型
- reactjs - Next.js getServerSideProps redirection ERR_HTTP_HEADERS_SENT error
- java - 即使我使用 DB_CLOSE_DELAY=-1 报告 H2 内存数据库也找不到表
- opengl - 体积光未正确渲染体积
- java - 微服务连接Oracle数据库
- javascript - 使用 fetch 设置 localStorage