「解决」docker 运行laravel,nginx这样配置为何不可?

docker的nginx应该如何配置才可以运行项目。
本机电脑系统:mac
nginx配置如下如何修改

想要实现的效果:

docker 运行laravel,nginx要如何配置?

将localhost:8083/yxfxs/public 访问laravel 改为 yxfxs.test即可访问

nginx配置:

server {
    listen       80;
    server_name  localhost;

    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }

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

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

}

hosts配置。如下:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       content.test

127.0.0.1       AcronisDriveSearchPlugin
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

laravel 项目目录:

docker 运行laravel,nginx要如何配置?

docker 镜像:

docker 运行laravel,nginx要如何配置?

算命的码农。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

hosts配置

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1    localhost
255.255.255.255    broadcasthost
::1             localhost
127.0.0.1     yxfxs.test
127.0.0.1    AcronisDriveSearchPlugin
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

这是 docker nginx 的配置

server {
    listen       80;
    server_name  yxfxs.test;
    index  index.html index.htm index.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        # 这儿的 /www/ 要注意替换成 php:8-fpm 这个镜像里面挂载的 laravel项目的public 目录
        fastcgi_param  SCRIPT_FILENAME /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

即可访问 file

关于端口号

  • 如果你本机端口没被占用, 在启用docker nginx 的时候可以直接用参数 -p "80:80"
  • 如果本地端口被占用了, 需要修改这个本机80端口的配置, 代理转发到8083上

考虑后面还要运行多个项目 就没有进行这个功能测试 有时间测试完这个东西再说吧。也欢迎大佬看到之后指正里面的问题或者直接回答关于去掉端口号的方法。

感谢风兮清扬提供的帮助

3年前 评论
讨论数量: 5

你这个配置完全不对呀。 看样子你还没弄清楚这里面的 宿主机和容器路径的区别。

强烈建议参考下我这个 docker 环境,很容易看懂,在次基础上可以定制自己的开发环境 github.com/839891627/dnmp

不管用不用,看一下希望对你有用!

3年前 评论
merle_Eul的神圣法杖 (楼主) 3年前

我不确定你的环境,以及你采用的docker网络通讯方式,假定你的nginx和php都是以docker的方式运行,并通过网络组通讯。
你可以参考如下方式建立两个容器之间的网络通讯:

// 创建网络组
docker network create nginx-network

// nginx容器
docker run --name nginx --net nginx-network -p 80:80 -p 443:443 -v /www:/www nginx

// php容器
docker run --name php --net nginx-network -v /www:/www php

这时候你进入容器,应该是能够通过容器名字互相 ping 通的。
所以你的nginx也就能够通过 fastcgi_pass   php:9000; 来与php进行通讯了。
网络想通了,剩下的还有其他问题的话,也就是路径解析,或者配置错误其他问题了。
3年前 评论

关注两点

  • 挂载到容器路径 Linux 一样的意思
    # 宿主c:\data 挂载到容器 \data
    c:/data:/data
  • nginx站点根
      index index.php index.html;
      # 站点目录是 C:\data\basic
      root /data/basic/public;
      location ~ [^/]\.php(/|$)
3年前 评论

我一般都是docker一个centos然后再装一个宝塔,一了百了

3年前 评论

hosts配置

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1    localhost
255.255.255.255    broadcasthost
::1             localhost
127.0.0.1     yxfxs.test
127.0.0.1    AcronisDriveSearchPlugin
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

这是 docker nginx 的配置

server {
    listen       80;
    server_name  yxfxs.test;
    index  index.html index.htm index.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        # 这儿的 /www/ 要注意替换成 php:8-fpm 这个镜像里面挂载的 laravel项目的public 目录
        fastcgi_param  SCRIPT_FILENAME /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

即可访问 file

关于端口号

  • 如果你本机端口没被占用, 在启用docker nginx 的时候可以直接用参数 -p "80:80"
  • 如果本地端口被占用了, 需要修改这个本机80端口的配置, 代理转发到8083上

考虑后面还要运行多个项目 就没有进行这个功能测试 有时间测试完这个东西再说吧。也欢迎大佬看到之后指正里面的问题或者直接回答关于去掉端口号的方法。

感谢风兮清扬提供的帮助

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!