Docker最新安装Nginx快速上手(教程全面讲解)

Nginx 介绍

1、稳定性极强 7*24小时不间断运行
2、Nginx提供了非常丰富的配置实例
3、占用内存小,并发能力强 最高能承受 5万以上

Nginx 安装

yum install gcc libffi-devel python-devel openssl-devel -y
yum install docker
version:版本号,好像我这上面22.0有区别,不能写成2,写成2的话,docker-compose up -d 时会报错,提示版本号要写成2.0的样子,不过有的地方我看着直接写成2也是可以的,可能是我的docker-compose版本不一致。
service:就是要定义的docker容器
nginx:容器的名称
restart:设置为always,表明此容器应该在停止的情况下总是重启,比如,服务器启动时,这个容器就跟着启动,不用手动启动,服务器启动之后,进入到docker-compose.yml文件路径下,执行docker-compose ps可以看到,该容器正在运行。
image:这个是需要依赖的容器,也就是nginx软件,可以到docker官方镜像上找到最新版的镜像。
ports:这个是容器自己运行的端口号和需要暴露的端口号。比如: - 8080:80,表示容器内运行着的端口是80,把端口暴露给8080端口,从外面访问的是8080端口,就能自动映射到80端口上。
volumes:这个是数据卷。表示数据、配置文件等存放的位置。(- . 这个表示docker-compose.yml当前目录位置开始创建这个文件)
version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80
image --镜像地址 
container_name 容器的名字 

#目录
cd /opt/docker_nginx/docker-compose.yml

就直接运行 docker-compose up -d 

启动

 sudo systemctl start docker

删除

sudo find / -name *subpro*.egg-info

Nginx配置文件

[root@bogon docker_nginx]# docker ps 
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                NAMES
91f9dd7f9f8b        daocloud.io/library/nginx:latest   "/docker-entrypoin..."   7 minutes ago       Up 7 minutes        0.0.0.0:80->80/tcp   nginx
[root@bogon docker_nginx]# docker exec -it 91f9dd7f9f8b bash

地址

/etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


#以上统称位全局块 worker_processes他的数值越大,Nginx的并发能力就越强

events {
    worker_connections  1024;
}
# events
# worker_connections他的数值越大,Nginx的并发能力就越强

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;

    #include /etc/nginx/conf.d/*.conf;
    
    server {
      listen       80;
      listen  [::]:80;
      server_name  localhost;

      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
      # 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;
      }
    }
    # server 块
    # listen 代表nginx监听的端口号
    # localhost 代表nginx接受请求的ip
  
}

# http块
# include代表引入一个外部的文件 -> /mimo.types中存着大量的媒体类型 
# include /etc/nginx/conf.d/*.conf;  -> 引入了conf.d目录下的以.conf位结尾的配置文件 

修改Docker-compose.yml的文件

退出

root@91f9dd7f9f8b:/opt# exit

关闭

[root@bogon docker_nginx]# docker-compose down

修改文件

当前目录下映射nginx配置文件(/etc/nginx/conf.d)->这部分是docker下的

version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx 
    ports:
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d

重新构建

docker-compose build 

启动

docker-compose up -d 

[root@bogon docker_nginx]# pwd
/opt/docker_nginx/conf.d

server {

        listen 80;
        server_name localhost;


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

}
[root@bogon docker_nginx]# docker-compose restart 

Nginx的反向代理的介绍

正向代理

正向服务器帮你访问百度
在这里插入图片描述

  1. 正向代理服务时由客户端设立的
  2. 客户端了解代理服务器和目标服务器都是谁
  3. 帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址

反向代理

域名解析成ip,反向代理服务器,在众多的淘宝服务器中挑选一台

在这里插入图片描述

  1. 反向代理服务器,配置时服务端的
  2. 客户端不知道访问到底是哪一台服务器
  3. 达到负载均衡,并且可以隐藏服务器真正的ip地址

Nginx的反向代理

准备一个目标服务器

[root@bogon conf.d]# pwd
/opt/docker_nginx/conf.d

动态资源转发

在xxx.xx.xx.xxx的机子上

server {

        listen 80;
        server_name localhost;
		
		# proxy_pass 动态资源转发请求在进来直接转发到http://xxx.xx.xx.xxx:8080/;
        # 基于反向代理访问到Tomcat服务器
        location / {
                proxy_pass http://xxx.xx.xx.xxx:8080/;
        }


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

}

Nginx中的location映射路径

1= 匹配 
location  = / {
	
	#精准匹配,主机名后面不能带任何的字符串   www.baidu.com/xxx就匹配不到
}

2、通用匹配 

location /xxx {
	
	#匹配所有以/xxx开头的路径
}

3、正则匹配
location ~ /xx {

	#匹配所有以/xxx开头的路径
}

4、匹配开头路径  异或
location ^~ /images/ {
	# 匹配所有以images开头的路径 
}

5、匹配以gif或者jpg或者png位结尾的路径
~* \.(gif|jpg|png)$ {
	
	#匹配以gif或者jpg或者png位结尾的路径
}

优先级关系


