首页 > 开发 > Nginx > 正文

Docker容器中运行nginx

2020-07-28 15:35:50
字体:
来源:转载
供稿:网友
本文介绍了从docker hub拉取官方nginx镜像并自定义部分配置,绑定端口运行的过程。希望小伙伴们能够从中得到些知识

nginx简介

Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势。与旧版本(<=2.2)的Apache不同,nginx不采用每客户机一线程的设计模型,而是充分使用异步逻辑,削减了上下文调度开销,所以并发服务能力更强。整体采用模块化设计,有丰富的模块库和第三方模块库,配置灵活。 在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。

docker hub拉取

docker hub 是docker官方的镜像源,里面有做好的nginx docker image,当然也可以发布自己的镜像到上面去。

如果官方镜像速度很慢,可以考虑用

阿里云的docker镜像仓库

主要步骤如下:

登录阿里云的docker镜像仓库
获取专属加速器地址
升级docker客户端(建议在1.6.0以上)
修改daemon配置文件(阿里已经给出了所有的命令,基本上只要copy paste即可)
然后使用

docker pull nginx

就可以快速下载官方的nginx docker image了。

基础的docker命令可以参看Docker初体验

Nginx docker image

在docker官方的页面上,有部分样例的说明。可以发现,网站的主目录是 /usr/share/nginx/html ,这跟我搜到的一些文章的说明不同。

我的需求是实现nginx搭建的文件浏览站。说白了就是下载站。实验室(or校园内)共享文件。原来采用的是 Python救急HttpServer和Ftpserver ,尽管后来采用了 python多线程启动httpserver ,但还是经常因为卡线程的问题,导致地址访问失败。所以那个方法作为临时应急工具还是可以的,但是如果想要长期共享文件,必须采用有完善功能的http server。

nginx的配置文件都在 /etc/nginx/ 下面,可以看到熟悉的 conf.d 文件夹,明显里面是用户自定义配置文件的位置。

修改自定义配置

default.conf文件内容如下:

server {  listen    80;  server_name localhost;  #charset koi8-r;  #access_log /var/log/nginx/log/host.access.log main;  root  /usr/share/nginx/html;  location / {    root  /usr/share/nginx/html;    index index.html index.htm;    ## 下面三行是添加的。    autoindex on;    autoindex_exact_size on;    autoindex_localtime on;  }  #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;  #}}

只需要完整的复制出来,并添加

autoindex on;autoindex_exact_size on;autoindex_localtime on;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表