Nginx的简介和使用nginx实现请求转发

一、什么是Nginx

  1. Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。从2004年发布至今,凭借开源的力量,已经接近成熟与完善。
  2. Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模块扩展。
  3. Nginx的稳定性、功能集、示例配置文件和低系统资源的消耗让他后来居上,在全球活跃的网站中有12.18%的使用比率,大约为2220万个网站。

二、Nginx主要功能

  1. 请求转发:通过路径匹配把用户的请求转发到不同的服务器

  2. 负载均衡:一个服务使用集群部署,用户请求时通过负载均衡算法把用户的请求分摊到不同的服务器上。常见的负载均衡算法有轮询、随机、加权轮询、ip哈希、最小请求时间等。

  3. 动静分离:Nginx是一个http服务,可以独立提供http服务。可以做网页静态服务器。

  4. 虚拟主机:可以实现在一台服务器虚拟出多个网站。

    基于端口的,设置不同的端口

    基于域名的,设置不同域名

三、安装window版的nginx

下载地址:http://nginx.org/en/download.html

解压之后,双击nginx.exe运行nginx。默认监听80端口

访问:localhost

四、Nginx的常用命令

nginx.exe -s stop //停止nginx
nginx.exe -s reload //重新加载nginx
nginx.exe -s quit //退出nginx

五、Nginx的简单使用

1.配置nginx代理,实现请求转发功能

在Nginx中配置对应的微服务服务器地址即可,打开nginx的conf目录下的nginx.conf

    server {
        listen       9001;#开放给用户的访问端口,即nginx的监听端口
        server_name  localhost;#主机名
		# ~表示正则匹配   匹配路径
        location ~ /eduservice/{ 
            proxy_pass http://127.0.0.1:8001;  #转发服务器地址
        }
        location ~ /eduoss/{
            proxy_pass http://127.0.0.1:8002;
        }location ~ /eduvod/{
            proxy_pass http://127.0.0.1:8003;
        }
        location ~ /cmsservice/{
            proxy_pass http://127.0.0.1:8003;
        }
        location ~ /ucenterservice/{
            proxy_pass http://127.0.0.1:8004;
        }
        location ~ /edusms/{
            proxy_pass http://127.0.0.1:8005;
        }
        location ~ /orderservice/{
            proxy_pass http://127.0.0.1:8006;
        }
        location ~ /staservice/{
            proxy_pass http://127.0.0.1:8007;
        }

BwHYuR.png

2、配置完成后保存配置,重启nginx

nginx -s reload