服务器 > 服务器 > 云虚拟主机

docker-compose部署nginx教程

31人参与 2025-02-14 云虚拟主机

docker-compose部署nginx

安装docker-compose

创建nginx文件夹

添加docker-compose.yml文件:

version: '3.3'
services:
  web:
    image: "xtulnx/nginx:tengine-latest"
    container_name: nginx
    hostname: s.nginx
    volumes:
      - ./conf.d:/etc/nginx/conf.d
      - ./html:/etc/nginx/html
      - ./data:/data
      - ./logs:/etc/nginx/logs
    # 配置转发时可直接写 proxy:s.dev
    extra_hosts:
      - "s.host:170.170.0.1"
      - "s.dev:127.0.0.1"
    working_dir: /etc/nginx
    ports:
      - "80:80"
      - "443:443"
    environment:
      - nginx_port=80
    restart: always

在当前文件下添加volumes中映射的文件夹

conf.d文件下的default.conf

  log_format custom_log '$remote_addr - $remote_user [$time_local] '
                         '"$request" $status $body_bytes_sent '
                         '"$http_referer" "$http_user_agent" '
                         '$request_body $query_string '
                         '"$http_user_agent" "$http_x_forwarded_for" "$request_uri" '
                         'proxy_to: $upstream_addr';

  log_format  httplog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" $request_body $query_string'
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_uri"';

  log_format  uplg  '$remote_addr - $remote_user [$time_local] [$upstream_addr] "$request" [$request_body]'
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

  log_format  hu  '$remote_addr - $remote_user [$time_local] "$request" '                                                      
                  '"$status" $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" '
                  '"$gzip_ratio" $request_time $bytes_sent $request_length' ' $request_body';
    
  access_log  logs/access.log  custom_log;

#   sendfile        on;

  #增加一下websocket配置
  map $http_upgrade $connection_upgrade {
    default upgrade;
    ''   close;
  }
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
#创建de
    include conf.d/*.conf;
    location / {
        # root   /usr/share/nginx/html;
        root   /etc/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;
    }

}

自定义的一些转发配置文件例子:

#重写路径的例子
#rewrite ^/dev_user/(.*\.*)$ /user/$1;


location ^~/user/ {
    #s.dev是nginx的compose里面配置的
	proxy_pass                   http://s.dev:8080/user/;
	#proxy_pass                  http://10.22.22.22:8080/user/;
	proxy_set_header            host $host;
	proxy_set_header            x-real-ip $remote_addr;
	proxy_set_header            x-forwarded-for $proxy_add_x_forwarded_for;
	client_max_body_size        100m;
}

配置生效重启

docker-compose exec web nginx -s reload
#注:exec 正在运行的容器中执行命令:nginx -s reload

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

docker之tomcat8.5容器中如何部署war包

02-14

MySQL docker容器数据更新统计shell脚本代码方式

02-14

docker容器增加或者修改容器映射端口的实现方法

02-14

docker安装及运行MySQL5.7容器完整脚本及说明

02-14

docker-compose如何定义一个桥接网络,并为该网络配置一个IP地址池

02-14

Docker如何修改正在运行的容器的时区和时间

02-14

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论