You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel+Nginx+MariaDB Docker容器Nginx配置文件报错求助

问题原因

你当前的Nginx配置结构逻辑错误:

  • 官方Nginx镜像的主配置/etc/nginx/nginx.conf已经包含全局指令(user、worker_processes等)和完整的http块,并通过include /etc/nginx/conf.d/*.conf;加载conf.d目录下的子配置。
  • 你把包含全局指令、events块、http块的完整配置放到了conf.d目录下,导致Nginx解析时出现嵌套错误(http块内重复嵌套http块),同时全局指令重复定义,触发配置报错。
  • 额外问题:你的报错配置里有一行无效代码m_header_time';,这也是语法错误的直接来源。
解决方案

步骤1:调整Docker Compose挂载

修改docker-compose.yml中Nginx服务的volumes,添加主配置文件挂载,用自定义主配置替换镜像默认的nginx.conf:

services:
  nginx:
    image: nginx:latest
    volumes:
      - ./:/var/www
      - ./_docker/nginx/nginx.conf:/etc/nginx/nginx.conf  # 新增:挂载主配置文件
      - ./_docker/nginx/conf.d:/etc/nginx/conf.d
    ports:
      - "8081:80"
    depends_on:
      - app
    container_name: beach_nginx

步骤2:重构Nginx配置文件

1. 主配置文件 _docker/nginx/nginx.conf

包含全局指令、events块和基础http配置,最后加载conf.d目录下的子配置:

user  nginx;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 30000;
pid /var/run/nginx.pid;
pcre_jit on;

events {
    worker_connections 8192;
    multi_accept on;
}

http {
    # Basic #######################
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    reset_timedout_connection on;
    keepalive_timeout 120;
    keepalive_requests      1000;
    types_hash_max_size     2048;
    server_tokens       off;
    send_timeout        30;
    client_body_timeout     30;
    client_header_timeout   30;
    server_names_hash_max_size  4096;

    # Limits ######################
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    client_body_temp_path   /var/cache/nginx/client_temp;

    proxy_connect_timeout   60;
    proxy_send_timeout      60;
    proxy_read_timeout      60;
    proxy_buffer_size       4k;
    proxy_buffers       8 16k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size  64k;
    proxy_temp_path     /var/cache/nginx/proxy_temp;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    # Logs ########################
    access_log  /var/log/nginx/access.log  main;
    error_log  /var/log/nginx/error.log;

    # Gzip ########################
    gzip on; 
    gzip_static on; 
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/x-icon image/svg+xml application/x-font-ttf;
    gzip_comp_level 9;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_disable "msie6";
    gzip_vary on;

    etag off;
    
    include /etc/nginx/conf.d/*.conf;
}

(注:已删除无效的m_header_time';行)

2. 子配置文件 _docker/nginx/conf.d/default.conf

仅保留server块内容(去掉外层的http、全局指令):

server {
    index index.html index.php;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    client_max_body_size 50m;
    
    location / {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        reset_timedout_connection on;
        keepalive_requests 1000;
        types_hash_max_size 2048;
        server_tokens off;
        send_timeout 30;
        client_body_timeout 30;
        try_files $uri $uri/ /index.php?$query_string;
        proxy_connect_timeout 60s;
        # 注:limit_req zone=one; 需要先在http块定义limit_req_zone,否则报错,暂时注释或添加对应配置
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache';
        if_modified_since off;
        expires off;
        etag off;
    }

    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;
    }
}

(注:若要启用limit_req zone=one;,需在主配置的http块中添加limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;)

步骤3:重启服务

执行命令重启Docker Compose服务:

docker-compose down
docker-compose up -d --build
额外检查

每次修改配置后,可在容器内测试配置语法是否正确:

docker exec -it beach_nginx nginx -t

内容的提问来源于stack exchange,提问作者Даниил Чернюк

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 16:07:33