1

My app files are located in phpfpm container and I need to serve them through nginx. I want to avoid mounting the same files in two containers, so I'm trying to figure out a way to serve them only from one, phpfpm, container. When I use reverse proxy to other containers:

server {
    listen 0.0.0.0:8080;
    server_name myapp.test;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://phpfpm:900;
        proxy_redirect off;
    }
}

I get 502 Bad Gateway error with the following error log record:

1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.18.0.1, server: myapp.test, request: "GET / HTTP/1.1", upstream: "http://172.18.0.2:9000/", host: "myapp.test"

I guess it's because phpfpm container is not a HTTP server.

So, alternatively, I try using fastcgi_pass like so:

server {
    listen 0.0.0.0:8080;
    server_name myapp.test;

    root /app;

    location / {
        try_files $uri $uri/index.php;
    }

    location ~ \.php$ {
        fastcgi_pass phpfpm:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

This serves *.php files as expected, but doesn't serve other files, namely static content.

How do I makenginx serve both .php and static files from my phpfpm container?

Here's my docker-compose.yml:

version: "3.7"
services:
  phpfpm:
    image: "php-fpm:7.3"
    volumes:
      - ./site:/app
    ports:
      - "9000:9000"

  nginx:
    image: "nginx:1.17"
    volumes:
      - ./nginx/app.conf:/opt/nginx/conf/nginx.conf
    ports:
      - "80:8080"

1 Answer 1

1

You have 2 issues:

  • You did not mount your static content into your Nginx container, therefore it cannot be served. Add this volume to your container
./site/public/:/var/www/html/public/:ro
  • You need to setup your Nginx config in order to serve this static content. You may try this one
server {
    listen 0.0.0.0:8080;
    server_name myapp.test;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi.conf;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to mention in my question that I started tinkering with this exactly because I wanted to avoid mounting the same app files twice, I wanted to serve everything from only one, php-fpm, in this case, container. However, the configuration provided in your answer is very useful for me, so I'm upvoting it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.