How to deploy nginx in docker application

作者:京仲轶 | 发布日期:2024-09-20 09:18:01

Docker installs nginx
Building the Nginx server under Docker is mainly divided into pre liminary pre paration, static resource deployment, port binding, domain name binding, reverse proxy, load balancing, and solving front-end AJAX cross-domain Questions and steps for HTTPS configuration.
Before installation, you first need to create a Docker container named `nginx-container`, map port 80 of the host to port 80 of the container, and change the local `nginx.conf `Mount the file into the configuration file in the container to achieve overwriting of the configuration file. At the same time, the log file will be mounted to the `/home/docker-nginx/log/` directory of the host machine to facilitate log management. Use the `-d` parameter when starting the container to run in background mode.
Static resource deployment can be achieved by storing files in the `~/nginx/html` directory, so that static resources can be accessed by directly accessing the directory in the browser.
You can use the `vim` editor to add custom configurations to the configuration file. For example, to configure independent ports for each project, use the `static.conf` file for port binding.
When binding a domain name, the corresponding domain name needs to be resolved to the IP address of the container. The reverse proxy function is used to forward client requests to back-end servers, such as Tomcat, to achieve load balancing.
When configuring load balancing, you can define the backend server group by setting the `upstream` block, such as `upstreamtomcat-kkb`. Specify each server and its corresponding weight in this block. The larger the weight value, the higher the priority. For example, set the weights of two servers to 2 and 1 respectively.
To solve the front-end AJAX cross-domain problem, you usually need to add instructions such as `proxy_pass` and `proxy_set_header` in the Nginx configuration file to achieve cross-domain resource sharing (CORS).
For HTTPS configuration, you need to pre pare the SSL certificate and configuration file, and use the `ssl` directive to configure the SSL listening port and certificate path in `nginx.conf`. Make sure all HTTPS-related configurations are correct for secure web access.
Deploy nginx in docker environment
In this article, we will briefly introduce how to deploy nginx in docker environment to help everyone better understand and operate.
First, you need to pull the nginx image. Next, create a mapping folder locally.
Then, start the container and copy the files in the container to the corresponding directory of the host machine.
After copying the files in the container to the corresponding directory of the host, modify the configuration file according to business needs.
After completing the configuration modification, restart the container to verify the changes.
If you need to delete the created container, you can follow the corresponding operations.
Start the container to complete the deployment.
Finally, verify to make sure everything is working properly.