如何创建动态子域 rails
How to create dynamic subdomain rails
如何为在我的网站上注册的每个用户创建子域?例如 userone.mysite.com 和 usertwo.mysite.com.
在 php 中可以使用 apache 虚拟主机完成,但我不知道如何在 Ruby 和 Rails 中做同样的事情。这是在 apache
中如何完成的
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
DocumentRoot /www/domain
</VirtualHost>
我浏览了很多博客,但找不到解决方案。
请指教
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.my_site.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/html/my_site
<Directory /var/www/html/my_site>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
并且不要忘记将 cname * 条目更改为您在 DNS 中的域
除了 Dinesh Saini 的回答 - 您还需要相应地更新 Rails 配置。例如,如果您需要深层子域,您应该更改
config.action_dispatch.tld_length 在 staging.rb 中,您应该重新检查 routes.rb。
实例:我必须实现按子域显示的商店 - 示例 URL
我的-shop.shop.testapp.com.So除了服务器配置更改我做了什么
constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
get '/', to: 'shopes#show', as: :shop
end
在控制器中寻找资源
Shop.find_by(id: request.subdomains[0])
我也设置了
config.action_dispatch.tld_length = 2
我这样做是为了暂存环境,因为它有这样的 URL staging.testapp.com 所以我需要另一个子域级别。我认为对你来说最好检查它是否有 1。
如何为在我的网站上注册的每个用户创建子域?例如 userone.mysite.com 和 usertwo.mysite.com.
在 php 中可以使用 apache 虚拟主机完成,但我不知道如何在 Ruby 和 Rails 中做同样的事情。这是在 apache
中如何完成的<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
DocumentRoot /www/domain
</VirtualHost>
我浏览了很多博客,但找不到解决方案。 请指教
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.my_site.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/html/my_site
<Directory /var/www/html/my_site>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
并且不要忘记将 cname * 条目更改为您在 DNS 中的域
除了 Dinesh Saini 的回答 - 您还需要相应地更新 Rails 配置。例如,如果您需要深层子域,您应该更改 config.action_dispatch.tld_length 在 staging.rb 中,您应该重新检查 routes.rb。
实例:我必须实现按子域显示的商店 - 示例 URL 我的-shop.shop.testapp.com.So除了服务器配置更改我做了什么
constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
get '/', to: 'shopes#show', as: :shop
end
在控制器中寻找资源
Shop.find_by(id: request.subdomains[0])
我也设置了
config.action_dispatch.tld_length = 2
我这样做是为了暂存环境,因为它有这样的 URL staging.testapp.com 所以我需要另一个子域级别。我认为对你来说最好检查它是否有 1。