Nginx, thin and multiple hosts

I am trying to configure multiple domains on my server with nginx + thin. For example, I would like for www.domain1.com and www.domain2.com to switch to different applications with different root paths in their respective applications.

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

I thought I could just try several server blocks, but then I ran into a problem when the default server chose a random thin port and both domains went to the same application. * The main reason is that all ports are for both applications, where inside the thin_cluster block. *

I believe that my main problem is that there is a thin_cluster that is not connected to a specific server. And then there is a server block named server_name, etc. However, thin_cluster cannot be nested inside a server block.

Any ideas on how to serve multiple hosts?

Here is my file /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; } } } 
+4
source share
1 answer

You can describe how the sections "server" and "upstream" 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 an example.

Instead of ungly if blocks, I used try_files. Just read about it in the documentation.

+6
source

Source: https://habr.com/ru/post/1339262/


All Articles