开发者

nginx, thin, and multiple hosts

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.

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

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.*

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?

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

Here is a example.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