nareshbhatia / proshop-nx

Sample shopping app using the Nx monorepo
proshop-nx-catalog.vercel.app
2 stars 0 forks source link

Reverse proxy requires hard-coded URLs for apps #2

Closed nareshbhatia closed 2 years ago

nareshbhatia commented 2 years ago

Nginx configuration is currently setup with hard-coded URLs for catalog and cart apps, e.g. http://192.168.86.247:3001/catalog (see proxy/default.conf). Could not figure out a way to specify these dynamically.

server {
    listen 80;
    listen [::]:80;
    server_name proshop.com;

    # setup specific routes to goto the correct app
  location /catalog {
    proxy_pass http://192.168.86.247:3001/catalog;
  }

  location /cart {
    proxy_pass http://192.168.86.247:3002/cart;
  }

  # Everything else gets redirected to catalog
  location / {
    proxy_pass http://192.168.86.247:3001/catalog;
  }
}

There is a concept of substituting environment variables in Ngnix config from docker-compose however I could not make it work.

themegaphoenix commented 2 years ago

Don't build your own proxy image. Instead mount the volume with the proxy configuration file. This saves a lot of time when building the image and makes it easier for documentation purposes.

This then makes it no different from an .env file (or environmental configuration) and hence you can just specify http://proshop-catalog:3001 as the proxy_pass. See how much easier it is now?

You are also unnecessarily exposing the ports in your Docker Compose. Instead create a Docker network.

This example is for vue and fastapi, but the principle remain for the both points: https://github.com/nguqtruong/nginx-fastapi-vuejs/blob/master/docker-compose.yml

nareshbhatia commented 2 years ago

Thank you for the pointers. Let me try this.

nareshbhatia commented 2 years ago

Thanks @themegaphoenix. I have fixed this using your suggestions.

  1. I am no longer creating an image for the proxy. I am using the standard nginx image and mounting a volume. See here.
  2. I am no longer exposing any ports except port 80 from the nginx proxy.
  3. I didn't create a custom Docker network because I was able to address all services by simply using their service names, e.g. http://proshop-catalog:3001. Please see my new docker-compose.yaml.
themegaphoenix commented 2 years ago

3. I didn't create a custom Docker network because I was able to address all services by simply using their service names, e.g. http://proshop-catalog:3001. Please see my new docker-compose.yaml.

Slight correction. By default, docker-compose creates a network for each compose file: https://runnable.com/docker/docker-compose-networking#:~:text=Docker%20Compose%20sets%20up%20a,other%20container%20on%20the%20network.

Good work!

For further improvements see this: https://stackoverflow.com/a/42174904

You can specify the build or the image name, and either build all the images (docker-compose build) or run the prebuilt images (docker-compose up, if the images are not found then it will build them anyway)

nareshbhatia commented 2 years ago

Thanks @themegaphoenix, I find your pointers and links very very useful.