docker nginx 安装和简单配置

  • 安装nginx
# 直接pull就行
docker search nginx #搜索
docker pull nginx #安装
  • 简单启动
#我们先简单运行nginx 需要拷贝其配置文件
docker run --name nginx -d -p 80:80 nginx
  • 下面创建文件夹方便进行映射,准备配置文件,其中ssl中放域名证书。
#创建文件夹 
mkdir -p /data/docker/nginx/{logs,ssl,html,conf/conf.d}/
#复制配置文件
docker cp nginx:/etc/nginx/nginx.conf /data/docker/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d/default.conf /data/docker/nginx/conf/conf.d/default.conf
  • 准备工作完成
#停止
docker stop nginx
#删除
docker rm nginx
  • 重新运行挂载
docker run --detach --name nginx -p 443:443 -p 80:80 --restart=always --privileged=true -v /data/docker/nginx/html:/usr/share/nginx/html:rw -v /data/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf/:rw -v /data/docker/nginx/conf/conf.d/:/etc/nginx/conf.d/:rw -v/data/docker/nginx/logs:/var/log/nginx/:rw -v /data/docker/nginx/ssl:/ssl/:rw -d nginx
  • 就可以访问啦,当然如果直接访问(这里是没有找到要显示的 html)
    在这里插入图片描述
  • 这里我们添加一个index.html 来显示内容
vim /data/docker/nginx/html/index.html
#写入下列代码,来测试我们的nginx

在这里插入图片描述

  • 保存退出(:wq)

  • 再次访问
    在这里插入图片描述

  • default.cnf 基本配置

server {
    listen    80;
    listen    443 ssl;
	
    # 增加ssl
    #ssl on;        #如果强制HTTPs访问,这行要打开
    ssl_certificate /ssl/ibalbal.com.pem;
    ssl_certificate_key /ssl/ibalbal.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    #ssl_protocols  SSLv2 SSLv3 TLSv1.2; #csdn 指定密码为openssl支持的格式
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #aliyun

    #ssl_ciphers  HIGH:!aNULL:!MD5;  # 密码加密方式 csdn
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #aliyun
    ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

     # 定义首页索引目录和名称
     location / {
	#默认情况
	root   /usr/share/nginx/html;
        index  index.html index.htm;
     }
     

    #重定向错误页面到 /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

  • nginx_proxy.cnf 代理配置
server {
    listen       80;
    listen    443 ssl;
    server_name  www.ibalbal.com;             #域名

    # 增加ssl
    #ssl on;        #如果强制HTTPs访问,这行要打开
    ssl_certificate /ssl/ibalbal.com.pem;
    ssl_certificate_key /ssl/ibalbal.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #aliyun

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #aliyun
    ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

    client_max_body_size 50m;
    client_body_buffer_size 50000m;
    fastcgi_intercept_errors on;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
       root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /portainer/ {
        proxy_pass http://111.111.111.111:9000/;
    }

    location /minio/ {

        proxy_pass http://111.111.111.111:9001;
    }

    location /oss/ {
        proxy_pass http://111.111.111.111:9001/;
    }

    location /kibana/ {
        proxy_pass http://111.111.111.111:5601/app;
    }

    #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;
    #}
}

  • nginx_upstream.cnf 负载均衡配置
upstream ib {
    server 111.111.111.111;
    server 111.111.111.112;
}
server {
    listen    80;       #侦听80端口,如果强制所有的访问都必须是HTTPs的,这行需要注销掉
    listen    443 ssl;
    server_name  www.ibalbal.com;             #域名

    # 增加ssl
    #ssl on;        #如果强制HTTPs访问,这行要打开
    ssl_certificate /ssl/ibalbal.com.pem;
    ssl_certificate_key /ssl/ibalbal.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    #ssl_protocols  SSLv2 SSLv3 TLSv1.2; #csdn 指定密码为openssl支持的格式
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #aliyun

    #ssl_ciphers  HIGH:!aNULL:!MD5;  # 密码加密方式 csdn
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #aliyun
    ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

     # 定义首页索引目录和名称
    location / {
	proxy_pass http://ib;
	tcp_nodelay     on;
       	proxy_set_header Host            $host;
       	proxy_set_header X-Real-IP       $remote_addr;
       	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

	#默认情况
	#root   /usr/share/nginx/html;
       	#index  index.html index.htm;
     } 
     

    #重定向错误页面到 /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

  • nginx_www.cnf 二级域名代理配置
server {
    listen    80;       #侦听80端口,如果强制所有的访问都必须是HTTPs的,这行需要注销掉
    listen    443 ssl;
    server_name  *.ibalbal.com;             #域名

    if ($http_host ~* "^(.*?)\.ibalbal\.com$") {    #正则表达式
	 set $domain $1;                     #设置变量
    }
	
    # 增加ssl
    #ssl on;        #如果强制HTTPs访问,这行要打开
    ssl_certificate /ssl/ibalbal.com.pem;
    ssl_certificate_key /ssl/ibalbal.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    #ssl_protocols  SSLv2 SSLv3 TLSv1.2; #csdn 指定密码为openssl支持的格式
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #aliyun

    #ssl_ciphers  HIGH:!aNULL:!MD5;  # 密码加密方式 csdn
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #aliyun
    ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

     # 定义首页索引目录和名称
    location / {
	if ($domain ~* "blog") {
	     proxy_pass http://ibalbal.com;
	}
	
	proxy_pass http://ibalbal.com;;
	tcp_nodelay     on;
       	proxy_set_header Host            $host;
       	proxy_set_header X-Real-IP       $remote_addr;
       	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

	#默认情况
	#root   /usr/share/nginx/html;
       	#index  index.html index.htm;
     } 
     

    #重定向错误页面到 /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

  • 请求转发详解
这里我们请求的网站为:192.168.1.0:80/static/a.html
第一种:
location后没有/      转发网站没有/

location /static{
proxy_pass  192.168.1.1:81
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/static/a.html

第二种:
location后没有/      转发网站有/

location /static{
proxy_pass  192.168.1.1:81/
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/a.html

第三种:
location后有/      转发网站没有/

location /static/{
proxy_pass  192.168.1.1:81
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/static/a.html

第四种:
location后有/      转发网站有/

location /static/{
proxy_pass  192.168.1.1:81/
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/a.html

总结:
从这四种我们可以的看出,当nginx里面匹配时可以把端口后的参数分为path1+path2(其中我在上方标注的location属于path1,proxy_pass属于path2)
当
proxy_pass  
里面是ip:port+/时nginx最后匹配的网址是 proxy_pass的内容加上path2
里面是ip:port时nginx最后匹配的网址是 proxy_pass的内容加上path1+path2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞飞y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值