安装您自己的 hello world 页面很容易。我用官方的解释一下nginx:latest
图像,但如果您愿意,您可以使用 bitnami 图像自己完成。
首先是非常基本的。只需运行 nginx 容器(无需 docker-compose)。我将详细和基本地解释它,当然我可以尝试执行更高级或更快的命令来读取容器内的文件,但这对于初学者来说可能会感到困惑。所以只需运行容器并为其命名my-nginx
:
$ docker run --rm -d -p 80:80 --name my-nginx nginx
Go to localhost:80
,您将看到默认的 nginx 页面。
现在您可以使用容器的名称在容器内执行。 exec 会将您带入“容器内部”,以便您可以检查其文件。
$ docker exec -it my-nginx bash
root@2888fdb672a1:/# cd /etc/nginx/
root@2888fdb672a1:/etc/nginx# ls
conf.d koi-utf mime.types nginx.conf uwsgi_params
fastcgi_params koi-win modules scgi_params win-utf
现在阅读nginx.conf
通过使用cat
。
该文件中最重要的行是:
include /etc/nginx/conf.d/*.conf;
这意味着所有confs
在该目录内使用/读取。
所以进入/etc/nginx/conf.d/
.
root@2888fdb672a1:~# cd /etc/nginx/conf.d/
root@2888fdb672a1:/etc/nginx/conf.d# ls
default.conf
The default.conf
是唯一的文件。在此文件中您可以看到配置:
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
服务器是本地主机,端口是80,将显示的文件在目录中/usr/share/nginx/html/
现在检查容器中的该文件:
root@2888fdb672a1:/etc/nginx/conf.d# cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
...
这是预期的文件。我们可以看到“Welcome to Nginx”页面。
那么我们怎样才能展示自己的index.html
?只需将其安装在/usr/share/nginx/html
.
You'll docker-compose.yaml
会看起来像这样。
version: '3'
services:
web:
image: nginx:latest
volumes:
- ./code:/usr/share/nginx/html
ports:
- "80:80"
代码目录只包含一个index.html
与你好世界。
跑步docker-compose up -d --build
当你卷曲时localhost:80
你会看到你自己的index.html
.
如果您确实想将代码放入/var/www/html
代替/usr/share/nginx
你可以做到的。
用你的test.conf
。在这里您定义将文件放入/var/www/html/
:
server {
listen 80;
listen [::]:80;
server_name test.local;
index index.html; #Only a basic helloworld index.html file
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
}
在撰写中,您将覆盖default.conf
用你自己的conf告诉nginx去查看/var/www/html
。
您的撰写可能如下所示:
version: '3'
services:
web:
image: nginx:latest
volumes:
- "./test.conf:/etc/nginx/conf.d/default.conf"
- "./code:/var/www/html"
ports:
- "80:80"
现在您还将看到自己的index.html
当它位于您自己指定的位置时。答案很长,但我希望这会有所帮助。