I am using nginx running inside of a docker container to route traffic based on the url.
Adding the following two lines of code in the nginx config file fixed the error Invalid Host header for me. See below for the config file(default.conf).
proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $remote_addr;
First the following is my simple two liner Dockerfile to create the nginx container and then configure it with routing.
FROM nginxCOPY ./default.conf /etc/nginx/conf.d/default.conf
So when the image is built, the default.conf file is copied to the configuration directory inside of the nginx container.
Next the default.conf file looks as follows.
upstream ui { # The ui service below is a ui app running inside of a container. Inside of the container, the ui app is listening on port 3000. server ui:3000;}upstream node-app { # The node-app service below is a server app running inside of a container. Inside of the container, the server is listening on port 8080. server node-app:8080;}server { listen 80; location / { # The root path, with is '/' will routed to ui. proxy_pass http://ui; ################## HERE IS THE FIX ################## # Adding the following two lines of code finally made the error "Invalid Host header" go away. # The following two headers will pass the client ip address to the upstream server # See upstream ui at the very begining of this file. proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; } location /api { # Requests that have '/api' in the path are rounted to the express server. proxy_pass http://node-app; }}#
Finally, if you want to take a look at my docker compose file, which has all the services(including nginx), here it is
version: '3'services: # This is the nginx service. proxy: build: # The proxy folder will have the Dockerfile and the default.conf file I mentioned above. context: ./proxy ports: - 7081:80 redis-server: image: 'redis' node-app: restart: on-failure build: context: ./globoappserver ports: - "9080:8080" container_name: api-server ui: build: context: ./globo-react-app-ui environment: - CHOKIDAR_USEPOLLING=true ports: - "7000:3000" stdin_open: true volumes: - ./globo-react-app-ui:/usr/app postgres: image: postgres volumes: - postgres:/var/lib/postgresql/data - ./init-database.sql:/docker-entrypoint-initdb.d/init-database.sql environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=passwordvolumes: postgres: