本文共 9908 字,大约阅读时间需要 33 分钟。
根据B站视频做的笔记,链接如下
2020最新 Nginx教程全面讲解(Nginx快速上手)一共14集,每集5到10分钟,个人感觉讲的很不错但是需要有docker基础,因为是基于docker快速搭建的nginx
Nginx介绍
1.1引言 为什么要学习Nginx 问题1:客户端到底要将请求发送给哪台服务器 问题2:如果所有客户端的请求都发送给了服务器1 问题3:客户端发送的请求可能是申请动态资源的,也有申请静态资源的 服务器搭建集群后 在搭建集群后,使用Nginx做反向代理 1.2Nginx介绍 Nginx是由俄罗斯人研发的,应对Rambler的网站并发,并且2004年发布的第一个版本Nginx的特点
1.稳定性极强,7*24小时不间断运行(就是一直运行) 2.Nginx提供了非常丰富的配置实例 3.占用内存小,并发能力强(随便配置一下就是5w+,而tomcat的默认线程池是150) Nginx的安装 2.1安装Nginx 使用docker-compose安装#在/opt目录下创建docker_nginx目录
cd /opt mkdir docker_nginx #创建docker-compose.yml文件并编写下面的内容,保存退出 vim docker-compose.yml version: '3.1' services: nginx: restart: always image: daocloud.io/library/nginx:latest container_name: nginx ports: - 80:80 1 2 3 4 5 6 7 8 执行docker-compose up -d 1 访问80端口,看到下图说明安装成功(ncthz.top是我阿里云服务器的域名,大家输入自己服务器的Ip就可以访问80端口了)2.2Nginx的配置文件
#查看当前nginx的配置需要进入docker容器中 docker exec -it 容器id bash #进入容器后 cd /etc/nginx/ cat nginx.conf nginx.conf文件内容如下 user nginx; worker_processes 1;error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; # 以上同城为全局块 # worker_processes的数值越大,Nginx的并发能力就越强 # error_log代表Nginx错误日志存放的位置 # pid是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;
}# http块
# include代表引入一个外部文件 # include /etc/nginx/mime.types; mime.types中存放着大量媒体类型 #include /etc/nginx/conf.d/*.conf; 引入了conf.d下以.conf为结尾的配置文件 conf.d目录下只有一个default.conf文件,内容如下server {
listen 80; listen [::]:80; server_name localhost;#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;location / {
root /usr/share/nginx/html; index index.html index.htm; } # 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; #} }# server块
# listen代表Nginx监听的端口号 # server_name代表Nginx接受请求的IP 2.3修改docker-compose文件 #退出容器 exit #关闭容器 docker-compose down #修改docker-compose.yml文件如下 1 2 3 4 5 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 bulid #重新启动容器 docker-compose up -d 这时我们再次访问80端口是访问不到的,因为我们映射了数据卷之后还没有编写server块中的内容我们在/opt/docker_nginx/conf.d下新建default.conf,并插入如下内容
server {
listen 80; listen [::]:80; server_name localhost;location / {
root /usr/share/nginx/html; index index.html index.htm; } } #重启nginx docker-compose restart 这时我们再访问80端口,可以看到是访问成功的Nginx的反向代理
3.1正向代理和反向代理介绍 正向代理: 1.正向代理服务是由客户端设立的 2.客户端了解代理服务器和目标服务器都是谁 3.帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址 反向代理: 1.反向代理服务器是配置在服务端的 2.客户端不知道访问的到底是哪一台服务器 3.达到负载均衡,并且可以隐藏服务器真正的ip地址 3.2基于Nginx实现反向代理 准备一个目标服务器 启动tomcat服务器 编写nginx的配置文件(/opt/docker_nginx/conf.d/default.conf),通过Nginx访问到tomcat服务器 准备tomcat服务器docker run -d -p 8080:8080 --name tomcat daocloud.io/library/tomcat:8.5.15-jre8
#或者已经下载了tomcat镜像 docker run -d -p 8080:8080 --name tomcat 镜像的标识#添加数据卷
docker run -it -v /宿主机绝对目录:/容器内目录 镜像名 default.conf文件内容如下server {
listen 80; listen [::]:80; server_name localhost;location / {
proxy_pass http://ncthz.top:8080/; } } #重启nginx docker-compose restart 这时我们访问80端口可以看到8080端口tomcat的默认首页3.3关于Nginx的location路径映射
优先级关系: (location = ) > (location /xxx/yyy/zzz) > (location ^~) > (location ~,~*) > (location /起始路径) > (location /) # 1. = 匹配 location / { #精准匹配,主机名后面不能带任何字符串 #例如www.baidu.com不能是www.baidu.com/id=xxx } #2. 通用匹配 location /xxx { #匹配所有以/xxx开头的路径 #例如127.0.0.1:8080/xxx xxx可以为空,为空则和=匹配一样 } #3. 正则匹配 location ~ /xxx { #匹配所有以/xxx开头的路径 } #4. 匹配开头路径 location ^~ /xxx/xx { #匹配所有以/xxx/xx开头的路径 } #5. 匹配结尾路径 location ~* \.(gif/jpg/png)$ { #匹配以.gif、.jpg或者.png结尾的路径 } 修改/opt/docker_nginx/conf.d/default.conf如下server {
listen 80; listen [::]:80; server_name localhost;location /index {
proxy_pass http://ncthz.top:8081/; #tomcat首页 }location ^~ /CR/ {
proxy_pass http://ncthz.top:8080/CR/; #毕设前台首页 }location / {
proxy_pass http://ncthz.top:8080/CRAdmin/; #毕设后台首页 } } #重启nginx docker-compose restart#访问ncthz.top/index可以进入tomcat首页
#访问ncthz.top/CR/XXX可以进入毕设前台首页 #访问ncthz.top或者ncthz.top:80可以进入毕设后台首页 Nginx负载均衡 Nginx为我们默认提供了三种负载均衡的策略: 1.轮询: 将客户端发起的请求,平均分配给每一台服务器 2.权重: 会将客户端的请求,根据服务器的权重值不同,分配不同的数量 3.ip_hash: 基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上 就是说如果这个客户端的请求的ip地址不变,那么处理请求的服务器将一直是同一个 4.1轮询 想实现Nginx轮询负载均衡机制只需要修改配置文件如下 1 upstream my_server{ server ncthz.top:8080; server ncthz.top:8081; } server { listen 80; listen [::]:80; server_name localhost;location / {
proxy_pass http://my_server/; #tomcat首页 } } upstream 名字{ server ip:端口; server 域名:端口; } server { listen 80; listen [::]:80; server_name localhost;location / {
proxy_pass http://upstream的名字/; } } #重启nginx docker-compose restart 多次刷新ncthz.top页面,根据版本号我们可以发现我们进入的是不同的tomcat4.2权重
实现权重的方式:在配置文件中upstream块中加上weight upstream my_server{ server ncthz.top:8080 weight=10; server ncthz.top:8081 weight=2; } server { listen 80; listen [::]:80; server_name localhost;location / {
proxy_pass http://my_server/; #tomcat首页 } } 4.3ip_hash 实现ip_hash方式:在配置文件upstream块中加上ip_hash; 1 upstream my_server{ ip_hash; server ncthz.top:8080 weight=10; server ncthz.top:8081 weight=2; } server { listen 80; listen [::]:80; server_name localhost;location / {
proxy_pass http://my_server/; #tomcat首页 } } Nginx动静分离 Nginx的并发能力公式: worker_processes * worker_connections / 4|2 = Nginx最终的并发能力 动态资源需要/4,静态资源需要/2 Nginx通过动静分离来提升Nginx的并发能力,更快的给用户响应 5.1动态资源代理 #配置如下 location / { proxy_pass 路径; } 5.2静态资源代理 #停掉nginx docker-compose down修改docker-compose.yml添加静态资源数据卷
不同版本的静态资源位置可能不同,可以在2.2中查看默认的位置(location块中root后的路径)#启动nginx
docker-compose up -d 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/html/:/usr/share/nginx/html 在/opt/docker_nginx/html下新建一个index.html 在index.html里面随便写点东西展示 修改nginx的配置文件location / {
root /usr/share/nginx/html; index index.html; } #配置如下 location / { root 静态资源路径; index 默认访问路径下的什么资源; autoindex on;#代表展示静态资源的全部内容,以列表的形式展开 } #重启nginx docker-compose restart #访问ncthz.top如下 Nginx集群 6.1引言 单点故障,避免nginx的宕机,导致整个程序的崩溃 准备多台Nginx 准备keepalived,监听nginx的健康情况 准备haproxy,提供一个虚拟的路径,统一的去接收用户的请求 6.2搭建 #先准备好以下文件放入/opt/docker_nginx_cluster目录中 #然后启动容器 注意确保80、8081和8082端口未被占用(或者修改docker-compose.yml中的端口) docker-compose up -d然后我们访问8081端口可以看到master,访问8082端口可以看到slave
因为我们设置了81端口的master优先级未200比82端口的slave优先级100高,所以我们访问80端口看到的是master 现在我们模仿8081端口的nginx宕机了 docker stop 8081端口nginx容器的ID 这时我们再去访问80端口看到的就是slave了 DockerfileFROM nginx:1.13.5-alpine
RUN apk update && apk upgrade
RUN apk add --no-cache bash curl ipvsadm iproute2 openrc keepalived
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
entrypoint.sh#!/bin/sh
#/usr/sbin/keepalvined -n -l -D -f /etc/keepalived/keepalived.conf --dont-fork --log-console &
/usr/sbin/keepalvined -D -f /etc/keepalived/keepalived.conf nginx -g "daemon off;" docker-compose.ymlversion: "3.1"
services: nginx_master: build: context: ./ dockerfile: ./Dockerfile ports: -8081:80 volumes: - ./index-master.html:/usr/share/nnginx/html/index.html - ./favicon.ico:/usr/share/nnginx/html/favicon.ico - ./keepalived-master.conf:/etv/keepalived/keepalived.conf networks: static-network: ipv4_address:172.20.128.2 cap_add: - NET_ADMIN nginx_slave: build: context: ./ dockerfile: ./Dockerfile ports: -8082:80 volumes: - ./index-slave.html:/usr/share/nnginx/html/index.html - ./favicon.ico:/usr/share/nnginx/html/favicon.ico - ./keepalived-slave.conf:/etv/keepalived/keepalived.conf networks: static-network: ipv4_address:172.20.128.3 cap_add: - NET_ADMIN proxy: image: haproxy:1.7-apline ports: - 80:6301 volumes: - ./happroxy.cfg:/usr/local/etc/haproxy/haproxy.cfg networks: - static-network networks: static-network: ipam: congig: - subnet: 172.20.0.0/16 keepalived-master.confvrrp_script chk_nginx {
script "pidof nginx" interval 2 }vrrp_instance VI_1 {
state MASTER interface etch0 #容器内部的网卡名称 virtual_router_id 33 priority 200 #优先级 advert_int 1 autheentication { auth_type PASS auth_pass letmein }virtual_ipaddress {
172.20.128.50 #虚拟路径 }track_script {
chk_nginx } } keepalived-slave.confvrrp_script chk_nginx {
script "pidof nginx" interval 2 }vrrp_instance VI_1 {
state BACKUP interface etch0 #容器内部的网卡名称 virtual_router_id 33 priority 100 #优先级 advert_int 1 autheentication { auth_type PASS auth_pass letmein }virtual_ipaddress {
172.20.128.50 #虚拟路径 }track_script {
chk_nginx } } haproxy.cfgglobal
log 127.0.0.1 local0 maxconn 4096 daemon nbproc 4 defaults log 127.0.0.1 local3 mode http option dontlognull option redispatch retries 2 maxconn 2000 balance roundrobin timeout connect 5000ms timeout client 5000ms timeout server 5000ms frontend main bind *:6301 default_backend webserver backend webserveer server nginx_master 127.20.127.50:80 check inter 2000 rise 2 fall 5 index-master.html<h1>master!</h1>
1 index-slave.html<h1>slave!</h1>
———————————————— 版权声明:本文为CSDN博主「南城.南城」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/m0_49558851/article/details/107786372转载地址:https://blog.csdn.net/sinat_17775997/article/details/116063364 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!