学习docker 部署nginx记录

docker pull nginx

$ docker pull nginx
$ docker run --name nginx-test -p 8081:80 -d nginx

docker config

$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
$ docker cp  d624b3debbc9(namenginx-test容器id 运行删除括号内容):/etc/nginx/nginx.conf ~/nginx/conf

  • www: 目录将映射为 nginx 容器配置的虚拟目录。
  • logs: 目录将映射为 nginx 容器的日志目录。
  • conf: 目录里的配置文件将映射为 nginx 容器的配置文件。

docker nginx 挂载相应的文件

$ docker run -d -p 8082:80 \
     --name nginx-test-web \
     -v ~/nginx/www:/usr/share/nginx/html \
     -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
     -v ~/nginx/logs:/var/log/nginx \
     nginx

  • -p 8082:80: 将容器的 80 端口映射到主机的 8082 端口。

  • --name nginx-test-web:将容器命名为 nginx-test-web。

  • ~/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。

  • -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。

  • -v ~/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。

部署dist

将实现准备好的静态网页放置在~/nginx/www

$ docker kill -s HUP container-name
$ docker restart container-name

HTML5 History 模式 设置重定向

nginx

location / {
  try_files $uri $uri/ /index.html;
}

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;

    server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /usr/share/nginx/html;

        index index.html;

        #   server_name you.server.com;

        location / {
            try_files $uri $uri/ @rewrites;
        }

        location @rewrites {
            rewrite ^(.+)$ /index.html last;
        }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            # Some basic cache-control for static files to be sent to the browser
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }
}
posted @ 2019-05-11 11:56  Pursue`  阅读(455)  评论(0编辑  收藏  举报