(location =) > (location /xxx/yyy) > (location ^~) > (location ~ , ~* \.(gif|jpg|png)$ ) > (location)

负载均衡

Nginx 为我们默认提供了三种负载均衡的策略

1、轮询:
将客户端发起的请求,平均的分配给每一台服务器
2、权重:
会将客户端的请求,根据服务求的权重值不同,分配不同的数量
3、ip_hash:
基于发起请求的客户端的ip地址不同,他始终会将请求发送指定的服务器上

下面是三台服务器

upstream my-server{
		#  ip_hash;
        server xxx.xx.xx.xxx:80 weight=10;
        server xxx.xx.xx.xxx;
        server xxx.xx.xx.xxx:80 weight=2;
}


server {

        listen 80;
        server_name localhost;

        # 动态资源转发 基于反向代理访问到Tomcat服务器
        #location / {
        #       proxy_pass http://xxx.xx.xx.xxx:8080/;
        #}

        location / {
                proxy_pass http://my-server/;

        }


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

}

Nginx动静分离

就是快速简单的说下,如果你放到阿里云这些或者其它的就单说,这里不考虑

在这里插入图片描述

动态资源

worker_connections * worker_processes / 4 | 2 = Nginx最终的并发能力 

动态资源需要/4

为什么除以4 nginx接受客户端请求 ,nginx发送请求到服务器,服务器给nginx响应,nginx在个客户端一个响应(动态资源最少4个链接数)


访问某些 动态资源(通过数据库,或者其它地方拿到数据)

Nginx帮你访问到服务器,服务器返回结果是给Nginx的,nginx在把结果交给客户端

静态资源

静态资源/2

nginx在本地去找,有没有图片和静态资源,如果有直接返回

静态资源是访问nginx服务所在的服务器中的一些静态资源,通过docker添加一个配置,把nginx所在的服务器的路径,映射到数据卷中

先修改docker ,添加一个数据卷,映射到Nginx服务器的一个目录

映射一个路径叫做img:我映射我/data/img的目录下

修改配置文件

[root@bogon docker_nginx]# pwd
/opt/docker_nginx

[root@bogon docker_nginx]# vim docker-compose.yml 
version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
      - /opt/docker_nginx/img/:/data/img
      - /opt/docker_nginx/html/:/data/html

关闭

[root@bogon docker_nginx]# docker-compose down

重启

[root@bogon docker_nginx]# docker-compose up -d

放入内容

在这里插入图片描述
在这里插入图片描述

修改配置文件

[root@bogon conf.d]# pwd
/opt/docker_nginx/conf.d
-rw-r–r--. 1 root root 604 Aug 11 03:27 default.conf


server {

        listen 80;
        server_name localhost;

        # 动态资源转发 基于反向代理访问到Tomcat服务器
        #location / {
        #       proxy_pass http://xxx.xx.xx.xxx:8080/;
        #}

        #代理静态资源
        location /html {
                root /data;
                index index.html;
        }

        #代理到img静态资源
        location /img {
                root /data;
                autoindex on;  #静态全部内容
        }


#       location / {
#               proxy_pass http://my-server/;
#
#       }


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

}

展示

在这里插入图片描述

在这里插入图片描述

目前改线上的简单的配置

#运行用户
user www-data;    
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

#工作模式及连接数上限
events {
    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  1024;#单个后台worker process进程的最大并发链接数
    # multi_accept on; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
     #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #设定日志格式
    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    
    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #设定请求缓冲
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

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

    #设定负载均衡的服务器列表
     upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
    server xxx.xxx.x.x:3128 weight=5;
    server xxx.xxx.x.x:80  weight=1;
    server xxx.xxx.x.x:80  weight=6;
    }


   server {
    #侦听80端口
        listen       80;
        #定义使用www.xx.com访问
        server_name  www.xx.com;

        #设定本虚拟主机的访问日志
        access_log  logs/www.xx.com.access.log  main;

    #默认请求
    location / {
          root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称

          proxy_pass  http://mysvr ;#请求转向mysvr 定义的服务器列表

          #以下是一些反向代理的配置可删除.

          proxy_redirect off;

		 #禁用缓存
     	 #proxy_buffering off;

          #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
          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 10m;    #允许客户端请求的最大单文件字节数
          client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,
          proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)
          proxy_send_timeout 90;        #后端服务器数据回传时间(代理发送超时)
          proxy_read_timeout 90;         #连接成功后,后端服务器响应时间(代理接收超时)
          proxy_buffer_size 4k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小
          proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
          proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)
          proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传



    # 定义错误提示页面
    error_page   500 502 503 504 /50x.html;  
        location = /50x.html {
        root   /root;
    }

    #静态文件,nginx自己处理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
        expires 30d;
    }
    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
    location ~ \.php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #设定查看Nginx状态的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    #禁止访问 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
     
     }
}

2020最新 Nginx教程全面讲解(Nginx快速上手)
记一次用pip安装docker-compose报错及解决方法
如何实现Nginx的高可用负载均衡
Nginx 简易教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

伟伟哦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值