【问题标题】:Nginx config not working on my docker containerNginx 配置在我的 docker 容器上不起作用
【发布时间】:2021-08-02 21:32:01
【问题描述】:

我是码头工人的新手。

我正在研究这个 docker-compose.yml,以便为 webserver、我的 laravel 和 db 的工作区创建这三个容器。大部分我都理解了。

它构建得很好,但是在 localhost 我得到的是 nginx 默认页面,而不是我的 laravel 公用文件夹。

我在 /nginx/conf.d/ 中有一个 app.conf 文件

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

version: '3.2'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: myapp-workspace
    container_name: myapp-workspace
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: "/var/www"
    volumes:
      - ./:/var/www
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: myapp-nginx
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - .:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: myapp-db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: 'database'
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_PASSWORD: 'password'
      MYSQL_USER: 'user'
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
        - ./dbdata:/var/lib/mysql/
        - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

Dockerfile

FROM php:7.3-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

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

# Install extensions
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

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

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

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

【问题讨论】:

  • 您的 nginx 配置将根文件夹设置为 /var/www/public,但您的 Dockerfile 将所有文件复制到 /var/www。您是否执行到 Docker 容器中并查看是否在上面的文件夹中找到了您的应用程序文件?
  • 是的,我有。谢谢回复,我已经找到问题了。还是试错不是学习的好方法哈哈,我还有很多问题。

标签: laravel docker nginx


【解决方案1】:

找到问题了。

似乎这一行“fastcgi_pass app:9000;”上的“app”部分在我的 app.conf 上应该是我的工作区容器的名称。

这样就可以了。

server {
listen 80;
index index.php index.html;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass myapp-workspace:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}

}

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2020-05-28
    • 1970-01-01
    • 2015-12-31
    • 2018-01-14
    • 1970-01-01
    相关资源
    最近更新 更多