参考 https://www.w3cschool.cn/docker/docker-install-nginx.html
访问 NGINX 镜像库地址: https://hub.docker.com/_/nginx?tab=tags
下载官方镜像
docker pull nginx:latest
或者 docker pull nginx
默认就是latest
docker images
// 查看镜像信息
docker inspect nginx
版本号 1.19.0
对于nginx服务,我们可能经常要看错误日志,尤其是在调试的初期;然后有可能需要运行多个项目,所以配置文件放在容器
外面会方便编辑;如果nginx只作为反向代理的话,那没必要挂载项目目录,如果还提供了静态资源或页面的服务,那么就需
要挂载项目目录啦,当然,为了通用起见还是挂载好,因为保不齐哪天你需要开发web什么的。
nginx镜像中的默认配置
日志位置:/var/log/nginx/
配置文件:/etc/nginx/nginx.conf; /etc/nginx/conf.d/
文件位置:/usr/share/nginx/html,这是nginx静态目录,不用修改,不用挂载。
本机创建目录和文件
项目目录:mkdir -p /data/www/quwan
日志位置:mkdir -p /data/dockers/nginx/logs
配置文件:mkdir -p /data/dockers/nginx/conf/conf.d
准备文件,内容贴在后面:
/data/dockers/nginx/conf/nginx.conf
/data/dockers/nginx/conf/conf.d/default.conf
总上所述,nginx容器的场景方式为:
1、(推荐)使用host网络启动:fastcgi_pass 127.0.0.1:9000
docker run --name nginx --network host -v /data/www/quwan:/www -v /data/dockers/nginx/logs:/var/log/nginx/ -v /data/dockers/nginx/conf/conf.d:/etc/nginx/conf.d/ -v /data/dockers/nginx/conf/nginx.conf:/etc/nginx/nginx.conf --privileged=true -d nginx
2、nginx与php-fpm通过外网IP访问:fastcgi_pass ip:9000
docker run --name nginx -p 80:80 -v /data/www/quwan:/www -v /data/dockers/nginx/logs:/var/log/nginx/ -v /data/dockers/nginx/conf/conf.d:/etc/nginx/conf.d/ -v /data/dockers/nginx/conf/nginx.conf:/etc/nginx/nginx.conf --privileged=true -d nginx
3、nginx与php-fpm通过容器链接的访问:fastcgi_pass php-fpm:9000
docker run --name nginx -p 80:80 -v /data/www/quwan:/www -v /data/dockers/nginx/logs:/var/log/nginx/ -v /data/dockers/nginx/conf/conf.d:/etc/nginx/conf.d/ -v /data/dockers/nginx/conf/nginx.conf:/etc/nginx/nginx.conf --privileged=true --link php-fpm -d nginx
进入容器查看
docker exec -it nginx bash
find / -name nginx.conf
/etc/nginx/nginx.conf
ls /etc/nginx
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
exit 退出容器
重启容器
docker restart nginx
测试
vi /data/www/quwan/hello.php
<?php
phpinfo();
?>
curl http://localhost/
curl http://localhost/hello.php
运行正常
修改配置文件后
docker exec -it nginx nginx -s reload
或者
docker restart nginx
附录:
1、/data/dockers/nginx/conf/nginx.conf
user nginx;
worker_processes 10;
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;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
2、/data/dockers/nginx/conf/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /api/ {
root /www ;
if (!-e $request_filename){
rewrite ^/(.*)$ /api/public/index.php?/$1 last;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
$fastcgi_script_name 指的就是 /api/public/index.php?/$1
$document_root 就是 /www ,于是转发到 php-fpm 的就是 /www/api/public/index.php?/$1,所以php-fpm那边
要能解析到这个地址,否则就是404
docker安装nginx镜像
最新推荐文章于 2024-10-11 08:13:26 发布