php Symfony与nginx不提供.js和.css文件

n3h0vuf2  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(123)

嘿,我正在尝试使用symfony,docker和nginx部署我的第一个应用程序。现在我只尝试在本地运行我的docker容器,但是当我打开index.php时,它说我的*.css*.js文件找不到,我得到404错误。此外,index.php正在工作,因为当我将默认的symfony index.php更改为echo "something"时,它可以工作。我使用Webpack Encore构建我的JS和CSS文件
docker-compose.prod.yml

services:

  # nginx
  web: 
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf

  app:
    build: 
      dockerfile: ./php/Dockerfile
    volumes:
      - /var/www/html/vendor
      - .:/var/www/html

Dockerfile

FROM php:8.1-fpm-alpine


RUN apk update
RUN apk add vim

# install php with pgsql
RUN set -ex \
  && apk --no-cache add \
    postgresql-dev

RUN docker-php-ext-install pdo pdo_pgsql pgsql

ENV COMPOSER_ALLOW_SUPERUSER=1

# get composer using multi-stage build
COPY --from=composer:2.4 /usr/bin/composer /usr/bin/composer

# copy composer.lock and composer.json
COPY ./composer.* ./

# install bundles from composer.json with options for deployment
RUN composer install --prefer-dist --no-dev --no-scripts --no-progress --no-interaction

# copy application files to default directory
COPY . .

# run composer dump-autload 
RUN composer dump-autoload --optimize

nginx的default.conf

server {
    listen 80;
    server_name localhost;
    root /var/www/html/public;
    index index.php;

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {

        try_files $uri $uri/ /index.php?$query_string;

    }
}

文件结构

app
│   composer.json
│   composer.lock
│   docker-compose.prod.yml
│   package-lock.json
│   package.json
│   webpack.config.js
│   
├───nginx
│   └───conf.d
│           default.conf
├───php
│       Dockerfile
│       
├───public
│   │   .htaccess
│   │   index.php
│   │   
│   └───build
│           500.c01022e5.js
│           app.90dc63d1.js
│           app.c6fd8b2d.css
│           entrypoints.json
│           manifest.json
│           runtime.61b1725c.js

这些是我打开localhost:80时得到的错误

GET http://localhost/build/runtime.61b1725c.js net::ERR_ABORTED 404 (Not Found)
GET http://localhost/build/app.c6fd8b2d.css net::ERR_ABORTED 404 (Not Found)
GET http://localhost/build/app.90dc63d1.js net::ERR_ABORTED 404 (Not Found)
GET http://localhost/build/500.c01022e5.js net::ERR_ABORTED 404 (Not Found)
wfauudbj

wfauudbj1#

你应该把./public目录挂载到nginx容器中:

services:

  # nginx
  web: 
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
      - ./public:/var/www/html/public

  app:
    build: 
      dockerfile: ./php/Dockerfile
    volumes:
      - /var/www/html/vendor
      - .:/var/www/html

相关问题