nginx,瘦主机和多主机 [英] nginx, thin, and multiple hosts

查看:115
本文介绍了nginx,瘦主机和多主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行Nginx + Thin的服务器上设置多个域.例如,我希望www.domain1.com和www.domain2.com转到具有各自应用程序的不同根路径的不同应用程序.

I am trying to set up multiple domains on my server running nginx + thin. For example, I would like www.domain1.com and www.domain2.com to go to different apps with different root paths to their respective apps.

如果您对nginx熟悉,我已经在帖子的底部发布了我的nginx.conf文件.

If you are familiar with nginx, I have posted my nginx.conf file at the bottom of this post.

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

I was thinking I could just try having multiple server blocks, but then I ran into a problem where the server would default to choosing a random thin port and both domains went to the same app. *The main reason being that all ports for both apps where inside of the thin_cluster block.*

我想我主要关心的是,有没有与特定服务器关联的thin_cluster.然后有一个带有server_name等的服务器块.但是,thin_cluster不能嵌套在服务器块内.

I guess my main concern is that there is the thin_cluster which has no association with a particular server. And then there is the server block which has the server_name, etc. However, the thin_cluster can't be nested inside of the server block.

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

Any ideas on how to serve multiple hosts?

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

Here is my /etc/nginx/nginx.conf file

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;
    }
  }
}

推荐答案

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

You may describe as mach "server" and "upstream" sections as you want.


upstream cluster1 {
  ...;
}
upstream cluster2 {
  ...;
}
server {
  listen 80;
  server_name www.domain1.com;
  root /home/app1;
  location / {
    try_files $uri/index.html $uri.html $uri @backend;
  }
  location @backend {
      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;
      proxy_pass http://cluster1;
  }
}
server {
  listen 80;
  server_name www.domain2.com;
  root /home/app2;
  location / {
    try_files $uri/index.html $uri.html $uri @backend;
  }
  location @backend {
      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;
      proxy_pass http://cluster2;
  }
}

这里是一个例子.

我使用了"try_files"而不是笨拙的"if"块.只需在文档中阅读它即可.

Instead of ungly "if" blocks, i used "try_files". Just read about it in documentation.

这篇关于nginx,瘦主机和多主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