1.官方网址
https://hub.docker.com/_/nginx/
====================================================================
2. 拉取官方镜像
(1) docker pull nginx:1.15.8
(2)官方镜像配置文件目录结构:/etc/nginx
/etc/nginx
|-- conf.d
| |-- default.conf
|-- fastcgi_params
|-- koi-utf
|-- koi-win
|-- mime.types
|-- modules
| |-- ngx_http_geoip_module-debug.so
| |-- ngx_http_image_filter_module.so
| |-- ngx_http_xslt_filter_module-debug.so
| |-- ngx_stream_geoip_module.so
| |-- ngx_http_geoip_module.so
| |-- ngx_http_js_module-debug.so
| |-- ngx_http_xslt_filter_module.so
| |-- ngx_stream_js_module-debug.so
| |-- ngx_http_image_filter_module-debug.so
| |-- ngx_http_js_module.so
| |-- ngx_stream_geoip_module-debug.so
| |-- ngx_stream_js_module.so
|-- nginx.conf
|-- scgi_params
|-- uwsgi_params
|-- win-utf
(3)镜像用户:www-data,nginx
注:nginx.conf文件中通过引入了conf.d目录下的所有配置文件:include /etc/nginx/conf.d/*.conf;
====================================================================
3.准备配置文件到宿主机以便于挂载
(1)从镜像中拷贝出配置文件(镜像中home目录是空的)
->拷贝nginx.conf文件:docker run --rm -v /etc/nginx:/home --privileged=true nginx:1.15.8 mv /etc/nginx/conf.d /home
->拷贝conf.d目录:docker run --rm -v /etc/nginx:/home --privileged=true nginx:1.15.8 mv /etc/nginx/nginx.conf /home
(2)修改配置文件/etc/nginx/conf.d/default.conf
location ~ \.php($|/) {
root /home/wwwroot;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
====================================================================
4. 运行镜像
(1)在文件docker-compose.yml中添加运行项,再运行:docker-compose up -d nginx
nginx:
image: nginx:1.15.8
container_name: nginx
privileged: true
ports:
- 80:80
restart: always
volumes:
- /home/wwwroot:/home/wwwroot
- /etc/nginx/conf.d:/etc/nginx/conf.d
- /var/log/nginx:/var/log/nginx
external_links:
- php7:php
(2)直接运行:
docker run --name nginx -d -p 80:80 --restart always -v /home/wwwroot:/home/wwwroot -v /etc/nginx/conf.d:/etc/nginx/conf.d --link php7:php nginx:1.15.8
====================================================================
5.去掉index.php
location / {
index index.php;
if (-e $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
重启nginx:docker-compose restart nginx