docker nginx

安装最新版

docker pull nginx
docker images

主机服务器创建目录,新建nginx.conf配置,如:
/user/local/nginx/nginx.conf

使用-v挂载主机的nginx.conf,修改比较方便:

docker run --name nginx -p 80:80 -d nginx -v /user/local/nginx/nginx.conf:/etc/nginx/nginx.conf

查看是否行成功

docker ps 

在这里插入图片描述

如果/user/local/nginx/nginx.conf配置修改了,那么docker中正在运行的nginx容器需要reload:

docker restart 08c28d2d7bfe

示例配置文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

	upstream myserver {
		server 192.168.7.222:8081 weight=10;
		server 192.168.7.222:8082 weight=5;
	}

	server {
		listen	80;
		server_name 192.168.7.222;

		location / {
			root html;
			proxy_pass http://myserver;
			index index.html index.htm;
		}

		location /a {
			root html;
			proxy_pass http://192.168.7.222:8081;
			index index.html index.htm;
		}

		location /b {
			root html;
			proxy_pass http://192.168.7.222:8082;
			index index.html index.htm;
		}
	}

}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

如果配置有问题,启动报错,赶紧使用命令查看一下日志:
docker logs 容器ID
在这里插入图片描述
一眼就看出哪里报错了吧。

配置文件中使用正则的最佳姿势:

upstream myserver {
                server 192.168.7.222:8081 weight=10;
                server 192.168.7.222:8082 weight=5;
        }

        server {
                listen  80;
                server_name ~^(?<subdomain>.+)\.donge\.com$;
                #使用正则一定要波浪符 ~
                #变量名定义格式:(?<变量名>.+)

                access_log /var/log/nginx/access.log;
                error_log /var/log/nginx/error.log;

                set $port 8081;

				#if和(之间必须空格,否则if不能识别
				#$subdomain和=之间必须要有空格,否则$subdomain='bb'整个当作变量就报错了
                if ($subdomain = 'bb' ){
                        set $port 8082;
                }

                location / {
                        root html;
                        #负载均衡使用upstream定义的服务器名
                        #proxy_pass http://myserver;

						#使用变量$port
                        proxy_pass http://192.168.7.222:$port;
                        index index.html index.htm;
                }

                location /a {
                        root html;
                        proxy_pass http://192.168.7.222:8081;
                        index index.html index.htm;
                }

                location /b {
                        root html;
                        proxy_pass http://192.168.7.222:8082;
                        index index.html index.htm;
                }
        }

姿势总结:

  1. 使用正则一定要波浪符 ~
  2. 变量名定义格式:(?<变量名>.+)
  3. if和(之间必须空格,否则if不能识别
  4. s u b d o m a i n 和 = 之 间 必 须 要 有 空 格 , 否 则 subdomain和=之间必须要有空格,否则 subdomain=subdomain='bb’整个当作变量就报错了
  5. 负载均衡使用upstream定义的服务器名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值