无法在 Dockerized Nginx 和 Laravel 上设置静态内容

问题描述 投票:0回答:1

我使用 docker-compose 设置了我的 PHP Laravel 应用程序,我制作了一些容器,包括 nginx,配置了它,除了文件夹

public
中的内容之外,一切都正常。我尝试以不同的方式设置我的 nginx.conf 但没有任何帮助,所以你能帮我吗?

这是我的 nginx.conf

events {}

http {
    server {
        listen 80 default_server;
        root /var/www/public;
        index index.html index.php;
    
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
    
        charset utf-8;

        location / {
            try_files $uri /index.php?$query_string;
        }
    
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|html|json|svg)$ {
            try_files $uri =404;
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        error_page 404 /index.php;
    
        location ~ \.php$ {
            fastcgi_pass php:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
}

这是我的 docker-compose.yml

version: '3.8'

services:
  nginx:
    container_name: nginx
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - php
      
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    image: php:8.2-fpm
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.env:/var/www/.env
    depends_on:
      db:
        condition: service_healthy

  db:
    platform: linux/x86_64
    container_name: db
    image: mysql:8.0
    restart: unless-stopped
    environment:
      - MYSQL_DATABASE='${DB_DATABASE}'
      - MYSQL_USER='${DB_USERNAME}'
      - MYSQL_PASSWORD='${DB_PASSWORD}'
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    healthcheck:
        test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
        timeout: 120s
        retries: 60

  node:
    platform: linux/x86_64
    build:
      context: .
      dockerfile: Dockerfile.node
    container_name: node
    image: node:20
    ports:
    - "3000:3000"
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - /var/www/node_modules

volumes:
  dbdata:
    driver: local

这是我的 Dockerfile

# Set the base image for subsequent instructions
FROM php:8.2-fpm

USER root

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    curl \
    unzip \
    git \
    libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev && \
    docker-php-ext-install mysqli pdo_mysql mbstring exif pcntl bcmath gd zip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Remove default server definition
RUN rm -rf /var/www/html

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

# Expose port 9000 and start php-fpm server
EXPOSE 9000

ENTRYPOINT ["sh", "./entrypoint-php.sh"]

CMD ["php-fpm"]

我已经搜索了很多网站来寻找解决方案,并尝试了不同的 nginx.conf 选项,但没有任何帮助。另外:我有 .env 和变量

APP_URL=localhost
(因为我在本地托管它)。我尝试访问的文件有 404 结果。我将不胜感激任何帮助,谢谢

更新

我通过将此卷添加到 docker-compose 中的 nginx 服务解决了我的问题:

- ./:/var/www

还将我的 nginx.conf 更改为:

events {}

http {
    server {
        listen 80 default_server;

        server_name localhost;
        root /var/www/public;

        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        index index.html index.php;

        client_max_body_size 5M;

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

        location ~ ^/.+\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            include fastcgi_params;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_param REALPATHTEST $realpath_root;
            internal;
        }
    }
}
php laravel nginx docker-compose static-files
1个回答
0
投票

更新

我通过将此卷添加到 docker-compose 中的 nginx 服务解决了我的问题:

- ./:/var/www

还将我的 nginx.conf 更改为:

events {}

http {
    server {
        listen 80 default_server;

        server_name localhost;
        root /var/www/public;

        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        index index.html index.php;

        client_max_body_size 5M;

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

        location ~ ^/.+\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            include fastcgi_params;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_param REALPATHTEST $realpath_root;
            internal;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.