Ubuntu环境使用Docker安装Nginx
原文链接来自于:
https://www.jianshu.com/p/481feafd2f3f
不过里面有些许错误和对新手来讲可能没有说清楚的地方
- 首先当然时pull nginx镜像
docker pull nginx
- 需要在你的Ubuntu环境下建立几个文件夹,用来挂载
切换到根目录下(执行ls命令可以看到 bin usr var home之类的)
挂载 nginx 的静态文件目录 mkdir /dir/html
挂载 nginx 的配置目录 mkdir /dir/conf/conf.d
挂载 nginx 的日志目录 mkdir /dir/log
在根目录下建立上面的文件夹目录,不嫌麻烦的话可以手动一级一级建立
3. 配置文件
将 nginx.conf 配置文件放在 /dir/conf/ 下。
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
autoindex on;
gzip on;
client_max_body_size 100M;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
include /etc/nginx/conf.d/*.conf;
}
将 default.conf 配置文件放在 /dir/conf/conf.d 下。
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/access.log main;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
- 在html目录下执行
vim index.html
在vim编辑器里面加上你想看到的内容
<html>
<h1>Nginx!!!</h1>
</html>
- 通过命令启动容器
docker run \
--name docker-nginx \
-d -p 89:80 \
-v /dir/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /dir/conf/conf.d:/etc/nginx/conf.d \
-v /dir/html:/usr/share/nginx/html \
-v /dir/log:/var/log/nginx \
nginx
最后再通过localhost:89这个网址即可访问我们刚才写好的nginx静态页面。若是你的docker是运行在虚拟机上的,在宿主机上访问注意改成虚拟机的IP地址。
最后的效果:

注意我这里相当于设置的时绝对路径,一定要确保你的路径是正确的,目录不对的时候会报错。细心就能解决。
本文档详细介绍了在Ubuntu环境中如何使用Docker安装Nginx,包括创建挂载目录、配置文件放置以及启动容器的步骤。在完成配置后,可以通过localhost:89访问Nginx的静态页面。注意路径的正确设置,以避免因目录错误导致的问题。
&spm=1001.2101.3001.5002&articleId=91472755&d=1&t=3&u=7f36d71846854f0cae46c56324492a49)
417

被折叠的 条评论
为什么被折叠?



