Docker上的Nginx默认页面

时间:2017-03-24 20:08:49

标签: php nginx docker docker-compose

// docker-compose.yml

    web:
        image: nginx:latest
        ports:
            - "8080:80"
        volumes:
            - ./code:/code
            # - ./site/site.conf:/etc/nginx/conf.d/site.conf
            - ./site/site.conf:/etc/nginx/sites-available/site.conf
            - ./site/site.conf:/etc/nginx/sites-enabled/site.conf
        links:
            - php
    php:
        build: ./php
        volumes:
            - ./code:/code

// site/site.conf

server {
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

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

我在docker中运行nginx的上述配置。由于某种原因,它继续显示默认的nginx页面。

在容器内部有一个文件/etc/nginx/nginx.conf,其中包含*.conf目录中的所有conf.d,但仍然无法让我的conf工作。

然后我尝试将我的文件复制到网站 - 可用和启用网站的文件夹仍然没有运气。

请帮助 感谢

1 个答案:

答案 0 :(得分:2)

我们需要做三件事才能让nginx启动并运行。

  1. 在构建过程中删除 /etc/nginx/conf.d/default.conf
  2. 制作上游指令以正确引用php-fpm服务。
  3. 更改nginx用户的 UID 以匹配您的。
  4. 最有效的解决方案是创建一个 Dockerfile ,解决所有这三个nginx问题并将其放入 docker-compose.yml 的nginx服务定义中。此配置有助于更正nginx用户并包含sites-enabled/.conf

    # /etc/nginx/nginx.conf: Changes user to www-data and includes /etc/nginx/sites-enabled/*.conf
    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;
    
    events {
      worker_connections  2048;
      multi_accept on;
      use epoll;
    }
    
    http {
      server_tokens off;
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      keepalive_timeout 15;
      types_hash_max_size 2048;
      include /etc/nginx/mime.types;
      default_type application/octet-stream;
      access_log off;
      error_log off;
      gzip on;
      gzip_disable "msie6";
      include /etc/nginx/conf.d/*.conf;
      include /etc/nginx/sites-available/*;
      open_file_cache max=100;
    }
    
    daemon off;
    

    以前的 nginx.conf 和此 Dockerfile 都应存储在同一目录中。

    # <your-nginx-build-context>/Dockerfile
    FROM nginx:latest
    
    # The default nginx.conf DOES NOT include /etc/nginx/sites-enabled/*.conf
    COPY nginx.conf /etc/nginx/
    COPY symfony.conf /etc/nginx/sites-available/
    
    # Solves 1
    RUN mkdir -p /etc/nginx/sites-enabled/ \
        && ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/symfony.conf \
        && rm /etc/nginx/conf.d/default.conf
    
    # Solves 2
    RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
    
    # Solves 3
    RUN usermod -u 1000 www-data
    
    CMD ["nginx"]
    
    EXPOSE 80
    EXPOSE 443
    

    确保使用以下内容更新 docker-compose.yml 中的nginx服务定义:

    web:
        build: ./nginx/Dockerfile