How to Set up an NGINX Reverse Proxy in Docker [Step

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

NGINX is a popular open source web server. As such, it can be used as a reverse proxy allowing you to set up multiple websites behind one single IP address. This article walks through how to use NGINX with Docker and its built in features for setting up an instance of NGINX that proxies requests back on the internet from your localhost computer or device connected via USB cable

The “nginx reverse proxy docker-compose” is a step by step guide on how to set up an NGINX Reverse Proxy in Docker. This will allow you to use the same application with multiple different server configurations without having to change any of your code.

How to Set up an NGINX Reverse Proxy in Docker [Step

Do you need high-performance web servers to handle requests for your Docker application? When you use Docker with an NGINX reverse proxy, you may handle and control web application requests to and from a containerized application in a variety of ways.

You’ll learn how to set up a Docker NGINX container proxy server and configure it to reverse proxy requests to and from another container in this post. Continue reading to find out more!

Prerequisites

Make sure you have the following items in order to follow along with this tutorial:

  • Docker Desktop 3.5.2 is used in this tutorial.
  • Windows 10 is a new operating system from Microsoft. The tutorial runs Docker on Windows, but the same fundamental techniques may be applied to Linux or macOS as well.
  • If you’re using Linux, you’ll need the Docker engine.

All of the examples in this tutorial will run on Alpine Linux, a basic Linux distribution that runs the most recent version of NGINX. The most recent version is v1.21.1 as of this writing.

Getting Started with NGINX on Docker is a related article.

Creating a PHP-FPM Reverse Proxy with NGINX

PHP, one of the most widely used programming languages, is often used to implement a reverse proxy. In the PHP realm, PHP-FPM (Fast CGI Process Manager) is a fantastic solution for proxying traffic. You may set up PHP-FPM to listen and reply to requests on a network port using PHP-FPM in one Docker container and NGINX in another, aiding in networking two containers together.

Bonus! Throughout the training, there will be examples of utilizing NodeJS as a reverse proxy. Keep an eye out for NodeJS callouts.

Now we’ll build up an NGINX container and a PHP-FPM Docker container to examine how they interact, proxying requests from the browser via NGINX to the PHP-FPM backend and back.

1. Make a directory to save your configuration files in. All of the configuration files required to provision both containers will be found in this directory. The directory C:ArticlesNGINX-PHP is used in this example.

2. Next, construct a Dockerfile using the information shown below. The Dockerfile below instructs the Docker engine to get the nginx:mainline-alpine image from Docker Hub and transfer the NGINX configuration file you’ll be generating into the image to give a bespoke NGINX configuration.

# The image that will be used to get the basic configuration from nginx: mainline-alpine # The location of other files that will be referenced. C:ArticlesNGINX-PHP WORKDIR # COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/ngin

Get the NodeJS Dockerfile from the ATA Scripts Github repository to build up the NodeJS Docker image!

3. Next, construct the docker-compose.yml file, which should include the following information. This Docker Compose file instructs Docker to build two containers, one for web and one for PHP, each running NGINX and PHP-FM.

# The docker-compose specification version is “3.9.” # This service is made up of the following applications: # The host name of the container web will be the NGINX custom container and its name, web: Instead of referring to image: nginx:mainline-alpine, use build to refer to the current directory (.), which will seek for a dockerfile by default. This is C:ArticlesNGINX-PHP build: in this tutorial. # The external directory volume to be mapped to an internal disk: – NGINX-Content:/usr/share/nginx/html C:ArticlesNGINX-Content:/usr/share/nginx/html # The mapping of external ports to internal port mapping ports is as follows: “80:80” – # The name php will also serve as the container’s host name: php: image: php:fpm-alpine php:fpm-alpine php:fpm-alpine php:f – “9000:9000” – “9000:9000” – “9000:90 # Both containers must be able to refer to the same file volumes: – NGINX-Content:/usr/share/nginx/html C:ArticlesNGINX-Content:/usr/share/nginx/html

Everything You Need to Know About Docker Compose is Related

Take a look at the NodeJS Docker. Set up the NodeJS Docker image in the ATA Scripts Github repository using this compose file!

4. Add the following to the default.conf NGINX configuration file. The NGINX configuration file below generates a site listener and passes content requests to the PHP container.

server # The listen 80 port; # The root directory, which must match the internal volume perfectly. /usr/share/nginx/html; # share root /usr/share/nginx/html; Run the following command for all files with the PHP extension: /.+.php(/|$) { # Send the request to the “php” host on port 9000. (default PHP-FPM port). # The “php” host name is created from the previously specified application name in the # Docker Compose file. php:9000 fastcgi pass; # Define one more parameter instructing PHP-FPM where to locate the file fastcgi param SCRIPT FILENAME $document root$fastcgi script name; # Include the default NGINX FastCGI parameters include fastcgi params;

To set up NodeJS in NGINX, get the NodeJS NGINX configuration file from the ATA Scripts Github repository!

5. Navigate to the C:ArticlesNGINX-PHP directory in a terminal session.

6. To build and start your own service, use the command docker-compose up. Both containers will be brought up as a result of this activity.

Docker Compose is used to start the NGINX and PHP-FPM containers.Docker Compose is used to start the NGINX and PHP-FPM containers.

7. Next, in C:, create a PHP file named index.php. Paste the following contents into ArticlesNGINX-Content. This file will be used to see whether NGINX is capable of serving and processing PHP files. To ensure that the container is operating, use the phpinfo() command to generate a PHP informative page.

Download the index.js file from the ATA Scripts Github repository instead of a PHP file!

8. Finally, open a web browser and go to http://localhost/index.php to make sure NGINX is serving the PHP code correctly. PHP version 8.0.8 is returned in the example below, although your version may differ.

Demonstrating that the PHP file is in good working order.Demonstrating that the PHP file is in good working order.

NGINX proxies both NodeJS and PHP-FPM

How would you go about proxying both PHP and NodeJS backends at the same time now that you’ve proxied them separately? Perhaps you’re working on a large application that includes both NodeJS and PHP components. To make this work, you’ll need to add both backends to your Docker Compose file.

1. Make a directory to save your configuration files in. The directory C:ArticlesNGINX-Both is used in this example.

2. Create a Dockerfile and put the information below into it. So far, there isn’t much of a difference between constructing PHP-FPM or a NodeJS container separately.

# The image that will be used to get the basic configuration from nginx: mainline-alpine # The location of any other files that will be referenced. WORKDIR C:ArticlesNGINX-BothNGINX-BothNGINX-BothNGINX-BothNGINX- # Overwrite the old internal settings with the modified default.conf. COPY./default.conf /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf

3. Next, construct the docker-compose.yml file, which should include the following information. You’ll see that a new container named node has been added.

# The docker-compose specification version is “3.9.” # The container collection that makes up your application services: The NGINX custom container web is as follows: Instead of referring image: nginx:mainline-alpine below, use build to # refer to the current directory (.) and it will search for a dockerfile by default build:. # The external directory path that will be mapped to internal volumes: – NGINX-Content:/usr/share/nginx/html C:ArticlesNGINX-Content:/usr/share/nginx/html # Ports that map external ports to internal port mapping ports: “80:80” – image: node:current-alpine node:node:node:node:node:node: # Tell Node to run the index.js file entrypoint instead of the old one: [‘node’,’/usr/share/nginx/html/index.js’] ports: – “3000:3000” – “3000:3000” – “3000:3 # It’s critical for all containers to be able to access the same file volumes: – Articles (C) NGINX-Content:/usr/share/nginx/html image: php:fpm-alpine php:fpm-alpine php:fpm-alpine php:fpm- volumes: “9000:9000” ports: – NGINX-Content:/usr/share/nginx/html C:ArticlesNGINX-Content:/usr/share/nginx/html

4. Next, write the following into the default.conf NGINX configuration file. Take a look at the proxy pass line. The request will be sent to the node host through this line.

server # The listen port is 80; # The root directory should be identical to the internal volume share root /usr/share/nginx/html; # To route all files with the PHP extension to PHP-FPM, use the following command: /.+.php(/|$) # fastcgi pass php:9000; # Send the request to the host “php” and port 9000 (the default PHP-FPM port). Include the default NGINX FastCGI parameters, such as fastcgi params; # Add one more argument to inform PHP-FPM where to look for the file. SCRIPT FILENAME $document root$fastcgi script name; fastcgi param SCRIPT FILENAME $document root$fastcgi script name; # Run the following to redirect all files with the JS extension to the NodeJS location: /.+.js(/|$) # proxy pass http://node:3000; # Pass the request to the host “node” and port 3000 (default NodeJS port)

5. Open a terminal and go to the directory C:ArticlesNGINX-Both.

6. As previously, use the docker-compose up command to construct and start your custom service.

Docker Compose is used to start the NGINX, PHP, and NodeJS containers.Docker Compose is used to start the NGINX, PHP, and NodeJS containers.

To test NGINX proxying both backends, create index.js in the C:ArticlesNGINX-Content directory and index.php in the same directory, if it does not already exist.

index.js

var http = require(‘http’); / Assign the HTTP server object, which is built-in to NodeJS. / Create a server on port 3000 with the words “Hello World!” as the content. res.write(‘Hello World!’); res.end(); http.createServer(function (req, res) res.write(‘Hello World!’); res.end();). console.log(“server started at port 3000”); listen(3000, function());

index.php

8. Finally, go to http://localhost/index.js and http://localhost/index.php in a web browser. As you can see in the screenshot below, both are now available.

Demonstrating the appropriate operation of both the NodeJS and NGINX containers.Demonstrating the appropriate operation of both the NodeJS and NGINX containers.

Conclusion

Why not load-balance several NGINX containers now that you’ve successfully proxied both a PHP-FPM and a NodeJS container, even at the same time?

The use of an NGINX reverse proxy in Docker opens up a whole new universe of possibilities for appropriately segmenting apps and traffic across containers!

In this article, we will learn how to set up an NGINX reverse proxy in Docker. We will also see how to configure the nginx.conf file and how to access it from outside of the container. Reference: docker nginx reverse proxy localhost.

Frequently Asked Questions

Related Tags

  • nginx reverse proxy docker-letsencrypt
  • nginx reverse proxy for docker containers
  • docker-compose nginx reverse proxy with multiple containers
  • best reverse proxy for docker
  • docker nginx reverse proxy not working