In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to build Nginx containers". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to build Nginx containers.
I. HTTP service
The biggest function of Nginx is to build a Web Server. With the container, as long as one line of command, the server is set up, no configuration at all.
$docker container run\-d\-p 127.0.0.2 name mynginx 8080 nginx 80\-rm\-- name mynginx\ nginx
The above command downloads and runs the official Nginx image, which defaults to the latest version (latest), which is currently 1.13.9. If you have installed a previous version on this machine, please delete and reinstall, because only 1.13.9 supports server push.
The parameters of the above command have the following meanings.
-d: running in the background
-p: Port 80 of the container is mapped to 127.0.0.2pur8080
-- rm: automatically delete container files after the container stops running
-- name: container name is mynginx
If there is no error, you can open a browser to access 127.0.0.2pur8080. Normally, the welcome page of Nginx is displayed.
Then, terminate the container, and the container file will be deleted automatically because of the-- rm parameter.
$docker container stop mynginx II. Map the web page directory
The web files are all in the container and cannot be modified directly, which is obviously very inconvenient. The next step is to map the directory / usr/share/nginx/html where the web page file is located to local.
First, create a new directory and enter it.
$mkdir nginx-docker-demo$ cd nginx-docker-demo
Then, create a new html subdirectory.
$mkdir html
In this subdirectory, place an index.html file with the following contents.
Hello World
Next, you can map this subdirectory html to the container's web page file directory / usr/share/nginx/html.
$docker container run\-d\-p 127.0.0.2 volume 8080 PWD/html 80\-rm\-- name mynginx\-- volume "$PWD/html": / usr/share/nginx/html\ nginx
Open a browser, visit 127.0.0.2 Hello World 8080, and you should see it.
Copy the configuration
It is not enough to modify the web page file, but also modify the configuration file of Nginx, otherwise there is no way to add SSL support.
First, copy the Nginx configuration file in the container locally.
$docker container cp mynginx:/etc/nginx.
The above command means to copy the / etc/nginx of the mynginx container to the current directory. Don't miss the last point.
After execution, there should be one more nginx subdirectory in the current directory. Then, rename this subdirectory to conf.
$mv nginx conf
The container can now be terminated.
$docker container stop mynginx IV. Mapping configuration directory
Restart a new container, this time mapping not only the web directory, but also the configuration directory.
$docker container run\-- rm\-- name mynginx\-- volume "$PWD/html": / usr/share/nginx/html\-- volume "$PWD/conf": / etc/nginx\-p 127.0.0.2 name mynginx 80\-d\ nginx
In the above code,-- volume "$PWD/conf": / etc/nginx means to map the container's configuration directory / etc/nginx to the local confs subdirectory.
The browser accesses 127.0.0.2 8080, and if you can see the web page, the local configuration is in effect. At this point, the container can be terminated.
$docker container stop mynginx 5. Self-signed certificate
Now to add HTTPS support to the container, the first thing to do is to generate the private key and certificate. A formal certificate requires the signature of the Certificate Authority (CA). For testing, just get a self-signed (self-signed) certificate.
Next, I refer to DigitalOcean's tutorial. First, make sure your machine has OpenSSL installed, and then execute the following command.
$sudo openssl req\-x509\-nodes\-days 365\-newkey rsa:2048\-keyout example.key\-out example.crt
The parameters of the above command have the following meanings.
Req: processes the certificate signing request.
-x509: generates a self-signed certificate.
-nodes: skip the stage of setting a password for the certificate so that Nginx can directly open the certificate.
-days 365: the certificate is valid for one year.
-newkey rsa:2048: generate a new private key using a 2048-bit RSA algorithm.
-keyout: the newly generated private key file is the example.key in the current directory.
-out: the newly generated certificate file is the example.crt in the current directory.
After execution, the command line will pop up a bunch of questions for you to answer, such as which country you are in, your Email, and so on.
One of the most important issues is Common Name. Normally, you should fill in a domain name, which can be 127.0.0.2.
Common Name (e.g. Server FQDN or YOUR name) []: 127.0.0.2
After answering the question, there should be two more files in the current directory: example.key and example.crt.
Create a new subdirectory certs under the conf directory and put the two files in this subdirectory.
$mkdir conf/certs$ mv example.crt example.key conf/ certs VI, HTTPS configuration
With the private key and certificate, you can open Nginx's HTTPS.
First, open the conf/conf.d/default.conf file and add the following configuration at the end.
Server {listen 443 ssl http2; server_name localhost; ssl on; ssl_certificate / etc/nginx/certs/example.crt; ssl_certificate_key / etc/nginx/certs/example.key; ssl_session_timeout 5m; ssl_ciphers HIGH / etc/nginx/certs/example.crt; ssl_certificate_key / MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on Location / {root / usr/share/nginx/html; index index.html index.htm;}}
Then, start a new Nginx container.
$docker container run\-- rm\-- name mynginx\-- volume "$PWD/html": / usr/share/nginx/html\-- volume "$PWD/conf": / etc/nginx\-p 127.0.0.2 name mynginx 808080 80\-p 127.0.0.2 name mynginx 8081 443\-d\ nginx
In the above command, not only port 80 of the container is mapped, but also port 443, which is the dedicated port of HTTPS.
Open a browser and visit https://127.0.0.2:8081/. Because a self-signed certificate is used, the browser prompts you that it is not secure. Don't worry about it, choose to continue to visit, you should be able to see Hello World.
At this point, I believe you have a deeper understanding of "how to build Nginx containers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.