Use nginx to reverse proxy multiple docker containers
Introduction
When I introduced docker before, I said that one of its advantages is that multiple environments can be built on the same server and isolated from each other. I did the actual operation yesterday, and I never expected that there would be so many pitfalls, and it would be more time-consuming than building a server from scratch before. After changing several plans, I finally solved it. I will ignore the sadness in the middle and just talk about the operation method.
Recommended tutorial: nginx tutorial
Architecture
The laradock image already exists in the server, which can run the laravel environment and Other PHP includes nginx, mysql, redis, etc.
My idea is to add another nginx container for reverse proxy. Distribute according to the subdomain name, which can be distributed to laradock or any other container (including wordpress, python, java), etc.
Modify lradock
To be modified There is only one place, which is the port nginx listens on.
1. Enter the laradock directory
2. Modify the .env file and modify the NGINX_HOST_HTTP_PORT value from 80 to 8000
3. Stop nginx, docker-compose stop nginx
4. Reinstall nginx, docker-compose build nginx
5. Start docker-compose up -d nginx
At this time, you cannot access it successfully in the browser. Because nginx no longer listens to port 80.
Add nginx container
Add a new nginx container for reverse proxy. When installing laradock, there is already an image of nginx:alpine, and the container can be generated directly. Of course, you can also pull a new image again. (Alpine version is recommended)
1. Check the nginx image ID, docker iamges
2. Generate a new nixn container, docker run --name proxy_nginx -p 80:80 -d [nginx image id]. You can also add the -v parameter to map the configuration file. My environment has less changes and is not needed.
3. When accessed in the browser at this time, it should be the default page of nginx, indicating that the startup is successful
4. Enter the generated container docker exec -it proxy_nginx sh
5. Install vim, apk add vim
6. Check the host ip, /sbin/ip route| awk '/default/ { print $3 }'
7. Add a new configuration file laradock.conf in the /etc/nginx/conf.d/ directory with the following content
server { listen 80; server_name you_site; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://[宿主机IP]:8000; } }
1. Exit Container, and then restart docker restart proxy_nginx
2. Open port 8000, refer to here
Conclusion
If you visit at this time, you can proxy to laradock nginx in it. If you want to add other containers in the future, just add new proxy rules in proxy_nginx.
The above is the detailed content of Use nginx to reverse proxy multiple docker containers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]
