首页 > 系统相关 >46_docker-compose_nginx

46_docker-compose_nginx

时间:2024-03-13 11:00:58浏览次数:21  
标签:log 46 nginx html proxy conf docker

1. 安装 Docker-compose
curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
2.拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:nginx-1.21.5
3. 配置文件 ./nginx/conf/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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 {
        listen       80;
        server_name  localhost; # 服务器地址或绑定域名

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;



        # =========================================================
        # ================== ↓↓↓↓↓↓ start ↓↓↓↓↓↓ ==================
        # =========================================================

        location / {
            root   /usr/share/nginx/html;
            #try_files $uri $uri/ @router;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 解决页面刷新 404 问题
            #proxy_pass http://zhengqingya.gitee.io; # 代理的ip地址和端口号
            #proxy_connect_timeout 600; #代理的连接超时时间(单位:毫秒)
            #proxy_read_timeout 600; #代理的读取资源超时时间(单位:毫秒)
        }

        #location @router {
            #rewrite ^.*$ /index.html last; # 拦截80端口后的所有请求地址到登录页面 -> 相当于后端的拦截器
        #}

     #   location ^~ /api {  # ^~/api/表示匹配前缀为api的请求
     #       proxy_pass  http://www.zhengqingya.com:5000/api/;  # 注:proxy_pass的结尾有/, -> 效果:会在请求时将/api/*后面的路径直接拼接到后面
#
     #       #  proxy_set_header作用:设置发送到后端服务器(上面proxy_pass)的请求头值
     #       #   【当Host设置为 $http_host 时,则不改变请求头的值;
     #       #     当Host设置为 $proxy_host 时,则会重新设置请求头中的Host信息;
     #       #     当为$host变量时,它的值在请求包含Host请求头时为Host字段的值,在请求未携带Host请求头时为虚拟主机的主域名;
     #       #     当为$host:$proxy_port时,即携带端口发送 ex: $host:8080 】
     #       proxy_set_header Host $host;
#
     #       proxy_set_header X-Real-IP $remote_addr; # 在web服务器端获得用户的真实ip 需配置条件①    【 $remote_addr值 = 用户ip 】
     #       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web服务器端获得用户的真实ip 需配置条件②
     #       proxy_set_header REMOTE-HOST $remote_addr;
     #       # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for变量 = X-Forwarded-For变量
     #   }

     #   location ^~ /blog/ {
     #       proxy_pass  http://zhengqingya.gitee.io/blog/;  # ^~/blog/表示匹配前缀是blog的请求,proxy_pass的结尾有/, 则会把/blog/*后面的路径直接拼接到后面,即移除blog
#
     #       proxy_set_header Host $proxy_host; # 改变请求头值 -> 转发到码云才会成功
     #       proxy_set_header  X-Real-IP  $remote_addr;
     #       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
     #       proxy_set_header X-NginX-Proxy true;
     #   }


        # =========================================================
        # ================== ↑↑↑↑↑↑ end ↑↑↑↑↑↑ ==================
        # =========================================================


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }
}
主配置文件 /nginx/conf/conf.d/default.conf
server {
  listen       80;
  server_name  localhost;

  #charset koi8-r;
  #access_log  /var/log/nginx/host.access.log  main;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
  }

  #error_page  404              /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #    root           html;
  #    fastcgi_pass   127.0.0.1:9000;
  #    fastcgi_index  index.php;
  #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  #    include        fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
}
前端错误页面 ./nginx/html/50x.html
<!DOCTYPE html>
<html>
  <head>
    <title>Error</title>
    <style>
      body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>An error occurred.</h1>
    <p>
      Sorry, the page you are looking for is currently unavailable.<br />
      Please try again later.
    </p>
    <p>
      If you are the system administrator of this resource then you should check
      the error log for details.
    </p>
    <p><em>Faithfully yours, nginx.</em></p>
  </body>
</html>
前端默认页
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to nginx!</title>
    <style>
      body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to nginx!</h1>
    <p>
      If you see this page, the nginx web server is successfully installed and
      working. Further configuration is required.
    </p>

    <p>
      For online documentation and support please refer to
      <a href="http://nginx.org/">nginx.org</a>.<br />
      Commercial support is available at
      <a href="http://nginx.com/">nginx.com</a>.
    </p>

    <p><em>Thank you for using nginx.</em></p>
  </body>
