通过docker-compose在php-fpm前面使用nginx,为什么php-fpm容器只有在命名为“app”时才能工作?

knpiaxh1  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(205)

我试着创建一个像这样的Docker服务:

  1. version: '3'
  2. services:
  3. app:
  4. image: php:8.2-fpm
  5. working_dir: "/var/www"
  6. volumes:
  7. - ./src:/var/www/html
  8. networks:
  9. - internal
  10. nginx:
  11. restart: always
  12. image: nginx:latest
  13. ports:
  14. - "8080:80"
  15. volumes:
  16. - ./nginx:/etc/nginx/conf.d
  17. - ./src:/var/www/html
  18. depends_on:
  19. - app
  20. networks:
  21. - internal
  22. networks:
  23. internal:
  24. name: internal

个字符
但是当我将app容器更改为apiapp时,出现了一个问题:

  1. services:
  2. apiapp:
  3. image: php:8.2-fpm
  4. working_dir: "/var/www"
  5. volumes:
  6. - ./src:/var/www/html
  7. networks:
  8. - internal
  9. nginx:
  10. restart: always
  11. image: nginx:latest
  12. ports:
  13. - "8080:80"
  14. volumes:
  15. - ./nginx:/etc/nginx/conf.d
  16. - ./src:/var/www/html
  17. depends_on:
  18. - apiapp
  19. networks:
  20. - internal
  21. networks:
  22. internal:
  23. name: internal
  1. $ docker-compose up -d
  2. [+] Running 3/3
  3. Network internal Created 0.0s
  4. Container my-php-apiapp-1 Started 0.5s
  5. Container my-php-nginx-1 Started 0.9s
  6. $ curl "http://localhost:8080"
  7. curl: (52) Empty reply from server

的字符串
我先删除我的服务,然后输入docker-compose up -d,所以我假设my-php-app-1my-php-apiapp-1无关,但我真的不知道为什么容器名称很重要。
我试图在Docker上找到““app”container”,但是Compose FAQsKey features and use cases of Docker Compose对我的问题没有帮助。
Nginx设置文件基本上来自qiita上的一篇文章:

  1. # https://qiita.com/shir01earn/items/f236c8280bb745dd6fb4
  2. server {
  3. listen 80;
  4. root /var/www/html;
  5. index index.php;
  6. location ~ [^/]\.php(/|$) {
  7. fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  8. if (!-f $document_root$fastcgi_script_name) {
  9. return 404;
  10. }
  11. fastcgi_param HTTP_PROXY "";
  12. fastcgi_pass app:9000;
  13. fastcgi_index index.php;
  14. include fastcgi_params;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. }
  17. location / {
  18. root /var/www/html;
  19. index index.php;
  20. try_files $uri $uri/ $uri.php /index.php?$query_string =404;
  21. }
  22. }

omhiaaxx

omhiaaxx1#

当你的Nginx配置说

  1. fastcgi_pass app:9000;

字符串
它使用FastCGI协议将请求转发到主机名app解析为的端口9000。
在编写上下文中,服务名称

  1. services:
  2. app: { ... }
  3. nginx: { ... }


解析为主机名,假设服务具有兼容的networks:。(另请参阅Docker文档中的Networking in Compose。)
如果你想将app容器重命名为其他名称,你需要将fastcgi_pass行也更改为匹配。如果Nginx配置看到一个不存在的主机名,你可能会得到一个启动时错误,这就是为什么你在示例中得到一个“连接拒绝”错误。

相关问题