简体   繁体   中英

Serve via containerized Nginx files from php-fpm container?

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 make nginx 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"

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM