搭建域名访问环境并使用 Docker 安装 Nginx"一起养成写作习惯!这是我参与[掘金日新计划.4月更文挑战]的第十天"

642 阅读3分钟

一、Docker安装Nginx

随便启动一个Nginx实例,只是为了复制出配置

docker run -p 80:80 --name nginx -d nginx:1.10

将容器内的配置文件拷贝到当前目录:

# 注意别忘了后边的点
docker container cp nginx:/etc/nginx .

修改文件名称:

mv nginx conf

把这个conf移动到 /mydata/nginx 下 终止原容器:

docker stop nginx

执行命令删除原容器:

docker rm $ContainerId

创建新的nginx,执行以下命令:

docker run -p 80:80 --name mynginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-d nginx:1.10

我们的Nginx服务就算安装好了,并且将配置、和日志记录都挂载到宿主机上了

file

将nginx加入到开机自启动:

docker update a8f2 --restart=always  # Nginx

二、设置域名

Windows系统 hosts文件位置:windows”→“System32”→“drivers”→“etc” Mac 系统 hosts 文件位置:/etc/hosts/ 在hosts文件添加域名和IP映射关系。

hosts文件配置:

192.168.10.10 gulimall.com

映射关系表:

域名虚拟机IP备注
gulimall.com虚拟机IP默认80端口
search.gulimall.com虚拟机IP
item.gulimall.com虚拟机IP
member.gulimall.com虚拟机IP

配置好域名后,然后访问 gulimall.com,可以看到已经能访问Nginx服务了: file

另外,该IP 192.168.10.10 为我们虚拟机内部的IP,我们可以通过该IP直接在主机浏览器访问或者通过配置的域名访问ES服务 gulimall.com:5601/ [root@localhost conf.d]# cat default.conf [root@localhost conf.d]# cat default.conf server { listen 80; server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/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   /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;
#}

}

1、复制一份默认的配置文件

[root@localhost conf.d]# cp default.conf gulimall.conf

nginx目录结构

[root@localhost nginx]# tree
.
|-- conf
|   |-- conf.d
|   |   |-- default.conf
|   |   `-- gulimall.conf
|   |-- fastcgi_params
|   |-- koi-utf
|   |-- koi-win
|   |-- mime.types
|   |-- modules -> /usr/lib/nginx/modules
|   |-- nginx.conf
|   |-- scgi_params
|   |-- uwsgi_params
|   `-- win-utf
|-- html
`-- logs
    |-- access.log
    `-- error.log4 directories, 13 files

查看本机虚拟网卡地址:

➜  ~ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=3<RXCSUM,TXCSUM>
    inet6 ::1 prefixlen 128
    inet 127.0.0.1 netmask 0xff000000
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
    nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether c8:2a:14:06:84:f2
    nd6 options=1<PERFORMNUD>
    media: autoselect (none)
    status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether e0:f8:47:45:26:f8
    inet6 fe80::e2f8:47ff:fe45:26f8%en1 prefixlen 64 scopeid 0x5
    inet 10.0.173.113 netmask 0xfffffc00 broadcast 10.0.175.255
    nd6 options=1<PERFORMNUD>
    media: autoselect
    status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=63<RXCSUM,TXCSUM,TSO4,TSO6>
    ether ca:2a:14:60:ba:00
    Configuration:
        id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
        maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
        root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
        ipfilter disabled flags 0x2
    member: en2 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 6 priority 0 path cost 0
    nd6 options=1<PERFORMNUD>
    media: <unknown type>
    status: inactive
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00
    inet 192.168.10.1 netmask 0xffffff00 broadcast 192.168.10.255

怎么样将Nginx服务和我们gulimall-product 关联起来呢?这里就需要使用代理了。

我们主机的虚拟机的网卡地址为:vboxnet0: inet 192.168.10.1,也可以通过该地址加端口号访问gulimall-product (localhost:10000) 服务。

http://localhost:10000/ http://192.168.10.1:10000/

对比图: file file

在宿主机域名配置中,gulimall.com 配置的IP为:192.168.10.10,即虚拟机里边的IP,所以,配置的该域名+端口号可以访问我们虚拟机里边开启的服务,如数据库,redis, elasticsearch 服务: gulimall.com:5601/ 可以访问ES服务。

说明:宿主机要和虚拟机之间通信,必须在同一局域网内,宿主机IP:192.168.10.1,虚拟机IP:192.168.10.10,他们两个是在同一局域网内,则两者可以通信。

修改 gulimall.conf 配置文件,使gulimall.com 的访问可以通过代理 proxy_pass 到 192.168.10.1:10000 服务。

[root@localhost conf.d]# vim gulimall.conf
server {
    listen       80;
    server_name  gulimall.com;
​
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
​
    location / {
        proxy_pass http://192.168.10.1:10000
    }
​
    #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;
    #}
}

然后重启

[root@localhost conf.d]# docker restart nginx;
nginx
[root@localhost conf.d]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                               NAMES
a8f2fcaa1ec7        nginx:1.10          "nginx -g 'daemon of…"   2 hours ago         Restarting (1) 3 seconds ago                                       nginx
fbd8372062fc        kibana:7.4.2        "/usr/local/bin/dumb…"   6 days ago          Up 2 hours                     0.0.0.0:5601->5601/tcp              kibana
26371b5a3a07        redis               "docker-entrypoint.s…"   3 weeks ago         Up 2 hours                     0.0.0.0:6379->6379/tcp              redis
7dc90206f09a        mysql:5.7           "docker-entrypoint.s…"   3 weeks ago         Up 2 hours                     0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
[root@localhost conf.d]# docker logs nginx --tail 20
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10

我们可以看到,修改配置文件出错了,第十行有问题。

file

vi 进入到配置文件,可以通过 :set number 命令显示文本行号,方便我们查找问题,上边报错原来是 proxy_pass 后边忘了加分号了,加上分号就好了。

    8     location / {
   9         proxy_pass http://192.168.10.1:10000;
  10     }

然后再重启Nginx,通过配置的域名访问gulimall商城首页项目: file

可以看到通过代理设置,已经可以正常访问了。

原理:让nginx帮我们进行反向代理,所有来自原gulimall.com的请求