docker-compsoe启动nginx
创始人
2024-05-28 21:32:09
0

本次采用的是nginx:1.20版本

下载命令

docker pull nginx:1.20

docker-compose.yml

version: '3'
services: nginx:restart: always image: nginx:1.20container_name: nginx1.20ports:- 80:80volumes: - /home/nginx-docker/nginx.conf:/etc/nginx/nginx.conf- /home/nginx-docker/log:/var/log/nginx- /home/nginx-docker/conf.d/default.conf:/etc/nginx/conf.d/default.conf- /home/nginx-docker/html:/usr/share/nginx/html

在启动docker compose之前需要以下两个配置

1.创建nginx.conf文件

这个文件是直接从1.20版本容器内部复制出来的,其他版本可以自行复制

user  nginx;
worker_processes  auto;error_log  /var/log/nginx/error.log notice;
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;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

2.创建default.conf文件

这个文件是直接从1.20版本容器内部复制出来的,其他版本可以自行复制

server {listen       80;listen  [::]:80;server_name  localhost;#access_log  /var/log/nginx/host.access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}
}

3.移动文件

根据配置文件的挂载得知:
需要把nginx.conf放在/home/nginx-docker/目录里面
default.conf放在/home/nginx-docker/conf.d/目录里面

 - /home/nginx-docker/nginx.conf:/etc/nginx/nginx.conf- /home/nginx-docker/conf.d/default.conf:/etc/nginx/conf.d/default.conf

4.至此配置完成,启动容器

docker compose up -d

此时访问可能没有nginx欢迎界面,是因为我把html目录挂载出来了,所以需要在/home/nginx-docker/html创建一个index.html进行显示




Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

至此结束

在这里插入图片描述

相关内容

热门资讯

问法预告|12月23日半岛问法... 养老继承是社会生活中谁也绕不开的话题,当面临这一问题时家庭成员之间难免会发生一些矛盾。当问题真正来临...
原创 泰... 这座桥在中国企业的支持下,刚通车不久,却因为一场突如其来的空袭被炸成废墟,显然这场冲突的复杂程度远远...
原创 为... 中国古代的社会经济主要依赖自给自足的小农经济,生产力发展相对滞后,但战争却始终贯穿着王朝的历史。从某...
一起普通师生纠纷,牵动全县多个... 今年9月底,一起因拉人进群而被跨省传唤的事件,登上新闻热搜。 澎湃新闻近日调查发现,该事件背后,是一...
如何在刑事律师推荐榜中选到合适... 刑事律师的重要性在法律领域,刑事律师扮演着关键角色。 当个人或企业面临刑事案件时,专业刑事律师能提供...
刑事律师排名怎么选到合适律师? 刑事律师排名的意义刑事律师排名在一定程度上能反映律师的专业能力、经验和业界认可度。 对于面临刑事案件...
制度深耕 丰景如峰 大豆收获作业。庞遵明摄 □本报记者 姜斌 刘畅 银装素裹的北大荒,是一卷由冰雪、沃土与数据共同谱写的...
每经热评|告慰小洛熙,唯有权威... 每经评论员 付克友 宁波5月龄女婴“小洛熙”在医院接受心脏手术后不幸离世,连日来引发舆论关注。一个尚...
从同仁堂涉假等案件谈:岂能将法... 我们生产网络舆情和危机管理专业有用的观点! 文/燕博士 临近年末,出现了两种类型的舆情。 一是,一些...
新疆乌苏银发调解员专解邻里纠纷 11月30日,新疆维吾尔自治区乌苏市寒意渐浓,乌苏市公安局虹桥街道派出所“夕阳红”调解室里却暖意融融...