Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Reverse proxy config

I'm having issues with getting a simple config to work with nginx. I have a server that host docker containers so nginx is in a container. So lets call the url foo.com. I would like for the url foo.com/service1 to actually just go to foo.com on another port, so it would actually be pulling foo.com:4321 and foo.com/service2 to be pulling foo.com:5432 and so on. Here is the config I have been having issues with.

http {
    server {
        listen 0.0.0.0:80;

        location /service1/ {
            proxy_pass http://192.168.0.2:4321/;
        }

        location /service2/ {
            proxy_pass http://192.168.0.2:5432/;
        }

    }
}

So the services and nginx live at 192.168.0.2. What is the prefered way to be able to do this? Thank you in advance!

As A side note, this is running in a docker container. Thanks!

like image 694
user3003510 Avatar asked Sep 19 '25 01:09

user3003510


2 Answers

I think you should check whether your foo.com is pointing to the right ip address or not first by removing the reverse proxy config. E.g.

http {
    server {
        listen 80;
        server_name foo.com;

        location / {
        }
    }
}

Then, if your ip address already has a service running on port 80 you should specify the server_name for each service like in my example. Nginx can only distinguish which service to respond to which domain by server_name.

*My guess is that you forgot the server_name option.

like image 98
Anh Cao Avatar answered Sep 21 '25 15:09

Anh Cao


http {
    server {
        listen 80;
        server_name foo.com;

        location /service1/ {
            proxy_pass http://192.168.0.2:4321/;
        }

        location /service2/ {
            proxy_pass http://192.168.0.2:5432/;
        }

    }
}
like image 21
kisiel Avatar answered Sep 21 '25 14:09

kisiel