我想通过Nginx和RoR Web服务器(如Unicorn,Thin或WEBrick)在我的本地计算机上部署我的Ruby on Rails应用程序.如下所示,我想通过post subdomain访问我的网络应用程序:upstream sub {server unix:/tmp/unicorn.subdom...

我想通过Nginx和RoR Web服务器(如Unicorn,Thin或WEBrick)在我的本地计算机上部署我的Ruby on Rails应用程序.
如下所示,我想通过post subdomain访问我的网络应用程序:
upstream sub {
server unix:/tmp/unicorn.subdomain.sock fail_timeout=0;
# server 127.0.0.1:3000;
}
server {
listen 80;
server_name post.subdomain.me;
access_log /var/www/subdomain/log/access.log;
error_log /var/www/subdomain/log/error.log;
root /var/www/subdomain;
index index.html;
location / {
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;
proxy_redirect off;
try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
}
location @ruby {
proxy_pass http://sub;
}
}
一切正常,当我输入post.subdomain.me时,我可以看到我的RoR应用程序.
问题:当我使用post.subdomain.me url时,我无法访问我的子域(request.subdomain返回空,request.host返回subdomain instaed of subdomain.me).但是当我使用post.subdomain.me:3000时,每件事情都很完美(我失去了一半的头发才能意识到这一点).为什么以及如何解决?
解决方法:
当您使用端口访问应用程序时 – 您正在直接访问rails服务器,而不是由nginx代理,这适用于调试,但通常不适合生产.
客户端可能不会传递主机头,$host默认为nginx主机
尝试
location @ruby {
proxy_set_header Host $host;
proxy_pass http://sub;
}
和’硬编码’方式:proxy_set_header主机post.subdomain.me;
本文标题为:ruby-on-rails – 为什么我无法通过nginx代理传递访问子域?


基础教程推荐
- win10下使用virtualbox + vagrant配置ruby开发机环境 2023-07-23
- Swift初始化器与可选链的使用方法介绍 2023-07-08
- ruby on rails validates 2023-09-22
- Swift中重写和重载的使用与对比总结 2023-07-05
- R语言-修改(替换)因子变量的元素操作 2022-11-26
- Ruby3多线程并行Ractor使用方法详解 2023-07-23
- R语言 ggplot2改变柱状图的顺序操作 2022-11-17
- R语言绘制折线图实例分析 2022-11-21
- 浅析ELF转二进制允许把 Binary 文件加载到任意位置 2023-07-06
- ruby-on-rails-为使用Rails 4,nginx和乘客的用户设置自定义域 2023-09-21