如何使用 Nginx + Rails 服务器处理动态子域?
How do I handle dynamic subdomains with Nginx + Rails server?
我在将子域名称从 nginx 代理到 rails 服务器时遇到问题。在我的 rails 应用程序中,我有 links,如 tenant1.localhost:3000
、tenant2.localhost:3000
等,并且工作正常。在生产中,我使用 Nginx + Puma,如果我用子域打开 link,nginx 不会代理任何请求给 Puma。
nginx.conf
upstream puma_muninn {
server app:3000;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
server_name localhost puma_muninn;
server_name ~^(?<subdomain>.+)localhost$;
root /var/www/muninn/public;
try_files $uri/index.html $uri @puma_muninn;
location @puma_muninn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://$subdomain.puma_muninn;
# limit_req zone=one;
access_log /var/www/muninn/log/nginx.access.log;
error_log /var/www/muninn/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
production.rb
config.action_dispatch.tld_length = 2
但正如我所说,puma 甚至没有收到来自 nginx 的请求。
有什么想法吗?
nginx.conf 中的域必须是特定的,而不是本地主机。
我在将子域名称从 nginx 代理到 rails 服务器时遇到问题。在我的 rails 应用程序中,我有 links,如 tenant1.localhost:3000
、tenant2.localhost:3000
等,并且工作正常。在生产中,我使用 Nginx + Puma,如果我用子域打开 link,nginx 不会代理任何请求给 Puma。
nginx.conf
upstream puma_muninn {
server app:3000;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
server_name localhost puma_muninn;
server_name ~^(?<subdomain>.+)localhost$;
root /var/www/muninn/public;
try_files $uri/index.html $uri @puma_muninn;
location @puma_muninn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://$subdomain.puma_muninn;
# limit_req zone=one;
access_log /var/www/muninn/log/nginx.access.log;
error_log /var/www/muninn/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
production.rb
config.action_dispatch.tld_length = 2
但正如我所说,puma 甚至没有收到来自 nginx 的请求。
有什么想法吗?
nginx.conf 中的域必须是特定的,而不是本地主机。