docker安装Nginx教程讲解

1:熟悉docker基础命令

  下载最新版nginx:   docker pull  nginx:last
  
  然后随便启动一下镜像,生成一个容易。进入容器 我们查看容器内部文件结构

  1:docker  run nginx  启动容器
  2:docker ps 查看自己启动的容器Id (docker ps -a  查看自己启动的和未启动的容器)
  3:docker exec -it 8bf811453641 /bin/bash   进入容器

1:

 查看三个文件,把配置文件,跟静态文件路径挂载出来
 容器内部
 /usr/share/nginx/html     静态文件路径
 /etc/nginx/nginx.conf      配置文件路径
 /var/log/nginx                 日志文件路径
 /etc/nginx/conf.d            配置文件路径

2:

1:新建挂载的文件目录及文件夹
   mkdir -p /data/nginx/{conf,conf.d,html,logs}
2:新建配置文件
  配置
  nginx配置文件
 /data/nginx/conf/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;

  server {
    
    
    listen    80;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    
    
      root  /usr/share/nginx/html;
      index index.html index.htm;
    }

    #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  html;
    }

  }
  
  include /etc/nginx/conf.d/*.conf;
}

2:/data/nginx/conf.d/default.conf

server {
    
     
  listen    80; 
  server_name localhost; 
 
  #charset koi8-r; 
  #access_log /var/log/nginx/log/host.access.log main; 
 
  location / {
    
     
    #root  /data/nginx/html; 
    root  /usr/share/nginx/html; 
    index index.html index.htm; 
    #autoindex on; 
    #try_files $uri /index/index/page.html; 
    #try_files $uri /index/map/page.html; 
  } 
 
  #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; 
  #} 
}

3:/data/nginx/html/index.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>系统时间</title>
</head>
<body>
<h1 id="datetime">
  <script>
    setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
  </script>
</h1>
</body>
保存好后关闭原先nginx容器并删除
 1:docker stop nginx  关闭
 2:docker rm nginx  删除   (注意:如果没有关闭的必须先关闭)

查看我们的nginx镜像
1:docker images
最后启动

 启动命令
 docker run --name nginx-test -d -p 9999:80 -v /data/nginx/html:/usr/share/nginx/html -v     /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest


 命令解析
docker run --name nginx-test -d -p 9999:80                       //这个表示启动的nginx容器  本机9999端口对于容器的80端口  9999>80
-v /data/nginx/html:/usr/share/nginx/html                           //静态文件挂载目录 -v 本机目录:容器文件目录
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf          //配置文件挂载
-v /data/nginx/logs:/var/log/nginx                                       //日志挂载
-v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest        

vue页面刷新导致404 请用这个配置文件

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;

  server {
    
    
    listen    80;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    
    
      root  /usr/share/nginx/html;
      index index.html index.htm;
	  try_files $uri $uri/ /index.html;
    }

    #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  html;
    }

  }
  
  include /etc/nginx/conf.d/*.conf;
}

猜你喜欢

转载自blog.csdn.net/qq_26856361/article/details/110668838