gpt4 book ai didi

php - 使用 nginx-proxy 找不到 nginx 返回文件并且不加载静态文件

转载 作者:行者123 更新时间:2023-12-03 14:52:05 24 4
gpt4 key购买 nike

我正在使用 nginx-proxy-automation运行我使用 CodeIgniter 4 编写的 php 应用程序应用程序结构如下:

php-application
docker
php-fpm
config
php.ini
Dockerfile
src
node_modules
app
public
tests
vendor
writable
.env
composer.json
package.json
spark
docker-comopse.yml
index.php文件在 public 中可用文件夹。 docker-compose.ymlphp-application包含以下内容:
nginx
nginx-proxy
docker-compose.yml
php-application
docker-compose.yml
php-application/docker-compose.yml我有这个:
version: '3.7'

services:
php-fpm:
container_name: boilerplate_app
restart: always
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
volumes:
- ./src:/var/www/html
environment:
# NGINX-PROXY ENVIRONMENT VARIABLES: UPDATE ME
- VIRTUAL_HOST=mysite.com
- VIRTUAL_ROOT=/var/www/html/public
- VIRTUAL_PORT=9000
- VIRTUAL_PROTO=fastcgi
- LETSENCRYPT_HOST=mysite.com
- LETSENCRYPT_EMAIL=info@mysite.com
- NETWORK=proxy
# /END NGINX-PROXY ENVIRONMENT VARIABLES
ports:
- '9000:80'
expose:
- 9000
networks:
- proxy

database:
container_name: boilerplate_db
restart: always
build:
context: ./docker/database
environment:
- MYSQL_DATABASE=boilerplate
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sql

phpmyadmin:
container_name: boilerplate_phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8088:80
environment:
- PMA_HOST=database
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
depends_on:
- database

networks:
proxy:
external:
name: proxy
基本上我有三项服务:
  • php-fpm:挂载应用程序文件在/var/www/html文件夹,并在 environment部分,我指定了 VIRTUAL_PORT作为 9000 因为 php-fpm跑过fastcgi .然后我链接了proxy网络,这是您图像的网络。
  • 数据库:在应用网络内运行
  • phpmyadmin:在应用网络内运行
  • Dockerfile php-fpm的内容包含以下内容:
    FROM php:8.0.2-fpm-alpine
    WORKDIR /var/www/html

    RUN docker-php-ext-install pdo_mysql
    RUN docker-php-ext-install mysqli
    RUN apk add icu-dev

    # Install intl
    RUN docker-php-ext-configure intl && docker-php-ext-install intl

    # Install curl
    RUN apk add --update libzip-dev curl-dev &&\
    docker-php-ext-install curl && \
    apk del gcc g++ &&\
    rm -rf /var/cache/apk/*

    COPY docker/php-fpm/config/php.ini /usr/local/etc/php/

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

    # Install nodejs
    RUN apk add --update nodejs nodejs-npm
    RUN npm install gulp-cli -g
    RUN npm install

    COPY src src/

    CMD ["php-fpm"]

    EXPOSE 9000
    当我使用 docker-compose up --build -d 启动容器时我访问时收到此消息 mysite.com (为了隐私,我隐藏了真正的域名):

    File not found.


    检查 nginx使用 sudo docker logs -f nginx 登录我得到:
    [error] 30#30: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 2.38.140.109, server: mysite.com, request: "GET / HTTP/2.0", upstream: "fastcgi://172.28.0.7:9000", host: "mysite.com"
    mysite.com 2.38.140.109 - - [29/Mar/2021:17:52:31 +0000] "GET / HTTP/2.0" 404 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63"
    为了解决这个问题,我更新了 nginx.tmplthis一。
    现在的问题是,当我加载我的网站时,所有静态文件(例如 js/image/css)都返回 404。
    我能做些什么来解决这个问题?

    最佳答案

    为了使用 nginx 代理从容器中提供静态文件,nginx 代理必须静态访问文件......它不会“代理”而是“读取”并提供文件......这意味着您必须安装应用程序文件到代理...
    我建议你在 nginx 代理后面安装一个新的、轻量级和干净的 nginx 以及你的 php-fpm 服务,这样你就可以完全管理你的路线/位置,
    首先我创建了一个 docker-compose.yml在我的应用程序中包含以下内容:

    version: '3.9'

    services:

    php-fpm:
    container_name: boilerplate_app
    restart: always
    build:
    context: .
    dockerfile: ./docker/php-fpm/Dockerfile
    volumes:
    - ./src:/var/www/html

    nginx:
    image: nginx:stable-alpine
    container_name: boilerplate_nginx
    restart: always
    volumes:
    - ./src:/var/www/html
    - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
    - ./docker/nginx/sites/:/etc/nginx/sites-available
    - ./docker/nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
    - php-fpm
    environment:
    VIRTUAL_HOST: example.com
    LETSENCRYPT_HOST: example.com
    LETSENCRYPT_EMAIL: info@example.com

    database:
    container_name: boilerplate_db
    restart: always
    build:
    context: ./docker/database
    environment:
    - MYSQL_DATABASE=boilerplate
    - MYSQL_USER=user
    - MYSQL_PASSWORD=secret
    - MYSQL_ROOT_PASSWORD=secret
    volumes:
    - ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sql

    phpmyadmin:
    container_name: boilerplate_phpmyadmin
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
    - 8088:80
    environment:
    - PMA_HOST=database
    - MYSQL_USER=user
    - MYSQL_PASSWORD=secret
    - MYSQL_ROOT_PASSWORD=secret
    depends_on:
    - database

    networks:
    default:
    external:
    name: proxy
    然后,在我的 app 目录中,我创建了 nginx 配置结构:
    app
    docker
    nginx
    conf.d
    default.conf
    sites
    default.conf
    nginx.conf
    我在哪里 conf.d :
    upstream php-upstream {
    server php-fpm:9000;
    }
    那么我有 sites :
    server {
    root /var/www/html/public;
    index index.php;

    location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

    fastcgi_pass php-upstream;
    fastcgi_index index.php;
    }
    }
    nginx.conf :
    user  nginx;
    worker_processes 4;

    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;
    }
    网站现在加载,但有一些问题:
  • 性能问题:我们这里有两个nginx层,加载时间增加了很多
  • 如果我浏览一些像 example.com/admin 这样的 url,我会从 nginx 得到 404,猜测这是我自己的配置问题
  • 关于php - 使用 nginx-proxy 找不到 nginx 返回文件并且不加载静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66859178/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com