Windows本地虚拟机的docker部署nginx并访问

镜像下载

# 下载最新版Nginx镜像 (其实此命令就等同于 : docker pull nginx:latest )
docker pull nginx	 
# 下载指定版本的Nginx镜像 (xxx指具体版本号)
docker pull nginx:xxx	 

创建挂载目录

mkdir -p /home/nginx/conf
mkdir -p /home/nginx/log
mkdir -p /home/nginx/html

创建Nginx配置文件

#启动
docker run --name nginx -p 9001:80 -d nginx

# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /data01/nginx/conf/nginx.conf
# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /data01/nginx/conf/conf.d
# 将容器中的html文件夹复制到宿主机
docker cp nginx:/usr/share/nginx/html /data01/nginx/

# 停止启动的nginx容器
# 直接执行docker rm nginx或者以容器id方式关闭容器
# 找到nginx对应的容器id
docker ps -a
# 关闭该容器
docker stop nginx
# 删除该容器
docker rm nginx
# 删除正在运行的nginx容器
docker rm -f nginx

启动nginx

#按挂载目录启动nginx
#启动docker
docker run \
-p 80:80 \
--name nginx \
-v /data01/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data01/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /data01/nginx/log:/var/log/nginx \
-v /data01/nginx/html:/usr/share/nginx/html \
--restart unless-stopped \
-d \
nginx:latest
参数解释
–name nginx启动容器的名字
-d后台运行
-p 80:80将容器的 80(后面那个) 端口映射到主机的 80(前面那个) 端口, 最好在80端口启动 nginx
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf挂载nginx.conf配置文件
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d挂载nginx配置文件
-v /home/nginx/log:/var/log/nginx挂载nginx日志文件
-v /home/nginx/html:/usr/share/nginx/html挂载nginx内容
nginx:latest本地运行的版本
\shell 命令换行

验证启动成功

docker ps -a | grep nginx                 # 得到容器启动信息
curl 127.0.0.1:8003                       # 得到index.html 内容
浏览器访问 http://192.168.117.128:8003/     # 得到index.html 内容

Nginx的主配置文件nginx.conf的内容如下:

  1. docker 启动nginx时,虽然修改配置文件位置时在挂载目录下的 nginx.conf 文件,但是配置的资源访问路径必须是nginx容器内部的资源路径, 否则无法获取到资源
  2. 更新配置文件,必须重启nginx容器配置才能生效
  3. 验证配置是否更新成功时, 可以进入nginx容器 检查/etc/nginx/conf目录下配置文件
# 全局块
user  nginx;                  # 执行进程的用户或者用户组
worker_processes  auto;       # Nginx的进程数, 一般设置和cpu核数一致

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

# events块
# 每个进程允许的最大并发数
events {
    worker_connections  1024;
}

# http块
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;
    #  include 如果有其他额外的配置文件, 可以使用这个参数添加配置文件

# server
server {
# server全局块
        listen       80;
        server_name  xujiayin.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

# location块
        location / {
            root   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;
        }

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

# 可以配置多个server块

}

总结:

一个nginx可以配置多个域名监听: 添加多个server配置即可
不同域名可以配置不同的后端服务: 设置proxy参数等配置

本地配置DEMO

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
    keepalive_timeout  65;

    server {
            listen       80;
            server_name  hengda.nginx.com;
            location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    }
    server {
            listen       80;
            server_name  xujiayin.nginx.com;
            location / {
                root   /usr/share/nginx/html/zhaowenlong;
                index  report.html report.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /usr/share/nginx/html/50x.html {
                root   html;
            }
    }
}

更改本地Windows的hosts文件

不配置hosts文件时, nginx.conf配置server_name无法直接访问, 必须更改客户机的hosts文件重新解析DNS

同一个局域网内的电脑访问服务器时, 需要更改客户机的hosts文件
文件位置 C:\Windows\System32\drivers\etc\hosts
如果无权限时, 先添加权限 或者去除只读勾选
更改hosts 文件:
    1. 管理员身份启动 powershell
    2. notepad C:\Windows\System32\drivers\etc\hosts
    3. 更新内容  "192.168.117.128   hengda.nginx.com xujiayin.nginx.com"
    4. ctrl + s 保存文件
    5. 按配置的域名访问 nginx 资源
    6. 本地访问: http://hengda.nginx.com/ 或者 http://xujiayin.nginx.com/ 即可访问ningx的index页面
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值