让nginx在docker中很好地使用php fpm

让nginx在docker中很好地使用php fpm,php,docker,nginx,docker-compose,Php,Docker,Nginx,Docker Compose,我试图用nginx和php-fpm设置docker,但似乎nginx-conf出现了一些问题,我不知道具体是什么 这是docker-compose.yml nginx.conf server { listen 80 default_server; server_name localhost; index index.php index.html; root /var/www/html/code; location ~ \.php$ { try_files $uri

我试图用nginx和php-fpm设置docker,但似乎nginx-conf出现了一些问题,我不知道具体是什么

这是docker-compose.yml

nginx.conf

server {
  listen 80 default_server;
  server_name localhost;

  index index.php index.html;
  root /var/www/html/code;

  location ~ \.php$ {
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;

    include fastcgi_params;
  }
}

./code/index.php

我不明白我在这里做错了什么,但当我访问http://localhost:8000 我得到的是nginx欢迎页面,而不是code/index.php中的页面

有人能帮忙吗?谢谢

注:我正在mac上运行最新的docker桌面。

问题在于:

    volumes:
      - .:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/web.conf:ro
NGINX附带conf.d/default.conf。由于您没有删除它,您至少应该覆盖它:

    volumes:
      - .:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
由于您没有这样做,NGINX仍然加载default.conf,这就是为什么您会看到默认主页

另外一个问题:当您以这种方式进行映射时,您的nginx.conf将出现在/var/www/html中。即使您没有在conf文件中提供这个位置,我仍然建议您避免这样做。您可以将卷映射为./code:/var/www/html。更好的是,我会从nginx服务中完全删除这个映射,因为它是不需要的

docker-compose up -d
    volumes:
      - .:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/web.conf:ro
    volumes:
      - .:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro