docker中nginx 安装教程,学费了吗

5.nginx 安装
  1. 拉取镜像

    docker pull nginx

  2. 创建nginx容器
    这个创建过程基本梳理为:创建nginx容器1 -> 本地新建数据卷文件夹 -> 拷贝配置文件到挂载用的本地文件夹 -> 删除nginx容器1 -> 挂载本地数据卷并新建nginx容器2

(1)创建临时nginx容器1
如果端口有冲突,需要进行调整

docker run --name myNginx -p 8090:80 -d nginx

(2)准备挂载文件夹
html文件夹下面将用于放置打包好的前端项目

mkdir -p /Users/dyrain/Downloads/Docker/nginx/logs

mkdir -p /Users/dyrain/Downloads/Docker/nginx/html

mkdir -p /Users/dyrain/Downloads/Docker/nginx/conf

mkdir -p /Users/dyrain/Downloads/Docker/nginx/conf.d

(3)拷贝配置文件

docker cp myNginx:/etc/nginx/nginx.conf /Users/dyrain/Downloads/Docker/nginx/conf/nginx.conf

docker cp myNginx:/etc/nginx/conf.d/default.conf /Users/dyrain/Downloads/Docker/nginx/conf.d/default.conf

docker cp myNginx:/usr/share/nginx/html /Users/dyrain/Downloads/Docker/nginx/

(4)停止并删除nginx容器

docker stop myNginx

docker rm myNginx

(5)创建nginx容器2

docker run --name myNginx -p 8090:80 -v
/Users/dyrain/Downloads/Docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v
/Users/dyrain/Downloads/Docker/nginx/conf.d:/etc/nginx/conf.d -v
/Users/dyrain/Downloads/Docker/nginx/logs:/var/log/nginx -v
/Users/dyrain/Downloads/Docker/nginx/html:/usr/share/nginx/html -d nginx

  1. 打包前端项目
  2. 在本地目录ruoyi-ui下先确认 vue.config.js 中关于地址的配置,这个地址需要与nginx配置文件中的地址一致
  3. 通过node容器在/app/ruoyi_node目录下执行以下命令即可进行打包
  4. npm run build:prod
  5. 打包后得到一个文件夹dist
  6. 将该文件夹拷贝到nginx容器挂载的目录下
  7. 编写nginx配置文件
    一共有两个配置文件 nginx.conf 和 default.conf

nginx.conf 配置如下

user  root;
worker_processes  1;

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

其中最后一行语句包含了对 default.conf 的引用,所以将项目相关的配置写到 default.conf 里就可以了。

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
#access_log  /var/log/nginx/host.access.log  main;
 
location / {
    root   /usr/share/nginx/html/dist;
	 try_files $uri $uri/ /index.html;
    index  index.html index.htm;
}


location /prod-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://host.docker.internal:8080/;
}
 
#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;
#}
}

/usr/share/nginx/html/dist为打包后的前端项目存放的路径

host.docker.internal:8080为后端服务地址和端口

启动nginx服务

可以使用以下指令重新加载配置文件,或者重启nginx容器

nginx -s reload

结果 本地访问 localhost:8090

后面讲解nginx 中配置的 ssl, https,
朋友们,先关注,在观看,实在不行公众号里转一转

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青柠编程

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值