【阿里云】使用docker安装nginx后可以直接访问

一、创建目录

powershell 复制代码
mkdir -p config/{cert,conf.d} html logs

二、上传nginx.conf的配置文件

powershell 复制代码
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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    # 新增 server 块以监听 80 端口
    server {
        listen 80;
        server_name _;  # 这里可以是你的域名,或者使用 "_" 来匹配所有未指定的主机名

        # 设置默认首页文件和站点根目录
        root /usr/share/nginx/html;
        index index.html index.htm;

        # 处理静态文件请求
        location / {
            try_files $uri $uri/ =404;
        }

        # 可选:重定向到 HTTPS (如果你有 SSL/TLS 证书)
        # return 301 https://$host$request_uri;
    }
}

三、配置docker-compose.yml文件

powershell 复制代码
version: '3.8'

services:
  nginx:
    image: nginx:1.23.3
    restart: unless-stopped
    container_name: nginx1.23.3
    ports:
      - "80:80"
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./config/nginx.conf:/etc/nginx/nginx.conf
      - ./config/conf.d/:/etc/nginx/conf.d/
      - ./config/cert/:/etc/nginx/cert/
      - ./html/:/usr/share/nginx/html/
      - ./logs/:/var/log/nginx/
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 30s
      timeout: 10s
      retries: 3

四、配置docker镜像加速源

powershell 复制代码
vim /etc/docker/daemon.json
powershell 复制代码
{
    "registry-mirrors": [
        "https://2a6bf1988cb6428c877f723ec7530dbc.mirror.swr.myhuaweicloud.com",
        "https://docker.m.daocloud.io",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com",
        "https://your_preferred_mirror",
        "https://dockerhub.icu",
        "https://docker.registry.cyou",
        "https://docker-cf.registry.cyou",
        "https://dockercf.jsdelivr.fyi",
        "https://docker.jsdelivr.fyi",
        "https://dockertest.jsdelivr.fyi",
        "https://mirror.aliyuncs.com",
        "https://dockerproxy.com",
        "https://mirror.baidubce.com",
        "https://docker.m.daocloud.io",
        "https://docker.nju.edu.cn",
        "https://docker.mirrors.sjtug.sjtu.edu.cn",
        "https://docker.mirrors.ustc.edu.cn",
        "https://mirror.iscas.ac.cn",
        "https://docker.rainbond.cc"
    ]
}

五、编写HTML

html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docker-compose搭建nginx</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

六、结果

相关推荐
Wpa.wk24 分钟前
Docker容器隔离(namespace+cgroups)-底层原理
运维·docker·容器
修己xj28 分钟前
Docker 部署 PicGo:一次配置,随处使用
运维·docker·容器
Ha_To28 分钟前
2026.1.29 docker的使用
运维·docker·容器
Hellc0071 小时前
Jenkins 上下游 Job + Docker 镜像部署完整实战(避坑版)
java·docker·jenkins
惊鸿Randy1 小时前
Docker 环境下 PostgreSQL 16 安装 pgvector 向量数据库插件详细教程(Bitnami 镜像)
数据库·docker·postgresql
Free Tester1 小时前
在Docker Node环境下安装包
docker·容器
Knight_AL1 小时前
Spring Boot + Docker:实现可挂载可热更新的 config.json
spring boot·docker·json
Wpa.wk1 小时前
Docker - 搭建镜像仓库- 了解
运维·经验分享·测试工具·docker·容器
小白考证进阶中1 小时前
阿里云ACP零基础一次过(个人经验)
阿里云·云计算·阿里云acp·acp云计算·阿里云acp考试·阿里云云计算·云计算acp
加油勇士2 小时前
NGINX 参数配置与调优
运维·服务器·nginx