Ayaz Pasha
Ayaz Pasha

Reputation: 1055

nginx configuration for multiple domain names

I have just installed nginx and have more than one domain name pointing to same IP. When calling each domain I have to redirect to different applications running on the same machine, each application is running on different port.

For ex, I have app1.domain.com, app2.domain.com & app3.domain.com

so, for app1.domain.com I have to redirect to localhost:<port1> likewise, app2.domain.com I have to redirect to localhost:<port2> and app3.domain.com I have to redirect to localhost:<port3>

How do I go about?

Thanks in advance

Upvotes: 2

Views: 6572

Answers (1)

linuxnewbee
linuxnewbee

Reputation: 1008

Well if your application are running on different ports then your nginx conf files should look like this.

upstream app1  {
      server 127.0.0.1:port1; #App1
}

upstream app2  {
      server 127.0.0.1:port2; #app2
}

server {
    listen       xxx.xxx.xxx.xxx:80;
    server_name  app1.domain.com;

    access_log  /var/log/nginx/log/app1.domain.com.access.log  main;
    error_log  /var/log/nginx/log/app1.domain.com.error.log;
    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send request back to apache1 ##
    location / {
    proxy_pass  http://app1;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

server {
   listen      xxx.xxx.xxx.xxx:80;
   server_name app2.domain.com;
   access_log  /var/log/nginx/log/app2.domain.com.access.log  main;
   error_log   /var/log/nginx/log/app2.domain.com.error.log;
   root        /usr/local/nginx/html;
   index       index.html;

   location / {
        proxy_pass  http://app2;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503         http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host            app2.domain.com;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Please let me know if you have any doubts. Thanks

Upvotes: 5

Related Questions