Symfony 3与Docker和nginx

我正在尝试使用DockerconfigurationSymfony3。

我的项目文件夹中有两个文件和一个目录:

文件: – docker-compose.yml – site.conf

目录: – 代码

泊坞窗,compose.yml:

web: image: nginx:latest ports: - "80:80" volumes: - ./site.conf:/etc/nginx/conf.d/site.conf links: - php php: image: php:7-fpm volumes: - ./test_api:/test_api/web 

site.conf:

我从官方nginx网站这里得到这个文件,我的server_name是在hosts文件中声明的。

 server { server_name test-api.local; root /var/www/project/web; location / { # try to serve file directly, fallback to app.php try_files $uri /app.php$is_args$args; } # DEV # This rule should only be placed on your development environment # In production, don't include this and don't deploy app_dev.php or config.php location ~ ^/(app_dev|config)\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; # When you are using symlinks to link the document root to the # current version of your application, you should pass the real # application path instead of the path to the symlink to PHP # FPM. # Otherwise, PHP's OPcache may not properly detect changes to # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 # for more information). fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; } # PROD location ~ ^/app\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; # When you are using symlinks to link the document root to the # current version of your application, you should pass the real # application path instead of the path to the symlink to PHP # FPM. # Otherwise, PHP's OPcache may not properly detect changes to # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 # for more information). fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; # Prevents URIs that include the front controller. This will 404: # http://domain.tld/app.php/some-path # Remove the internal directive to allow URIs like this internal; } # return 404 for all other php files not matching the front controller # this prevents access to other php files you don't want to be accessible. location ~ \.php$ { return 404; } error_log /var/log/nginx/project_error.log; access_log /var/log/nginx/project_access.log; } 

代码目录:

它包含了新鲜的Symfony 3.1安装。

没有,我先做的是docker – 组成它然后安装所有的图像为我,似乎运行。

然后我去浏览器,并在我的url栏中input:

 http://test-api.local/ 

和这个: http://test-api.local/app.php和这个: http://test-api.local/app_dev.php

我得到这个错误:

 502 Bad Gateway nginx/1.11.1 

我的命令行中没有任何错误发生。 任何人都可以看到我的设置有什么问题…?

以下是您需要应用以使Symfony项目与Docker Compose一起工作的更改。

docker-compose.yml

 web: image: nginx:latest ports: - "80:80" volumes: - ./site.conf:/etc/nginx/conf.d/site.conf # Add the volumes from the PHP container, as Nginx needs access to your project files volumes_from: - php links: - php php: image: php:7-fpm volumes: # I changed to the path that is specified in your site.conf file - ./test_api:/var/www/project 

site.conf ,更改这些行:

 fastcgi_pass unix:/var/run/php5-fpm.sock; 

 fastcgi_pass php:9000; 
  • php指的是你的PHP容器的名字(你在docker-compose.yml文件中定义docker-compose.yml )。
  • 9000是PHP容器公开的用于PHP-FPM的端口。

我测试了它,一切按预期工作。

嗯。 看起来像你的nginx正试图服务文件/连接到PHP容器中的套接字。 但是容器不能看到对方。

您需要在php容器中嵌入nginx,或者通过卷在容器之间共享文件和/var/run/php5-fpm.sock套接字。