2

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the instant reply, will make the changes accordingly & keep updated.
Further, after configuration its opening up the respective application for different domains as expected, but for any action in the application like lets say 'login' the request again hits the nginx which gives out a 404 Not Found response. Kindly any help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.