当使用docker-compose和nginx和php-fpm时,上游php-fpm有时会提供404文件找不到

时间:2019-07-10 13:28:32

标签: php docker nginx docker-compose

在我的本地开发环境中,我将docker-compose与nginx和php-fpm容器一起使用,但是有时php-fpm出现404文件未找到错误。只有在发生多个ajax调用时,我才能始终如一地得到它。

这是我的docker-compose文件。

version: '3'
services:
  nginx:
    build:
      context: docker
      dockerfile: nginx.dockerfile
    volumes:
      - ./:/var/www/html
    ports:
      - 80:80
    depends_on:
      - php
  php:
    build:
      context: docker
      dockerfile: php.dockerfile
    volumes:
      - ./:/var/www/html

nginx.conf

user  nginx;
worker_processes  4;
daemon off;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;
    # Switch logging to console out to view via Docker
    #access_log /dev/stdout;
    #error_log /dev/stderr;

    sendfile        on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*.conf;
}

conf.d / default.conf

upstream php-upstream {
    server php:9000;
}

sites-available / site.conf

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;
    root /var/www/html/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

nginx.dockerfile

FROM nginx

COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY nginx/site.conf /etc/nginx/sites-available/site.conf

WORKDIR /var/www/

CMD [ "nginx" ]

EXPOSE 80 443

当我访问一个有两个ajax调用的页面时,一次会给我200

"GET /index.php" 200

接下来会给我404

ERROR: Unable to open primary script: /var/www/html/public/index.php (No such file or directory)

通常,当我刷新失败的代码后,现在可以正常工作,而正常工作的代码现在可以在404中正常工作。

0 个答案:

没有答案