</html>
4. docker-compose.yml
version: "3"
services:
  nginx:
    image: registry.cn-hangzhou.aliyuncs.com/ns-2023/lib-2023:nginx-1.21.5
    container_name: nginx # 容器名为'nginx'
    restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
    volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
      - "./nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
      - "./nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
      - "./nginx/html:/usr/share/nginx/html"
      - "./nginx/log:/var/log/nginx"
    environment: # 设置环境变量,相当于docker run命令中的-e
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
    ports: # 映射端口
      - "80:80"
5. 运行
# 运行
docker-compose -f docker-compose-nginx.yml -p nginx up -d

# 进入容器
docker exec -it nginx /bin/bash

# nginx修改配置后重载
nginx -s reload

标签:log,46,nginx,html,proxy,conf,docker
From: https://www.cnblogs.com/ckh2023/p/18070161

相关文章

  • windows 配置 docker
    安装dockerdesktop安装后修改路径在Microsoftstore安装Linux的任意版本安装后需要将Linux迁移到其他盘,避免减少c盘空间。开发时,项目放在Linux子系统目录下,避免文件系统不一致导致文件修改时不会触发监听事件。docker中将安装的Linux选为默认系统VScode安......
  • 41_Docker网络
    Docker网络常用命令dockerhelpnetworkconnectConnectacontainertoanetworkcreateCreateanetworkdisconnectDisconnectacontainerfromanetworkinspectDisplaydetailedinformationononeormorenetworksls......
  • 40_DockerFile简介
    Dockerfile简介常用保留字FROM基础镜像,当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板,第一条必须是FROMMAINTAINER镜像维护者的姓名和邮箱地址RUN容器构建时需要运行的命令,有两种格式RUNyum-yinstallvimRUN[".......
  • docker-compose 部署gitlab
    主机ip:192.168.1.139[root@localhostgitlab_docker]#catdocker-compose.ymlversion:'3.1'services:gitlab:image:'gitlab/gitlab-ce:latest'container_name:gitlab#随着docker重启自动启动restart:alwaysenvironment:......
  • 43_Docker可视化工具
    portainer安装dockerrun-d-p8000:8000-p9000:9000\--nameportainer\--restart=always\-v/var/run/docker.sock:/var/run/docker.sock\-vportainer_data:/data\portainer/portainer#IP:9000进入容器监控三剑客docker-comp......
  • 42_Docker容器编排
    下载安装docker-composecurl-L"https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname-s)-$(uname-m)"-o/usr/local/bin/docker-compose#curl-L"https://get.daocloud.io/docker/compose/releases/download/v2.17.2/d......
  • Nginx安装nginx-rtmp-module模块
    简介nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。流程查看当前nginx的版本(假设安装位置为:/usr/local/nginx)下......
  • docker it 多条命令
    目录Dockerit选项多条命令的使用准备工作在容器内执行多条命令在Docker容器内部备份数据库并上传至远程存储场景描述示例代码-i选项-t选项-it组合选项示例用法结语Dockerit选项多条命令的使用在Docker中,dockercontainerexec命令可以在正在运行的容器内部......
  • 如何查看docker 项目的配置文件
    目录如何查看Docker项目的配置文件方法一:通过Docker命令查看配置文件方法二:通过数据卷挂载方式查看配置文件方法三:使用Docker可视化工具查看配置文件示例场景方法一:通过Docker命令查看配置文件方法二:通过数据卷挂载方式查看配置文件方法三:使用Docker可视化工具查看配......
  • Rust 构建开源 Pingora 框架可以与nginx媲美
    一、概述Cloudflare为何弃用Nginx,选择使用Rust重新构建新的代理Pingora框架。Cloudflare成立于2010年,是一家领先的云服务提供商,专注于内容分发网络(CDN)和分布式域名解析。它提供一系列安全和性能优化服务,包括防火墙、DDoS防护、SSL/TLS加密和威胁分析。二、Pingora......