docker-compose容器部署nginx

0 版本

  • nginx镜像版本:nginx:1.21.3

1 配置文件说明

1.1 容器中的配置文件目录结构

└─/
  └─etc/
	 └─nginx/
		├─conf.d/
		│  └─default.conf
		├─fastcgi_params
		├─mime.types
		├─modules
		├─nginx.conf
		├─scgi_params
		└─uwsgi_params

1.2 nginx.conf结构说明

全局配置;

events配置{
	..
}

http配置{
	http全局配置;
	upstream配置{
		..
	}
	server配置{
		server全局配置;
		location配置{
			..
		}
	}
}

1.3 nginx.conf配置文件

  • 下面是容器中nginx.conf配置文件的内容,由include /etc/nginx/conf.d/*.conf可以看出,我们可以将自定义配置放到/etc/nginx/conf.d/路径中,容器会自动加载这里的配置文件
    # 全局配置
    user  nginx;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    # events配置
    events {
        worker_connections  1024;
    }
    
    # http配置
    http {
    	# 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;
    	
    	# upstream和server配置可以在/etc/nginx/conf.d/*.conf中进行配置
        include /etc/nginx/conf.d/*.conf;
    }
    
  • 下面是default.conf 自定义配置的内容
    # server配置
    server {
    	# server全局配置
        listen       80;
        listen  [::]:80;
        server_name  localhost;
    
        #access_log  /var/log/nginx/host.access.log  main;
    	
    	# location配置
        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;
        #}
    }
    

2 dokcer-compose启动

2.1 目录结构

  • orion-web是一个项目名称,根据实际情况编写
    ├─nginx
    │	├─docker-compose.yml
    │	├─config
    │	│  	└─orion-web.conf
    │	└─orion-web
    │	│	└─..(静态资源)
    
  • orion-web.conf配置文件
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    

2.2 配置docker-compose.yml文件

  • docker-compose.yml
    version: '3'
    services:
      image: nginx:1.21.3
      ports:
        - "80:80"
      volumes:
        - "./config/orion-web.conf:/etc/nginx/conf.d/orion-web.conf"
        - "./orion-web:/usr/share/nginx/html"
    
  • 参数说明
    • volumes
      • ./orion-web:/usr/share/nginx/html 将宿主机的静态资源映射到容器中的/usr/share/nginx/html
      • ./config/orion-web.conf:/etc/nginx/conf.d/orion-web.conf 根据静态资源路径编写配置文件,然后配置后端反向代理(此处暂无配置),这样之后就可以将其映射到/etc/nginx/conf.d/路径中了
    • ports 端口映射
    • image 镜像版本

2.3 启动

  • 启动:在docker-compose.yml所在目录中执行docker-compse up -d
  • 停止并删除:docker-compose down
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值