nginx的简单使用

Nginx

一. 正向代理与反向代理

正向代理: 1.客户端需要设置; 2. 站在目标服务器的角度, 不知道真正的客户端是谁.

反向代理: 1.客户端不需要设置; 2.站在客户端的角度, 我们不知道实际的目标服务器是谁.

二. nginx的启动、关闭、热启动

启动,进入到nginx的家目录,打开dos命令行,执行如下命令:

nginx

关闭, 进入到nginx的家目录,打开dos命令行,执行如下命令:

nginx -s stop

热启动,进入到nginx的家目录,打开dos命令行,执行如下命令:

nginx -s reload

三. nginx作为静态资源服务器来使用

nginx默认的访问路径是: $NGINX_HOME/html

我们可以在这个目录下放置静态的资源,然后通过 uri 的方式来访问,例如:

  1. http://localhost/vedio.mp4
  2. http://localhost/4.png
  3. http://localhost/test/4.png
3.1 修改nginx的默认访问路径

nginx的所有的配置在 $NGINX_HOME/conf/nginx.conf 这个文件中,在第44行代码可以配置默认的访问路径:

在这里插入图片描述

也可以调整到其他的路径下,如下所示:

在这里插入图片描述

四. nginx作为反向代理服务器

4.1 简单的反向代理

需要修改 location, 将其内容代码块全部注释,加上如下代码:

 location / {
     #root   D:/html6;
     #index  index.html index.htm;
     # 当用户访问 http://localhost 的时候,会将我们我们请求转发到  http://localhost:8081
     proxy_pass http://localhost:8081;
 }

在这里插入图片描述

4.2 集群代理

前序:在 idea 中 springboot的启动类中,反键 Edit 'App', 将 Allow parallel run 勾上。然后启动两个服务器(端口不能相同)。

修改 nginx.conf 文件,配置如下:

    upstream cluster {
        server localhost:8081;
        server localhost:8082;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   D:/html6;
            #index  index.html index.htm;
            # 当用户访问 http://localhost 的时候,会将我们我们请求转发到 tomcat_cluster 这个集群中
            proxy_pass http://cluster;
        }
        // 其他配置省略
     }
4.2.1 轮询

nginx默认按照轮询的方式来访问服务器,目前通过测试可以看出,每个轮询两次。

4.2.2 weight

在一些实际的情况下,集群中有些服务器性能好,有些差,那么我们不可能让所有的服务器平分请求,而是让性能好的机器处理多一些请求,那么可以通过权重来控制:

upstream cluster {
        server localhost:8081 weight=8;
        server localhost:8082 weight=2;
}
4.2.3 ip_hash

ip_hash是nginx的一种负载的策略,它将特定的用户的每一次请求转发到一个特定的服务器上。ip_hash底层的原理是基于 一致性hash算法

upstream cluster {
    	ip_hash;
        server localhost:8081 weight=8;
        server localhost:8082 weight=2;
}
  location / {
            #root   D:/html6;
            #index  index.html index.htm;
            # 当用户访问 http://localhost 的时候,会将我们我们请求转发到 tomcat_cluster 这个集群中
            proxy_pass http://cluster;
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值