docker 安装 Nginx
准备工作
# 打开目录
[root@localhost \]# cd /usr/local/docker
# 创建文件夹
[root@localhost docker]# mkdir nginx
# 进入文件夹
[root@localhost docker]# cd nginx/
# 创建 config
[root@localhost nginx]# mkdir conf
# 打开 config
[root@localhost nginx]# cd conf
# 编写配置文件
[root@localhost conf]# vim nginx.conf
配置文件内容
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
下载镜像
[root@localhost conf]# docker pull nginx:latest
编写脚本
# 打开目录
[root@localhost conf]# cd /usr/local/docker/nginx
# 编写脚本
[root@localhost nginx]# vim startNginx.sh
脚本内容
docker run -d \
--restart=always \
--name nginx \
-p 80:80 \
-v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html \
nginx
启动脚本
# 赋予权限
[root@localhost nginx]# chmod -R 777 startNginx.sh
# 启动脚本
[root@localhost nginx]# ./startNginx.sh
在conf.d目录新增:static-gmall.conf文件
# 打开目录
[root@localhost nginx]# cd /usr/local/docker/nginx/conf/conf.d
# 编写static-gmall.conf文件
[root@localhost conf.d]# vim static-gmall.conf
文件内容
server {
listen 80;
server_name static.gmall.com;
location /static/ {
root /usr/share/nginx/html;
}
如果想要转发网关
server {
listen 80;
server_name jiuzhou.com;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ @router;
index /index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
location /api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.17.0.1:9999/;
}
}
172.17.0.1这个地址是你容器的网关ip,相当于桥梁。(重要)
root /usr/share/nginx/html;这个是容器内部的地址(重要)
}
重启脚本
# 重启脚本
[root@localhost conf.d]# docker restart 容器ID
更改windows中host设置
# 打开 本机此电脑 输入
C:\Windows\System32\drivers\etc
# 进入指定文件夹
# 打开名为 host 文件
# 拉到最下面 添加
你虚拟机地址 域名地址 例:joakim.com
# 添加后进行保存
# 正常使用域名访问图片
http://joakim.com/static/1677120963562.png
基础命令
虚拟机防火墙命令
查询防火墙状态,绿色的时开启了防火墙,黑色的是表示防火墙关闭
systemctl status firewalld
关闭防火墙
systemctl stop firewalld
开启防火墙
systemctl start firewalld