Nginx正向、反向代理与vpn

2023-06-3020:54:23服务器及运维Comments1,157 views字数 1390阅读模式

什么是Nginx?

Nginx是俄罗斯程序员Igor Sysoev开发的轻量级Http服务器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

Nginx有什么用

正向代理、反向代理、web服务器、负载均衡器、邮件服务器等文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

正向代理与vpn

客户端安装正向代理软件,客户端发送请求到代理服务器,再由代理服务器去访问目标网站,隐藏自己真实的ip。∑(●ΦДΦ●)这个操作是不是很眼熟,没错这就是你们用的vpn。大天朝的那个墙啊,墙内的人想出去,墙外的人想进来。防火墙之父,曾经开通新浪微博,结果3小时内,上万人留言过来骂(●`Д´●)ツ┏━┓,直接把他的微博骂关闭了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

Nginx正向、反向代理与vpn
正向代理图

反向代理

客户端发送请求到反向代理服务器,反向代理服务器再将请求转发给内部网络的服务器,隐藏的是内部服务器的ip。这篇文章主要讲反向代理的配置,啥,你说为什么不讲正向服务器,大家都很感兴趣!因为我还不想进局子里喝茶,_(:зゝ∠)_文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

Nginx正向、反向代理与vpn
反向代理

Nginx的Location匹配规则

在Nginx安装目录conf文件夹下,有nginx.conf文件,下面来讲解一下nginx.conf文件中的反向代理Location配置规则文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

Location配置规则分为四个级别:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

1.最优先:完全匹配 =/xxx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

2.第二优先级:^~ /xxx,代表以xxx开头的路径文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

3.第三优先级:正则表达式,如 ~ /\w 代表匹配数字字母下划线文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

4.最弱优先级别:/ ,任意以/开头的路径文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

    server {
        listen       80;
        server_name  localhost;
        
        // 最优先级
        location =/first {
            proxy_pass http://127.0.0.1:8081 ;
        }
        //第二优先级
        location ^~ /first2 {
            proxy_pass http://127.0.0.1:8082 ;
        }
        //第三优先级
        location ~ /[a-z] {
            proxy_pass http://127.0.0.1:8083 ;
        }
        location ~ /\w {
            proxy_pass http://127.0.0.1:8084 ;
        }
        //最弱的优先级
        location / {
            proxy_pass http://127.0.0.1:8080 ;
        }

按照上面的Location匹配规则,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

如果输入http://localhost/first,因为精准匹配,所以会转到127.0.0.1:8081/first文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

如果输入http://localhost/first2 或者 http://localhost/first2123 ,会转到127.0.0.1:8082/first2或者127.0.0.1:8082/first212文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

如果输入http://localhost/second,会转到127.0.0.1:8083/second ,因为匹配规则级别相同的情况下,以匹配程度最高的为准,但是匹配程度相同情况下,以写在最上面的那一条规则为准。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

如果你想要反向代理的时候,http://localhost/first直接转成proxy_pass 127.0.0.1:8081,不向后面追加/first,那么可以采用以下方式进行配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html

    server {
        listen       80;
        server_name  localhost;
        
        // 最优先级
        location =/first/ {
            proxy_pass http://127.0.0.1:8081/;
        }
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/48970.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/48970.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定