Docker中运行Laravel:如何配置Nginx实现本地访问?

docker中运行laravelnginx配置指南

为了在docker中运行laravel项目,需要正确配置nginx容器。以下是配置步骤:

  1. 修改nginx配置

    在nginx容器的配置文件中,需要将fastcgi_pass修改为php容器的名称和端口,并更新script_filename的参数以指定laravel项目的public目录。修改后的配置如下:

    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   phpimagename:9000;
          fastcgi_index  index.php;
          fastcgi_param  script_filename  /var/www/laravel/public$fastcgi_script_name;
          include        fastcgi_params;
        }
    }
    登录后复制
  2. 挂载本地目录到镜像

    启动php容器时,使用-v参数将本地laravel项目目录挂载到镜像的/var/www/laravel路径。这样,php容器可以访问laravel项目的代码和资源。

    docker run --name phpimagename -v "本地的laravel绝对路径:/var/www/laravel"  php:8-fpm
    登录后复制
  3. 启动nginx容器

    使用-p参数将nginx容器的80端口映射到主机上的8083端口,并启动容器。

    docker run -p "8083:80" nginx
    登录后复制
  4. 更新hosts文件

    在主机上的hosts文件中,将访问域yxfxs.test解析到127.0.0.1。

    127.0.0.1 localhost
    127.0.0.1 yxfxs.test
    登录后复制
  5. 访问项目

    访问localhost:8083/yxfxs/public即可访问laravel项目。

以上就是Docker中运行Laravel:如何配置Nginx实现本地访问?的详细内容,更多请关注php中文网其它相关文章!

https://www.php.cn/faq/1227165.html

发表回复

Your email address will not be published. Required fields are marked *