gpt4 book ai didi

ruby-on-rails - nginx、瘦和多主机

转载 作者:行者123 更新时间:2023-12-05 00:38:59 25 4
gpt4 key购买 nike

我正在尝试在运行 nginx + Thin 的服务器上设置多个域。例如,我希望 www.domain1.com 和 www.domain2.com 使用不同的根路径访问不同的应用程序。

如果你熟悉 nginx,我已经在这篇文章的底部发布了我的 nginx.conf 文件。

我想我可以尝试使用多个服务器块,但是
然后我遇到了一个问题,服务器会默认选择一个随机的瘦端口,并且两个域都转到同一个应用程序。 *主要原因是这两个应用程序的所有端口都在 Thin_cluster 块内。*

我想我主要担心的是 Thin_cluster 与特定服务器没有关联。然后是具有 server_name 等的 server 块。但是,thin_cluster 不能嵌套在 server 块内。

关于如何为多个主机提供服务的任何想法?

这是我的/etc/nginx/nginx.conf 文件

user  nginx;
worker_processes 5;

error_log /var/log/nginx.error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx.access.log main;

sendfile on;

keepalive_timeout 65;

upstream thin_cluster {
server 0.0.0.0:3000;
server 0.0.0.0:3001;
server 0.0.0.0:3002;
server 0.0.0.0:3003;
server 0.0.0.0:3004;
}

server {
listen 80;
server_name www.domain1.com;

root /home/ec2-user/helloCloud/public;

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;

if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://thin_cluster;
break;
}
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

最佳答案

您可以根据需要将其描述为“服务器”和“上游”部分。

上游集群 1 {
...;
}
上游集群 2 {
...;
}
服务器 {
听80;
server_name www.domain1.com;
根/home/app1;
地点/{
try_files $uri/index.html $uri.html $uri @backend;
}
位置@backend {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header 主机 $http_host;
proxy_redirect 关闭;
proxy_pass http://cluster1;
}
}
服务器 {
听80;
server_name www.domain2.com;
根/home/app2;
地点/{
try_files $uri/index.html $uri.html $uri @backend;
}
位置@backend {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header 主机 $http_host;
proxy_redirect 关闭;
proxy_pass http://cluster2;
}
}

这是一个例子。

我使用了“try_files”,而不是丑陋的“if”块。只需在文档中阅读它。

关于ruby-on-rails - nginx、瘦和多主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4961329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com