nginxNginx

Nginx 基础笔记

2021-07-17  本文已影响0人  yjtuuige

一、Nginx介绍

1.1引言

1.2 Nginx介绍

Nginx是由俄罗斯人研发的,应对 Rambler 的网站并发,并且2004年发布的第一个版本
Nginx 的特点

  1. 稳定性极强,7*24小时不间断运行 (一直运行)
  2. Nginx 提供了非常丰富的配置实例
  3. 占用内存小,并发能力强 (随便配置一下就是5w+,而 tomcat 的默认线程池是150)

二、Nginx的安装

2.1安装 Nginx

[root@VM-0-6-centos home]# mkdir docker_nginx
version: '3.9'
services: 
  nginx:
    restart: always
    image: nginx:latest    // 拉取镜像
    container_name: nginx    // 设置容器名称
    ports: 
      - 80:80
[root@VM-0-6-centos docker_nginx]# docker-compose up -d
Starting nginx ... done

2.2 Nginx的配置文件

[root@VM-0-6-centos docker_nginx]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
24aa71edacb5   nginx:latest   "/docker-entrypoint.…"   9 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
[root@VM-0-6-centos docker_nginx]# docker exec -it 24a bash
root@24aa71edacb5:/# 
root@24aa71edacb5:/# cd /etc/nginx/       // nginx 配置文件目录
root@24aa71edacb5:/etc/nginx# cat nginx.conf
user  nginx;
worker_processes  auto;    // 数值越大,Nginx的并发能力就越强

error_log  /var/log/nginx/error.log notice;    // Nginx错误日志存放的位置
pid        /var/run/nginx.pid;    // Nginx运行的一个标识

# 以上统称为全局块

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

http {
    include       /etc/nginx/mime.types;    // 引入一个外部文件,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;    // 引入了 conf.d 目录下,以 .conf 为结尾的配置文件
}
# http块
# server块
server {
    listen       80;        // Nginx监听的端口号
    listen  [::]:80;
    server_name  localhost;        // Nginx接受请求的IP

    #access_log  /var/log/nginx/host.access.log  main;
    
    # location块
    location / {
        root   /usr/share/nginx/html;    // 将接受到的请求根据/usr/share/nginx/html去查找静态资源
        index  index.html index.htm;    // 默认去上述的路径中找到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.3 修改 docker-compose 文件

root@24aa71edacb5:/etc/nginx/conf.d# exit    // 退出容器
exit
[root@VM-0-6-centos docker_nginx]# docker-compose down    // 停止 docker-compose
Stopping nginx ... done
Removing nginx ... done
Removing network docker_nginx_default
version: '3.9'
services: 
  nginx:
    restart: always
    image: nginx:latest
    container_name: nginx
    ports: 
      - 80:80
    volumes:
      - /home/docker_nginx/conf.d/:/etc/nginx/conf.d    // 挂载卷
[root@VM-0-6-centos docker_nginx]# docker-compose build
nginx uses an image, skipping
[root@VM-0-6-centos docker_nginx]# docker-compose up -d
Creating network "docker_nginx_default" with the default driver
Creating nginx ... done
[root@VM-0-6-centos docker_nginx]# ls
conf.d  docker-compose.yml
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
[root@VM-0-6-centos conf.d]# docker-compose restart
Restarting nginx ... done

三、Nginx的反向代理

3.1正向代理和反向代理介绍

3.2 基于Nginx实现反向代理

docker run -d -p 8080:8080 --name tomcat  tomcat
#或者已经下载了tomcat镜像
docker run -d -p 8080:8080 --name tomcat 镜像的标识
#添加数据卷
docker run -it -v /宿主机绝对目录:/容器内目录 镜像名
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    # 基于反向代理访问到 tomcat 服务器
    location / {
        proxy_pass http://ip:8080/;        // tomcat服务器的IP和端口
    }
}
[root@VM-0-6-centos docker_nginx]# docker-compose restart
Restarting nginx ... done

3.3 关于 Nginx 的 location 路径映射

  1. 精准匹配 = /
location = / {
    # 精准匹配,主机名后面不能带任何字符串
    # 例如www.baidu.com不能是www.baidu.com/id=xxx
}
  1. 通用匹配
location /xxx {
    # 匹配所有以/xxx开头的路径
    # 例如127.0.0.1:8080/xxx  xxx可以为空,为空则和=匹配一样
}
  1. 正则匹配
location ~ /xxx {
    # 匹配所有以 /xxx 开头的路径
}
  1. 匹配开头路径
location ^~ /xxx/xx {
    # 匹配所有以/xxx/xx开头的路径
}
  1. 匹配结尾路径
location ~* \.(gif/jpg/png)$ {
    # 匹配以 .gif、.jpg 或者 .png 结尾的路径
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location /index {
        proxy_pass http://ip:8081/;   # tomcat 首页
    }

    location ^~ /ceshi/ {
        proxy_pass http://ip:8080/ceshi/;     # 测试项目前台首页
    }

    location / {
        proxy_pass http://ip:8080/Admin/;     # 测试项目后台首页
    }
}

[root@VM-0-6-centos docker_nginx]# docker-compose restart
Restarting nginx ... done

四、Nginx 负载均衡

4.1 轮询

upstream my-server{       # 轮询方式:my-server 为自定义名称(名称不能用下划线)
    server ip:port;    # 服务器地址1
    server ip:port;    # 服务器地址2
    ......
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        proxy_pass http://my-server/;         # upstream的名称
    }
}
[root@VM-0-6-centos docker_nginx]# docker-compose restart
Restarting nginx ... done

