docker 安装 Nginx

查看版本

复制代码
https://hub.docker.com/_/nginx/tags

拉取镜像(挑选一个)

复制代码
docker pull nginx:stable-alpine3.23-perl

配置

创建nginx的配置文件
复制代码
nginx.conf
bash 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    # 开启高效传输
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    # 长连接超时时间
    keepalive_timeout  65;

    # 开启gzip压缩(优化Vue静态资源加载速度)
    gzip  on;
    gzip_min_length  1k;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    server {
		listen 80;
		
		location /app1/ {
        alias /usr/share/nginx/html/app1/dist/;
        try_files $uri $uri/ /app1/index.html;
        add_header Cache-Control "no-cache, no-store";
		}

		# 错误页配置
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
			root   /usr/share/nginx/html;
		}
	}
}
创建docker-compose.yml 文件
bash 复制代码
docker-compose.yml
bash 复制代码
services:
  nginx:
    image: nginx:stable-alpine3.23-perl
    # 容器名称
    container_name: db-nginx
    # 开机自启
    restart: unless-stopped
    # 端口映射:主机80端口 -> 容器80端口(可根据需要修改,如8080:80)
    ports:
      - "17779:80"
      # 如果需要HTTPS,添加443端口映射
      # - "443:443"
    # 目录挂载(将本地目录映射到容器内)
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./html:/usr/share/nginx/html:ro
      - ./logs:/var/log/nginx

初始化启动

bash 复制代码
docker-compose up -d

二次启动

bash 复制代码
docker-compose start

停止

bash 复制代码
docker-compose stop

停止并删除容器

bash 复制代码
docker-compose down
相关推荐
wno70418 小时前
Nginx禁止蜘蛛程序访问
运维·nginx
csdn_aspnet19 小时前
如何使用 Docker 设置 PostgreSQL
运维·数据库·docker·postgresql·容器
赵文宇(温玉)1 天前
Docker离线安装
运维·docker·容器
程序员agions1 天前
Docker Compose 生产环境配置记录
运维·docker·容器
always20221 天前
AI运行时 · 附录3 - GPU 设备插件与 MIG:K8s 上的 AI 加速器编排
人工智能·容器·kubernetes
AR-26710-2 天前
docker Day6——容器数据卷
运维·docker·容器
AR-26710-2 天前
docker Day5——改动+systemf复习
运维·docker·容器
钱栈up3 天前
现场部署6个服务菜单全挂:根因不在后端,在前端那个环境变量
mysql·nginx
Csvn3 天前
Nginx 故障案例库:16 张卡的现象、证据、根因与防复发
后端·nginx
always20223 天前
AI运行时 · 06 - Kata 与 K8s 集成:RuntimeClass / CSI kata-directvolume / 性能开销与调优
人工智能·容器·kubernetes