Nginx subdomain configuration is wrong

536 views Asked by At

I would like to configure my Nginx version 1.10.2 as a reverse proxy hosted in a CentOS 7 OS. I have several applications running on the same WildFly 10 server. Those applications are available at http://213.xxx.xxx.xxx/app1 and http://213.xxx.xxx.xxx/app2. I created a subdomain for app2 http://app2.example.com. My nginx.conf file contains those servers:

server {
    listen 80;
    server_name example.com www.example.com;
    location / {
        proxy_pass http://213.xxx.xxx.xxx/app1;
    }
}

server {
    listen 80;
    server_name app2.example.com www.app2.example.com;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://213.xxx.xxx.xxx/app2;
    }
}

From my web browser I can reach app1 at URL example.com. But I can't reach app2. When I send a request to app2.example.com, it redirects me to app2.example.com/app2. Can someone tells me what is wrong with my configuration?

1

There are 1 answers

2
Tarun Lalwani On

Seems like you are app does the redirects and that is why app1 works and app2 doesn't work. Now there are few things you can try

server {
    listen 80;
    server_name app2.example.com www.app2.example.com;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://213.xxx.xxx.xxx/app2;
        proxy_redirect http://213.xxx.xxx.xxx/app2/ http://$host/;
    }
}