Angular+Docker+Nginx部署报错:nginx.conf第10行存在意外"}"
解决Nginx配置语法错误:unexpected "}" in /etc/nginx/nginx.conf:10
问题背景
使用Docker+Nginx部署Angular 15应用,Harness流水线CI阶段已完成,但CD环节失败,报错:
[emerg] 1#1: unexpected "}" in /etc/nginx/nginx.conf:10
错误原因
该错误表明Nginx配置文件第10行存在语法解析问题,结合提供的配置文件,大概率是以下原因之一:
- 块闭合符号
}前存在多余空格或不可见特殊字符 - 配置文件使用了Windows风格的CRLF换行符,在Linux容器中触发解析错误
- 块结构嵌套不规范导致Nginx无法正确识别
修复方案
1. 修正Nginx配置文件
替换为格式规范的版本,确保块结构清晰、符号无冗余:
# worker_threads to run; # "auto" sets it to the number of CPU cores available in the system, # which offers the best performance. worker_processes auto; events { worker_connections 1024; } http { server { listen 0.0.0.0:80; listen [::]:80; default_type application/octet-stream; # Gzip Settings gzip on; gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; client_max_body_size 256M; # Brotli Settings (uncomment if Brotli module is installed in Nginx) # brotli on; # brotli_comp_level 4; # brotli_buffers 32 8k; # brotli_min_length 100; # brotli_static on; # brotli_types image/jpeg image/bmp image/svg+xml text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon; root /usr/share/nginx/html; # Security: Hide server version server_tokens off; location / { # Security: Pass original Server header proxy_pass_header Server; try_files $uri $uri/ /index.html =404; } } }
2. 同步Dockerfile端口配置
原Dockerfile中EXPOSE 8080与Nginx配置的listen 80不匹配,修正后保持一致性:
FROM node:16 AS builder WORKDIR /app COPY package*.json ./ RUN npm install --max-old-space-size=4096 COPY . . RUN npm run build -- --base-href /ui/ --configuration=development FROM nginx:alpine COPY --from=builder /app/dist/ /usr/share/nginx/html COPY ./nginx.conf /etc/nginx/nginx.conf # Match Nginx listen port EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
3. 本地验证(可选)
推送流水线前,本地先验证配置和镜像:
- 检查Nginx配置语法:
nginx -t -c ./nginx.conf - 构建并运行镜像:
docker build -t angular-nginx-app . docker run -p 8080:80 angular-nginx-app
访问http://localhost:8080/ui/确认应用正常加载。
内容的提问来源于stack exchange,提问作者Kapil Soni
相关产品推荐
相关产品推荐

