安装nginx docker镜像,获取nginx官方镜像
docker pull nginx
查看镜像库
docker images
使用nginx镜像来创建nginx容器实例
docker run --name nginx-test -p 80:80 -d nginx
创建容器成功后,启动nginx容器
docker run --name nginx-test -p 80:80 -d nginx
访问本机测试,将nginx关键目录映射到本机,首先在本机创建nginx的一些文件存储目录
mkdir -p /root/nginx/www /root/nginx/logs /root/nginx/conf
查看nginx-test容器id
docker ps -a
将nginx-test容器配置文件copy到本地
docker cp c719d006e6f9:/etc/nginx/nginx.conf /root/nginx/conf
创建新nginx容器nginx-web,并将www,logs,conf目录映射到本地
docker run -d -p 80:80 --name nginx-web -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/logs:/var/log/nginx nginx
启动nginx
docker start nginx-web
在本机/root/nginx/www目录下创建index.html内容为
[root@localhost ~] <!DOCTYPE html> <html> <body> <p><em>Thank you for using nginx</em></p> </body> </html>
完成后重新访问本机,打开网页输入本机ip
设置反向代理
进入到/root/nginx/conf/nginx.conf 添加如下即可
server{ listen 80; charset utf-8; server_name 192.168.181.128; location / { proxy_pass http://192.168.181.128:8080; proxy_redirect default; } }
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; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; server{ listen 80; charset utf-8; server_name 192.168.181.128; location / { proxy_pass http://192.168.181.128:8080; proxy_redirect default; } } }
这样就可以用本机80端口代理本机8080端口了,直接使用80访问端口
删除指定容器
docker stop nginx-web docker rm -f nginx-web
docker设置自动启动
systemctl list-units --type=service systemctl list-unit-files | grep enable systemctl enable docker systemctl disable docker
docker容器设置自动启动
docker run -d -p 80:80 --name nginx-web --restart=always -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/logs:/var/log/nginx nginx
docker update --restart=always nginx-web
原文链接:https://blog.csdn.net/zhongguootngxu/article/details/117629480
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END