1.下载nginx镜像
docker pull nginx
2.创建挂载目录
mkdir -p /opt/module/nginx/{conf,conf.d,html,logs}
3.运行nginx
方式一:不挂载配置文件
docker run -i -t -d -p 10080:80 nginx /bin/bash
方式二:挂载配置文件
docker run \
--name nginx-10801 \
-d -p 10801:80 \
-v /opt/module/nginx/html:/usr/share/nginx.html \
-v /opt/module/nginx/logs:/var/log/nginx \
-v /opt/module/nginx/conf:/usr/local/nginx/nginx.conf:ro \
-v /opt/module/nginx/conf.d:/usr/local/nginx/conf.d \
nginx-10801
注意:
- 第一个"-v",是项目位置,把项目放到挂载到的目录下即可;
- 第二个"-v",是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
- 第三个"-v",把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致
- 重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录
- 每个 -v的内容里都有一个":",前面的部分表示本地路径,后面的部分表示docker镜像里的路径
4.编辑配置文件
4.1 nginx.conf
vim /opt/module/nginx/conf/nginx.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;
include /etc/nginx/conf.d/*.conf;
client_max_body_size 100M;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
}
4.2 default.conf
vim /opt/module/nginx/conf.d/default.conf
内容如下
server {
listen 80;
server_name localhost;
location / {
root /opt/module/nginx/html;
index index.html index.htm;
autoindex on;
try_files $uri /index/index/page.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
4.3 index.html
vim /opt/module/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>
</head>
<body>
<h1>*********************************** Welcome to scm nginx! ***********************************</h1>
<p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
5.重启nginx
docker restart nginx