使用docker中的nginx镜像容器部署nginx默认的index页面

1、配置default.conf文件,此文件被nginx.conf文件include后引用

server {
    listen       80;   #监听容器的80端口
    server_name  localhost; # 服务名称

  

           location / {
             root  /usr/share/nginx/html/;  #网站根目录,这里使用的是nginx容器默认的index存放目录
             index  index.html;   # 访问的首页名称
       }
}

2、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;
}

3、执行 docker run -it  -p 8080:80  --name nginx    将nginx容器的80 端口映射为宿主机的8080端口,如果宿主机是阿里云服务器,需要开放8080端口供外网访问

 

4、在宿主机上执行curl http://localhost:8080

 5、在windows机器上通过外网访问阿里云宿主机

 

6、停止nginx容器 使用命令:docker stop  容器id      

7、重新启动nginx容器使用命令: docker restart    容器id 

8、以bash命令形式进入容器:docker exec -it  容器id/bin/bash

posted @ 2023-04-20 11:13  无声袖箭  阅读(742)  评论(0)    收藏  举报