0

I have a macOS laptop running a application. The UI uses port 8081 and the API uses 3031. I can access this on the mac system using localhost:8081 and all of the API calls work through port localhost:3031.

The mac is on my VPN and I can ssh into it from another machine using 10.0.0.25 and I can also access the application in my browser using 10.0.0.25:8080. The problem is that the code for the application has "localhost:3031" hardcoded for the API, so even though I can see the UI in the 2nd machine, none of the API calls work because the application on the 2nd machine is sending the requests to "localhost:3031". Well, obviously this doesn't work because the 2nd machine doesn't have the API running locally.

I can't really edit the code to change it so that "localhost:3031" is no longer the hard-coded value for the API side of the application. I'm wondering what options I have to make it work so that the 2nd machine can successfully run the application and the API calls successfully send and responses are received.

I thought that maybe if I set up an nginx reverse proxy on the mac and on the 2nd machine I access 10.0.0.25:8081 in the browser and on the mac system it listens to port 8081 and proxies that traffic to localhost:8081 and then listen to port 3031 and proxy that to localhost:3031, but I get this message:

ERROR: for nginx Cannot start service nginx: Ports are not available: exposing port TCP 0.0.0.0:3031 -> 0.0.0.0:0: listen tcp 0.0.0.0:3031: bind: address already in use

Here's my nginx default.conf:

server {
    listen 8080;

  location / {
    proxy_pass http://localhost:8081;
  }

  access_log /var/log/nginx/8081-access.log;
    error_log /var/log/nginx/8081-error.log error;
}

server {
  listen 3031;

  location / {
    proxy_pass http://localhost:3031;
  }

  access_log /var/log/nginx/3031-access.log;
  error_log /var/log/nginx/3031-error.log error;
}

And the nginx docker-compose.yml file that sets up the nginx reverse proxy container:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    # volumes:
      # - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "8080:8081"
      - "3031:3031"

Any ideas on how I can accomplish this connection with the constraints that I have? If a reverse proxy will not work, is there another method?

Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23
Michael
  • 101
  • 4
  • It will need to listen to a particular address (`10.0.0.25`?) since `0.0.0.0/0` covers `127.0.0.1` as well. You can't "partially override" the "global" listen / binding. – Tom Yan Jul 27 '22 at 01:48

2 Answers2

0

I your case, your ports 8081 and 3031 already used by docker and nginx couldn't bind on those ports. So if you would like to use reverse-proxy, you should change configuration for nginx and docker to listen for ports on particular IP addresses, docker on 127.0.0.1 and nginx on 10.0.0.25.

docker-compose.yml should looks like:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    # volumes:
      # - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "127.0.0.1:8080:8081"
      - "127.0.0.1:3031:3031"

nginx default.conf should looks like:

server {
    listen 10.0.0.25:8080;

  location / {
    proxy_pass http://localhost:8081;
  }

  access_log /var/log/nginx/8081-access.log;
    error_log /var/log/nginx/8081-error.log error;
}

server {
  listen 10.0.0.25:3031;

  location / {
    proxy_pass http://localhost:3031;
  }

  access_log /var/log/nginx/3031-access.log;
  error_log /var/log/nginx/3031-error.log error;
}
Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23
0

Find the process that is using port "3031":

lsof -ti:3031

Then find the pid of that process and kill it:

kill -9 [pid]
soup
  • 76
  • 4