【问题标题】:Make PHP and Nginx Docker images work together使 PHP 和 Nginx Docker 镜像协同工作
【发布时间】:2017-05-26 05:54:05
【问题描述】:

我尝试了许多建议,但无法使其发挥作用。 我想和NGINX-PHP合作创建一个docker-compose.yml文件。

这是我做的:

version: "2"

services:
  nginx:
    image: nginx:latest
    restart: always
    ports: 
      - "80:80"
      - "443:443"
    links:
      - php
    depends_on:
      - php
    expose:
      - "80"
      - "443"
    volumes:
      - ./www:/var/www/html
      - ./config/nginx/site.conf:/etc/nginx/sites-available/default
      - ./config/nginx/site.conf:/etc/nginx/sites-enabled/default

  php:
    image: php:7-fpm
    restart: always
    volumes:
      - ./www:/var/www/html

Docker 图像运行没有错误,但是当我想访问 Nginx 时,我得到了这个:

欢迎使用 nginx!

如果看到这个页面,说明nginx web服务器安装成功 和工作。需要进一步配置。

有关在线文档和支持,请参阅 nginx.org。 nginx.com 上提供商业支持。

感谢您使用 nginx。

我尝试登录两个映像并检查卷。我还检查了是否可以从nginx ping php。一切似乎都很好......

这是我的Nginx 站点配置:

server {
    server_name ncp-docker;

    listen 80;
    index index.php index.html index.htm;
    root /var/www/html;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /(inc|uploads/avatars) {
        deny all;
    }
}

【问题讨论】:

标签: php nginx docker docker-compose


【解决方案1】:

您默认使用默认服务器配置,因此您需要覆盖默认的 nginx 虚拟主机:

    - ./config/nginx/site.conf:/etc/nginx/conf.d/default.conf

使用它并删除启用站点和站点可用的卷

【讨论】:

    【解决方案2】:

    我认为您可能处理不正确。

    nginx 配置不应该直接运行 PHP。 PHP 在一个单独的容器上运行。

    相反,您应该有 PHP 应用程序的 nginx 反向代理。

    类似这样的:

    
    server {
        listen 80;
        server_name yourdomain.example.com;
        location / {
            proxy_pass http://php:9000/;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
        }
     }
    

    您可能需要添加/更改一些细节......但这是我在需要时在 nginx 中剪切和粘贴的基本反向代理

    【讨论】:

    • 端口 9000 不是 HTTP 端口,它是 CGI 通信,所以你不能 proxy_pass 给它。因此,他实现了 fastcgi_pass。
    • 啊 - 我永远记不住的 php 细节 :) 谢谢@Robert!
    【解决方案3】:

    正如 Robert 建议的那样,您必须覆盖默认的 nginx 虚拟主机,但是,我想补充一点,您还可以为您的 nginx 容器添加额外的 Dockerfile 并避免使用卷。在您的docker-compose.yml 中,您必须将image: nginx:latest 更改为build: ./nginx/。对于 Dockerfile,这样的东西应该可以解决问题:

    FROM nginx:latest
    RUN rm /etc/nginx/conf.d/default.conf
    ADD conf.d/ /etc/nginx/conf.d/  #Replace the default nginx virtual host
    

    我发现这更容易,因为稍后您可以轻松地将更多内容添加到容器中,例如用于 HTTPS 连接的 SSL 证书:

    ADD ssl/ /etc/nginx/ssl/    
    

    这将节省您使用卷的时间,并使您在未来的 nginx 设置方面更加轻松。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多