0

I have two Docker instances running Wordpress and my Nodejs application separately in the same machine.

Now I want to set up Nginx to accept connections only on my domain (mydomain.com). So if users access to mydomain.com/ they should be redirected to wordpress container, and if they access to mydomain.com/customer, the request should be redirected to Nodejs app container.

This schema explains the idea:

network schema

I am not very good at Nginx and it is being so difficult for me to edit the config. file to match that schema. Also I dont find any kind of examples or documentation to achieve this configuration. Is it so weird?

E. Williams
  • 405
  • 1
  • 6
  • 21

1 Answers1

0

It is the very common nginx usage example. The very basic setup you need is

server {
    listen 80;
    server_name mydomain.com;
    proxy_set-header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://<docker1_address:port>;
    }
    location /customer {
        proxy_pass http://<docker2_address:port>;
    }
}
server {
    # this would be the default server for all requests
    # where HTTP Host header is missing or not equal to 'mydomain.com'
    listen 80 default_server;
    # close the connection immediately
    return 444;
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • Thanks! Is there a way to do it without hardcoding the IPs of docker containers? – E. Williams Nov 13 '20 at 22:56
  • If your nginx is running in another docker container and all of this is managed by docker-compose, you can (should) use the container names instead. If the nginx is running on host, you can use any domain name that resolves to the required docker address at nginx startup. – Ivan Shatsky Nov 13 '20 at 23:01
  • It is also worth to mention that if you need to use the WebSockets protocol with your NodeJS app, the NodeJS related location would be more complicated. [Here](https://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss/14969925#14969925) is an example. – Ivan Shatsky Nov 14 '20 at 01:49