[英]Subdomain creation in nginx server
我正在嘗試重定向/重寫url,以為子頁面創建子域。 例如: http : //example.com/user將以http://user.example.com返回url,但無法確定任何方式。 我的應用程序位於Rails和Nginx服務器上的ruby中 。 有什么竅門嗎?
以下應注意子域重定向。
server {
server_name example.com;
location ~ ^/(?<user>[^/]+)/?$ {
return 301 $scheme://$user.$server_name;
}
}
您最好查看Nginx
上的通配符子域 ,並為用戶提供Rails
路由。
請記住,Nginx沒有與數據庫的連接。 因此, Nginx
conf實際上只需要能夠通過服務器將Web流量發送到Rails應用程序。
然后, 如果有用戶存在,則可以使用Rails應用程序路由將流量發送到特定的用戶頁面。 您將有一個constraint
-
這是我要做的:
/etc/nginx/nginx.conf (or wherever your nginx config is stored)
server {
server_name yourdomain.com *.yourdomain.com;
...
}
這將使您能夠將任何子域請求傳遞到Rails應用。
然后,我將在您的Rails routes
執行此操作:
config/routes.rb
#put this at the top of the file so it does not conflict
get '', to: 'users#index', constraints: lambda { |request| User.exists?(name: request.subdomain) }
您可以在Rails應用中進行操作。 只需將一條規則添加到您的路由文件中:
get '/', to: 'users#show', constraints: { subdomain: /\\d+/ }
然后,無論您訪問*.example.com
是什么,它都會重定向到users#show
。
有關更多信息,請檢查本教程。Rails Routing Guide ,第3.8部分。
否則,您可以在nginx config中進行配置重寫。 我不確定它是否有效,請查看此指南
server {
listen 80;
server_name user.example.com;
return 301 example.com/user
}
希望對您有所幫助。
更新
您只需要重寫您的nginx配置。
server {
listen 80;
server_name example.com;
rewrite \/home$ $scheme://home.example.com;
}
然后保存配置,使用sudo service nginx reload
重新加載nginx sudo service nginx reload
以重新加載配置文件。
讓我知道您是否仍然擔心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.