docker部署Nginx

docker部署Nginx

下载docker镜像

  1. 启动docker
  2. 运行命令 docker nginx:版本号 下载某版本的Nginx

启动Nginx容器

  1. docker run -d --name test-nginx -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/develop:/develop -p:80:80 nginx 启动Nginx并将容器命名为test-nginx、映射主机端口80
    • 运行测试
    [root@localhost ~]# docker run -d --name test-nginx -p:80:80 nginx
    e8d788fb291ae633a33cfaea34cb61a4665c32d4022dd8d29f1f386ecd615d2e
    [root@localhost ~]# curl localhost:80
    <!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>
    
    • -d 后台运行
    • –name 给容器命名
    • -p 暴露端口, 宿主机端口:容器内部端口
    • -v 挂载容器目录至主机

关于docker中运行Nginx容器之后项目无法访问的问题-Nginx配置

  1. Nginx需要暴露配置文件和项目部署文件
    • Nginx配置文件:/etc/nginx/nginx.conf
    • Nginx项目文件:/develop
    • 可选文件:日志文件
  2. Nginx配置文件中的路径应为容器内映射地址的路径
    http {
        include       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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  服务器地址或域名;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   /develop/项目路径;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    
  3. 关于在外部修改配置文件之后需要重启容器

转载请注明出处

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值