docker-compose部署nginx

1.安装docker-compose
2.创建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中映射的文件夹

3.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;
    }

}

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

#重写路径的例子
#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;
}
4.配置生效重启,可写成reload.sh:
 docker-compose exec web nginx -s reload
#注:exec 正在运行的容器中执行命令:nginx -s reload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值