若依分离版本地 docker 部署,安装 nginx 容器 后将 ruoyi-ui(前端)打包放到 html 下,修改默认的 /conf.d/default.conf:

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;

        location / {
            root   /usr/share/nginx/html/ruoyi-ui;
            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://localhost:8080/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

try_files $uri $uri/ /index.html; 做了 vue 的静态访问映射,去除 .html 后缀。
/prod-api/ 对应正式站打包后台接口路径,/stage-api/ 对应测试站打包访问后台接口路径。这两者因为 vue 项目打包 代理器失效,所以需要通过 web 服务器反向代理来实现访问。

然后通过 http://localhost/prod-api/ 按照理论上来说,应该访问是 8080 端口服务。本地 nohup java -jar targets/ruoyi-admin.jar & 启动之后,8080 端口服务是存在的,但 /prod-api/ 并没有代理到后台的 8080 端口服务。

尝试通过百度解决,但搜索到的都是关于 nginx 如果配置反向代理的,之后缩小搜索范围,改为 docker、nginx、反向代理无效,找到了 在docker下安装nginx实现反向代理宿主机请求

突然意识到 nginx 服务与我在本地环境启动的 8080 端口服务不在一台设备,或者更准确一点不在一个 ip 上,nginx 内的 8080 端口没有服务,所以提示报错,这恰好说明了 nginx 的反向代理是起作用的。

所以只需要修改代理服务的地址为本地环境,即宿主机的局域网 ip,就可以正常访问了。

可以通过设置->网络查看到当前的 ip 地址,也可以通过 ifconfig,eth0 inet 查看到。替换掉 localhost,再次重启 nginx,登录页正常显示了。


2023-03-17

在通过 docker 安装了 php-fpm(php80)和 nginx 之后,配置 nginx 的 default.conf 文件增加一条 server 来解析 php 项目。

server {
    listen       80;
    server_name  localhost;
    charset utf-8;

    location / {
        root   /usr/share/nginx/html/carcar/public;
#        try_files $uri $uri/ /index.html;
        try_files $uri $uri/ /index.php?$query_string;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   php80:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/carcar/public/$fastcgi_script_name;
        include        fastcgi_params;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

在尝试将 fastcgi_pass 设置为 http://docker.for.mac.host.internal:9000 时,启动报错:invalid host in upstream "http://docker.for.mac.host.internal:9001"。更换成本地地址 10.7.2.48 也是一样的错。

为什么之前使用 proxy_pass 还能识别,到了 fastcgi_pass 就不认识了?没办法,经过一端时间的查询,没有结果,但有了一个新的方法:docker --link,这个叫做容器互联

使用时需要链接的容器处于运行状态,这里就是 php-fpm 的容器名 php80,可以通过 : 后面增加一个别名。解析时,会把 php80 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把容器名(或别名,如果有设置的话)php80 映射成 127.0.0.1,让 nginx 通过 php80:9000 访问 php-fpm。

docker run \
-p 80:80 \
--name nginx \
--restart=always \
--link php80 \
-v /Users/mac/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/mac/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /Users/mac/docker/nginx/log:/var/log/nginx \
-v /Users/mac/docker/nginx/html:/usr/share/nginx/html \
-d nginx:latest

nginx 启动成功!在本地 /Users/mac/docker/nginx/html 地址下放一个 phpinfo.php 文件,查看一下当前的 php-fpm 信息。

文章目录