4.2 权重

upstream my_server{
    server ip:8080 weight=10;        #  weight 权重 10
    server ip:8081 weight=2;          #  weight 权重 2
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        proxy_pass http://my_server/;   # tomcat首页
    }
}

4.3 ip_hash

upstream my_server{
    ip_hash;        #  ip地址不变,始终访问同一台服务器
    server ip:8080;
    server ip:8081;
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        proxy_pass http://my_server/;   # tomcat首页
    }
}

五、Nginx 动静分离

5.1 动态资源代理

location / {
  proxy_pass 路径;
}

5.2 静态资源代理

location / {
  # root  静态资源路径;
  root   /usr/share/nginx/html;        
  # index  默认访问路径下是什么资源;
  index  index.html index.htm;
  autoindex  on;        # 以列表的形式,展示静态资源的全部内容
}
[root@VM-0-6-centos docker_nginx]# docker-compose down
Stopping nginx ... done
Removing nginx ... done
Removing network docker_nginx_default
version: '3.9'
services:
  nginx:
    restart: always
    image: nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /home/docker_nginx/conf.d/:/etc/nginx/conf.d
      - /home/docker_nginx/html/:/usr/share/nginx/html        # 添加挂载静态页面数据卷
[root@VM-0-6-centos docker_nginx]# docker-compose up -d
Creating network "docker_nginx_default" with the default driver
Creating nginx ... done
location /html {        # 此处有 /html,后面可不用写 /html,会直接访问到此目录下
    root /usr/share/nginx;        
    index index.html;
}
[root@VM-0-6-centos docker_nginx]# docker-compose restart
Restarting nginx ... done

六、Nginx 集群

6.1 引言

6.2 搭建

FROM nginx:1.13.5-alpine
RUN apk update && apk upgrade
RUN apk add --no-cache bash curl ipvsadm iproute2 openrc keepalived    # 用apk 安装 keepalived
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]        # 运行 entrypoint.sh 文件
#!/bin/sh
/usr/sbin/keepalived -D -f /etc/keepalived/keepalived.conf        # 运行 keepalived
nginx -g "daemon off;"        # 运行 nginx
version: "3.9"
services:
  nginx_master:        # 主机设置
    build:
      context: ./
      dockerfile: ./Dockerfile
    ports:
      - 8081:80
    volumes:
      - ./index-master.html:/usr/share/nginx/html/index.html
      - ./favicon.ico:/usr/share/nginx/html/favicon.ico
      - ./keepalived-master.conf:/etc/keepalived/keepalived.conf
    networks:
      static-network:        # 固定 IP 地址
        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/nginx/html/index.html
      - ./favicon.ico:/usr/share/nginx/html/favicon.ico
      - ./keepalived-slave.conf:/etc/keepalived/keepalived.conf
    networks:
      static-network:
        ipv4_address: 172.20.128.3
    cap_add:
      - NET_ADMIN
  proxy:
    image: haproxy:1.7-alpine
    ports:
      - 80:6301
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    networks:
      - static-network

networks:
  static-network:
    ipam:
      config:
        - subnet: 172.20.0.0/16
vrrp_script chk_nginx {
    script "pidof nginx"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER        # 主机
    interface eth0        # 容器内部的网卡名称
    virtual_router_id 33
    priority 200        # 优先级
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass letmein
    }

    virtual_ipaddress {
        172.20.128.50        # 虚拟地址,连接 haproxy
    }

    track_script {
        chk_nginx
    }
}
vrrp_script chk_nginx {
    script "pidof nginx"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP        # 从机
    interface eth0        # 容器内部的网卡名称
    virtual_router_id 33
    priority 100        # 优先级,要低于主机
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass letmein
    }

    virtual_ipaddress {
        172.20.128.50        # 虚拟地址,连接 haproxy
    }

    track_script {
        chk_nginx
    }
}
global
    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 webserver
    server ngxin_master 172.20.128.50:80 check inter 2000 rise 2 fall 5
[root@VM-0-6-centos docker_nginx_cluster]# docker-compose up -d

因为我们设置了 8081 端口的 master 优先级为 200,比 8082 端口的 slave 优先级 100 高,所以我们访问 80 端口,看到的是 master

[root@VM-0-6-centos docker_nginx_cluster]# docker stop docker_nginx_cluster_nginx_master_1
docker_nginx_cluster_nginx_master_1
[root@VM-0-6-centos docker_nginx_cluster]# docker start docker_nginx_cluster_nginx_master_1
docker_nginx_cluster_nginx_master_1
上一篇下一篇

猜你喜欢

热点阅读