docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker-compose部署nginx

docker-compose部署nginx教程

作者:与民更始

文章介绍了如何安装和配置docker-compose,创建一个Nginx容器,并通过docker-compose.yml文件进行配置,包括映射文件夹和自定义转发配置,最后,提供了重启和配置生效的脚本

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文