Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 345
Victor Bocharsky Avatar asked Apr 27 '15 16:04

Victor Bocharsky


1 Answers

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;
  }
}
like image 84
Szymon Stepniak Avatar answered Oct 23 '22 02:10

Szymon Stepniak