1

Hi this may be a noob question.

I am trying to replicate a 502 bad gateway error when using nginx as a web server. I have locally running VM of Ubuntu 20.04.6 LTS on which I have freshly installed nginx using

sudo apt-get install nginx

then

nginx -v which returned

nginx version: nginx/1.18.0 (Ubuntu)

and verified that it is running by the command

systemctl status nginx that shows that it is running.

Then I created a docker container with a simple Dockerfile:

# Use the official Node.js image with Alpine Linux as the base image
FROM node:14-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port that the app will run on
EXPOSE 9000

# Define the command to run your application
CMD ["node", "app.js"]

And this is the app.js file

// app.js
const express = require('express');
const app = express();
const port = 9000;

app.get('/', (req, res) => {
  res.send('<h1>Hello, Docker World!</h1>');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});

When I access the localhost:9000 it returns the web page saying 'Hello, Docker World!'

Next I want to serve this localhost:9000 using the nginx web server. Then I will shutdown the docker container in an attempt to generate bad gateway error response from nginx web server.

3 Answers 3

1

Firstly, open the configuration file located at /etc/nginx/sites-enabled/default.

Check the file's content; you will easily find a location /. Add these three proxy lines inside the location /.

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:9000;
}

Then, restart the Nginx service, and you will be good to go.

systemctl restart nginx

Check out the tutorial for a case study on Nginx reverse proxy: How to Set Up Nginx Reverse Proxy Servers by Example"

1
  • 1
    Thank you so much. This replicated the behavior I wanted.. 1. Added the configuration to /etc/nginx/sites-enabled/default 2. Restarted the nginx server with systemctl restart nginx 3. Started the docker container docker start keen_litcherman that started LISTENING at port 9000 4. localhost redirected to the docker container and displayed "Hello, Docker World!" 5. Stopped the container 6. Upon refreshing the page localhost the server returned 502 Bad Gateway nginx/1.18.0 (Ubuntu) Nov 17, 2023 at 7:18
0

You need to bind the docker container 9000 port to your localhost. Use the following command for binding.

docker run -d -p 9000:9000 <your_container_image> 
1
  • I have already done this and the docker container is accessible on localhost:9000. I want to add this address to nginx such that when I open localhost in browser it would connect to the localhost:9000 and return its contents. Nov 16, 2023 at 11:18
0

Did you added these lines in your nginx configuration for configuring reverse proxy from 9000 to 80 port.

location /some/path/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://localhost:8000;
}

If already added this share your output of nginx -t command and also check your logs

1
  • Can you please guide me as to how to do this. The documentation was not very clear about where to set this up. I am not very familiar with nginx. Where in nginx do I set up the reverse proxy and what to use in /some/path Nov 16, 2023 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .