Nginx搭建伪CDN服务器

Nginx

Nginx应用场景

1. HTTP 服务器:Nginx可以独立提供 HTTP 服务。可以做网页静态服务器。
2. 虚拟主机:可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
3. 反向代理,负载均衡:当网站的访问量达到一定程度后,需要用多台服务器集群可以使用 Nginx 做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

虚拟主机

1. 虚拟主机是一种特殊的软硬件技术,可以将每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供 www 服务。
2. Nginx 支持三种类型的虚拟主机配置
	1. 基于 IP 的虚拟主机
	2. 基于域名的虚拟主机
	3. 基于端口的虚拟主机
Nginx 搭建
docker-compose.yml
version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./wwwroot:/usr/share/nginx/wwwroot
nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;
    # 配置虚拟主机 192.168.75.145
    server {
	# 监听的ip和端口,配置 192.168.75.145:80
        listen       80;
	# 虚拟主机名称这里配置ip地址
        server_name  nginx.com;
	# 所有的请求都以 / 开始,所有的请求都可以匹配此 location
        location / {
	    # 使用 root 指令指定虚拟主机目录即网页存放目录
	    # 比如访问 http://ip/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/index.html
	    # 比如访问 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html

            root   /usr/share/nginx/wwwroot/html80;
	    # 指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }

    }
    server {
        listen       8080;
        server_name  192.168.79.139;
        location / {
            root   /usr/share/nginx/wwwroot/html8080;
            index  index.html index.htm;
        }
    }
}

代理服务器

代理服务器的好处:
	1. 代理服务器可以缓存数据,提高访问速度。
	2. 防火墙作用
	3. 通过代理服务器访问不能访问的目标站点
正向代理
架设在客户机与目标主机之间,客户机必须指定代理服务器,并将本来要直接发送到 Web 服务器
上的 Http 请求发送到代理服务器中。
反向代理
反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,
此时代理服务器与目标主机一起对外表现为一个服务器。
反向代理的好处:
	现在许多大型 web 网站都用到反向代理。除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  nginx.com;
        location / {
        # 访问nginx.com(没域名可以host配置)会跳转下面的请求
	 		proxy_pass http://192.168.79.139:9000;
	    # 指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }

    }
    server {
        listen       8080;
        server_name  192.168.79.139;
        location / {
            root   /usr/share/nginx/wwwroot/html8080;
            index  index.html index.htm;
        }
    }
}

负载均衡

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    upstream myTomcat {
        server 192.168.79.139:9090 weight=10;
        server 192.168.79.139:9091 weight=10;
    }
    server {
        listen       80;
        server_name  nginx.com;
        location / {
            proxy_pass http://myTomcat;
            index  index.html index.htm;
        }

    }
    server {
        listen 9000;
        server_name  192.168.79.139;
        location / {
            proxy_pass http://192.168.79.139:9091;
            index  index.html index.htm;
        }

    }
}

搭建伪CDN服务器

将静态资源放到cdn目录下  即可直接用URL引入静态资源
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  nginx.com;
        location / {
            root /usr/share/nginx/wwwroot/cdn/;
            index  index.html index.htm;
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值