Reverse proxy nginx and Express server returning 502

I am running a single VPS with an Nginx reverse proxy configuration forwarding to three Angular websites. Each site is running inside its own Docker container behind an Express server.

The first two sites I got up and running no problem. They are simple Angular sites with the server.js and index.html files at the base of the project’s directory.

  • Docker compose environment and paths
  • Checking reason behind node failure
  • Better rules for gnu make with docker
  • How to upload next version of code on amazon AWS which has docker instance?
  • WordPress Docker Container Cannot Upload Theme
  • Class 'MongoId' not found in Laradock application
  • For the third site I got a little fancier and am using Grunt to build the project files into a new directory called public. My goal is to have the site viewable from this public directory.

    Here’s my nginx.conf file. Note that the first two server blocks are working as expected. Browsing to the third, however, results in “502 Bad Request”:

    server {
        listen 80;
        server_name domain1.com domain2.ca;
        location / {
                proxy_pass              http://127.0.0.1:9001;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_redirect          off;
                proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
        }
    }
    
    server {
        listen 80;
        server_name sub1.domain1.com sub1.domain2.ca;
        location / {
                proxy_pass              http://127.0.0.1:9002;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_redirect          off;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    // THIS IS THE SERVER THAT IS NOT WORKING
    server {
        listen 80;
        server_name sub2.domain1.com sub2.domain2.ca;
        location / {
                proxy_pass              http://127.0.0.1:9003;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_redirect          off;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    

    This is the file that starts up my Express server. All three projects have very similar servers, but this one serves static files at /public instead of just /

    var express = require('express');
    var http = require('http');
    
    var app = express();
    
    app.use(express.static(__dirname + '/public'));
    
    var port = 80;
    http.createServer(app).listen(port, function() {
      console.log("server listening on port " + port);
    });
    

    Lastly, because this site is inside a docker container, here’s my Dockerfile for building it:

    # Start with angular node grunt bower setup
    FROM brymastr/angular:latest
    
    # Add to working dir
    WORKDIR /src
    ADD . /src
    
    # install dependencies
    RUN     npm install
    RUN     bower install --allow-root
    RUN     sudo apt-get -y install ruby-full
    RUN     sudo gem install sass
    RUN     grunt build
    
    # Run application
    EXPOSE  80
    CMD ["nodemon", "server.js"]
    

    This image works just fine when running the container on both of my local dev environments. This makes me think that the issue is with the nginx configuration on the VPS.

    To recap: nginx takes the request and forwards it to the port assigned for the docker container that runs the site. The container’s express server picks up the request and serves the files in the /public folder of the project.

    Am I missing something when it comes to the routing between nginx and express?

    Any help is greatly appreciated. I am still new at this and I’m just trying to save some money running small sites on one VPS. There’s probably a better/simpler way to do this but this is what I’ve come up with for now. I’ve searched extensively and tried much including having express serve static files at / and having nginx proxy pass to 127.0.0.1:9003/public but no luck.

    Thanks

  • How to set UIDs for official Docker images?
  • Publishing Docker to AWS from Visual Studio
  • Jelastic run.log not working on docker
  • Unable to connect to remote MySQL from a docker on Ubuntu Desktop
  • Docker Compose on Windows 7: Could not locate Gemfile or .bundle/ directory
  • NodeJS + Forever + Docker configuration doesn't work
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.