How to use custom Nginx config for official nginx Docker image?

痴心易碎 提交于 2019-12-10 17:11:44

问题


I have next docker-compose file:

nginx:
    build: .
    ports:
        - "80:80"
        - "443:443"
    links:
        - fpm
fpm:
    image: php:fpm
    ports:
        - "9000:9000"

The Dockerfile command list is:

FROM nginx

ADD ./index.php /usr/share/nginx/html/

# Change Nginx config here...

The Nginx server work fine and I can see default html page on http://localhost/index.html, but don't execute PHP scripts. So when I get http://localhost/index.php - browser download PHP file instead of execute them.

How can I use custom Nginx config to execute PHP script in my case?


回答1:


You can create a very simple docker image containing your custom nginx configuration and mount this volume in the container that uses original nginx image.

There are just a few steps to follow.

1. Create your custom nginx config image project

mkdir -p nginxcustom/conf
cd nginxcustom
touch Dockerfile
touch conf/custom.conf

2. Modify Dockerfile

This is the file content:

FROM progrium/busybox
ADD conf/ /etc/nginx/sites-enabled/
VOLUME /etc/nginx/sites-enabled/

3. Build the new image

docker build -t nginxcustomconf .

4. Modify your docker-compose.yml file

nginxcustomconf:
  image: nginxcustomconf
  command: true

nginxcustom:
  image: nginx
  hostname: nginxcustom
  ports:
    - "80:80"
    - "443:443"
  volumes_from:
    - nginxcustomconf

The sample conf/custom.conf may look like this:

server {
  listen 82;
  server_name ${HOSTNAME};

  set $cadvisor cadvisor.docker;

  location / {
    proxy_pass              http://$cadvisor:8080;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout   150;
    proxy_send_timeout      100;
    proxy_read_timeout      100;
    proxy_buffers           16 64k;
    proxy_busy_buffers_size 64k;
    client_max_body_size    256k;
    client_body_buffer_size 128k;
  }
}


来源:https://stackoverflow.com/questions/29900692/how-to-use-custom-nginx-config-for-official-nginx-docker-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!