docker 运行 nginx 实际项目部署 详细大全
先提前看一眼命令:
docker run -d -p 8082:80 --name nginxTest -v /opt/dockervols/nginx/ssl:/etc/nginx/ssl -v /opt/dockervols/nginx/logs:/var/log/nginx -v /opt/dockervols/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/dockervols/nginx/html:/usr/share/nginx/html -v /opt/dockervols/nginx/conf.d:/etc/nginx/conf.d nginx:1.19
先在宿主机提前创建文件或文件夹,一定要!!!
宿主机 /opt/dockervols/nginx/ssl 文件夹,文件夹内容为 私钥+公钥证书(4313816__xxx.pem,4313816__xxx.key; 提前在宿主机创建好) , 映射容器内 /etc/nginx/ssl
宿主机 /opt/dockervols/nginx/logs 文件夹,文件夹内容为 access.log和error.log ,这两个文件提前在宿主机创建好 映射容器内 /var/log/nginx
宿主机 /opt/dockervols/nginx/nginx.conf 提前创建好此文件 nginx.conf,其内容可以从容器中先copy一份默认的配置 ,映射容器内 /etc/nginx/nginx.conf 文件
nginx.conf:
user nginx;
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;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
宿主机 /opt/dockervols/nginx/html 文件夹 ,其下面内容 50x.html index.html 提前创建好这两个文件 ,其内容可以从容器中先copy一份默认的配置 ,映射容器内 /usr/share/nginx/html
50x.html:
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</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 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>
宿主机 /opt/dockervols/nginx/conf.d 文件夹,其里面内容 default.conf 提前创建好此文件 default.conf,其内容可以从容器中先copy一份默认的配置 ,映射容器内 /etc/nginx/conf.d
default.conf:
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#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;
#}
}
注意:以上只有宿主机中只有 nginx.conf是文件,其他均是文件夹
只要在宿主机提前创建好了文件夹和文件,运行命令后 容器内也会出现对应的文件,
例如 宿主机中 /opt/dockervols/nginx/ssl/4313816__xxx.pem和/opt/dockervols/nginx/ssl/4313816__xxx.key 创建好后,容器内/etc/nginx/ 下本不存在的ssl文件夹和4313816__xxx.pem,4313816__xxx.key 也会被创建,大功告成!!
快乐运行命令即可。
完成后在宿主机 nginx目录下,创建两个命令 test.sh 和 reload.sh
test.sh:
docker exec nginx129 nginx -t #nginx129为容器名称,后面的nginx固定
给test.sh赋予可执行权限,chmod +x test.sh
reload.sh:
docker exec nginx129 nginx -s reload #nginx129为容器名称,后面的nginx固定
给reload.sh赋予可执行权限,chmod +x reload.sh
注意:修改完nginx里的 配置文件后(default.conf) 后先执行test.sh 然后执行 reload.sh
docker run -d -p 8082:80 --name nginxTest -v /opt/dockervols/nginx/ssl:/etc/nginx/ssl -v /opt/dockervols/nginx/logs:/var/log/nginx -v /opt/dockervols/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/dockervols/nginx/html:/usr/share/nginx/html -v /opt/dockervols/nginx/conf.d:/etc/nginx/conf.d nginx:1.19
最后:项目中 实际要监听80和443端口启动命令
docker run --name nginx --net=host -v /opt/dockervols/nginx/ssl:/etc/nginx/ssl -v /opt/dockervols/nginx/logs:/var/log/nginx -v /opt/dockervols/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/dockervols/nginx/html:/usr/share/nginx/html -v /opt/dockervols/nginx/conf.d:/etc/nginx/conf.d -d nginx:1.19