使用docker安装nginx并配置端口转发
使用docker安装并运行nginx命令:
docker run --name=nginx -p 80:80 -d docker.io/nginx
使用命令:
docker exec -it nginx /bin/bash 进入容器可查看到几个重要的文件
配置文件:nginx.conf 在 /etc/nginx/nginx.conf
日志文件: /var/log/nginx/access.log /var/log/nginx/error.log
使用cat命令打开nginx.conf
root@dc048fc59765:/var/log/nginx# cat /etc/nginx/nginx.conf user nginx;
worker_processes ; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; events {
worker_connections ;
} 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 ; #gzip on; include /etc/nginx/conf.d/*.conf;
}
root@dc048fc59765:/var/log/nginx#
发现第32行,配置了一个子配置文件,进入conf.d发现有一个default.conf文件
打开default.conf文件:
root@dc048fc59765:/etc/nginx/conf.d# cat default.conf
server {
listen ;
listen [::]:;
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 /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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;
#}
} root@dc048fc59765:/etc/nginx/conf.d#
在浏览器中输入:http://192.168.11.241

从default.conf中11行可以看出,index页面在/usr/share/nginx/html
现在,我们配置一下容器卷:
docker rm -f nginx 删除一下之前的容器
再次执行比较完整的命令:
docker run \
--restart=always \
--name nginx \
-d -p : \
-v /data/nginx/html:/usr/share/nginx/html \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/log:/var/log/nginx \
nginx
这里有几个注意事项:
(1)第一个“-v”,是项目位置,把html代码放到挂载到的目录下即可;
(2)第二个“-v”,是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",
这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
(3)第三个“-v”,把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致
(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录
我们先启动一下,可以发现是有问题的,因为配置文件还没有。
[root@zuul-server data]# docker run --name nginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/nginx/log:/var/log/nginx nginx
c8d49810b4afd4b6661beb942f0f19a67cf64f9798af9d2eb8a2aa242b2af434
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/data/nginx/nginx.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged\\\" at \\\"/var/lib/docker/overlay2/ee154ee69264707a542409b514cfff950b31cefa4dcd4e66c3635d0aa94f5058/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
很明显,报错了,容器也没有启动成功,怎么解决了?
解决办法:
1 进入/data/nginx目录,
2 删除nginx.conf文件夹 rm -rf nginx.conf
3 新建nginx.conf文件 touch nginx.conf
4 然后把之前cat /etc/nginx/nginx.conf的内容放入到nginx.conf
5 同理,cd /data/nginx/conf.d ,touch default.conf,把之前 cat /etc/nginx/conf.d/default.conf中的内容放入到新建的default.conf中。
最后 docker restart nginx 搞定。

使用docker安装nginx并配置端口转发的更多相关文章
- windows下nginx中配置端口转发 ----本文来自转载
什么是端口转发 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 转载原文链接:https://www.cnblogs.com/chanshuyi/ ...
- linux中nginx中配置端口转发
域名指向主机IP地址,通过域名:8080才能访问网站,去掉后面的8080:或者其他的端口号,直接使用域名访问网站 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...
- docker安装nginx,配置SSL
nginx安装 下载镜像并测试 1.docker pull nginx 2.docker images nginx 查看我们拉取到本地的nginx镜像IMAGE ID 3.首先测试下nginx镜像是否 ...
- docker安装nginx并配置通过https访问
1. 下载最新的nginx的docker image docker pull nginx:latest 创建挂载路径 2.准备nginx需要的文件 nginx的配置文件 首先是nginx.conf文件 ...
- 使用docker部署nginx并配置https
我只有一台服务器,但我想在这台服务器上运行多个项目,怎么办? 总不能靠加端口区分吧? 百度和Google是个好东西,于是我找到了答案,使用nginx. 通过nginx,我可以给我的一台服务器配置两个域 ...
- Windows下Nginx的安装与使用(一):配置端口转发
什么是端口转发 当我们在服务器上搭建一个图书以及一个电影的应用,其中图书应用启动了 8001 端口,电影应用启动了 8002 端口.此时如果我们可以通过: localhost:8001 //图书 lo ...
- Docker 安装 Nginx 负载均衡配置
Docker 安装 # 1)安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2)添加Docker软件包源(否则d ...
- Linux、Docker安装Nginx
Docker安装Nginx #docker images nginx #docker search nginx #docker pull nginx #docker run -it -p 8084:8 ...
- Nginx实现数据库端口转发
前言 因开发.测试.生成等服务器网络策略问题,导致部分服务器A需要访问数据库而无法正常访问数据库,此处采用端口代理方式解决此问题,即通过一台能正常访问数据库的服务器B做tcp端口代理,实现服务器A通过 ...
随机推荐
- Kafka入门(1):概述
摘要 在本文中,我将从为什么需要消息队列开始讲起,举两个小例子,跟你聊聊目前消息队列的一些使用场景. 比如消息队列在复杂系统中的解耦,又比如消息队列在高并发下的场景如果让流量变得更平缓. 随后我会跟你 ...
- 用python实现栈/队列/双端队列/链表
栈是元素的有序集合,添加操作与移除操作都发生在其顶端,先进后出栈操作:创建空栈,增删(顶端),查(顶端元素,元素个数,是否为空)应用:将十进制数转换成任意进制数 class Stack: # 用列表创 ...
- 数据可视化之DAX篇(十)在PowerBI中累计求和的两种方式
https://zhuanlan.zhihu.com/p/64418286 假设有一组数据, 已知每一个产品贡献的利润,如果要计算前几名产品的贡献利润总和,或者每一个产品和利润更高产品的累计贡献占总体 ...
- Linux-常见的命令
1.杀掉tomcat进程 ps -ef |grep tomcat kill -9 pid 2.启动http服务 service httpd start 3.停止mysql服务 servi ...
- Ethical Hacking - GAINING ACCESS(6)
Server Side Attack Analysing scan results and exploiting target system. Go to the Analysis page and ...
- Disease Manangement 疾病管理
题目描述 Alas! \(A\) set of \(D (1 <= D <= 15)\) diseases (numbered \(1..D\)) is rshning through t ...
- 虚拟DOM Vitural DOM Tree
提起Virtual DOM,总是给人一种高深莫测的感觉,大家都知道它比DOM快.那么Virtual DOM到底是何方神圣呢?在深入理解Virtual DOM之前,先让我们回顾一下DOM. 一.什么 ...
- 牛客练习赛 66B题解
前言 当初思路 开始没想到异或这么多的性质,于是认为对于每个点\(u\),可以和它连边的点\(v\)的点权 \(a_v=a_u \oplus k\)(证明:\(\because\) \(a_u\opl ...
- Intelij DataGrip 的安装和使用
链接: Intelij DataGrip 安装教程以及汉化教程 Intelij DataGrip 使用教程 以上两个教程已使用过,没有问题 如有侵权请联系删除
- 推特(Twitter)如何绑定谷歌二次验证码/谷歌身份验证/双重认证?
1.下载Twitter,找到双重验证界面 手机连接VPN下载Twitter(获取免费VPN可加微信客服“Ecyzm-”),注册登陆后,点左上角账户头像-Settings and privacy - A ...