# 引言

  • 所有配置方法和介绍均来至于网络搜索汇总,主要用于方便后期巩固学习。

# 何为反向代理

  • 在介绍反向代理之前,先来了解一下正向代理。

正向代理:如果把局域网外的 Internet 想象成一个巨大的资源库,则局域网中的客户端要访问 Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理,下面是正向代理的原理图。
991a203be1ec82b36e1f326376617d40.png
反向代理:看下面原理图,就一目了然。其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。
1c87e386a0ce624a3949bda443cf346d.png

正向代理和反向代理的区别,一句话就是:如果我们客户端自己用,就是正向代理。如果是在服务器用,用户无感知,就是反向代理。

# Nginx 配置文件

  • 在学习 Nginx 之前,要熟知它的配置文件,毕竟,下面需要做的所有配置(反向代理、负载均衡、动静分离等),都是基于它的配置文件。

  • Nginx 默认的配置文件是在安装目录下的 conf 目录下,后续对 Nginx 的使用基本上都是对此配置文件进行相应的修改。完整的配置文件,可以看一下文章最后。修改过 nginx.conf 配置文件,记得要✔️重启 Nginx 服务(☆☆☆☆☆)

  • 配置文件中有很多 #号,该符号表示注释内容,去掉所有以 #开头的段落,精简之后的配置文件内容如下(PS:其实注释掉的地方,都是一些功能的使用代码,需要用到的时候,取消注释即可):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    # 主进程叫master,负责管理子进程,子进程叫worker
    # worker_processes配置项表示开启几个业务进程,一般和cpu核数有关
    worker_processes 1;

    events {
    worker_connections 1024;
    }

    http {
    # include表示可以引入其他文件,此处表示引入http mime类型
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    # 虚拟主机,可以配置多个
    server {
    listen 80;
    server_name localhost;

    location / {
    # 路径匹配之后,哪个目录下去匹配相应的网页,html是相对路径
    root html;
    index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }
    }

去掉注释信息后,可以将 nginx.conf 配置文件分为三部分:

  1. 全局块
    1
    worker_processes  1;
  • 从配置文件开始到 events 块之间的内容,主要会设置一些影响 Nginx 服务器整体运行的配置指令,主要包括:配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

  • 上面这行 worker_processes 配置,是 Nginx 服务器并发处理服务的关键配置,该值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的约束。

  1. events 块
    1
    2
    3
    events {
    worker_connections 1024;
    }
  • events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括:是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 work process 可以同时支持的最大连接数等

上述例子就表示每个 work process 支持的最大连接数为 1024。这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。

  1. http 块
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
    listen 80;
    server_name localhost;

    location / {
    root html;
    index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }
  • 这部分是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http 块也可以包括 http 全局块、server 块。下面的反向代理、动静分离、负载均衡都是在这部分中配置

http 全局块:http 全局块配置的指令包括:文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。

server 块:这块和虚拟主机有密切关系,从用户角度看,虚拟主机和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

location 块:这块的主要作用是:基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。(☆☆☆☆☆)

# 反向代理配置

  1. location 配置规则

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    location [ = | ~ | ~* | ^~ | @ ] /uri {

    }

    =   :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
    ~ :用于表示 uri 包含正则表达式,并且区分大小写。
    ~* :用于表示 uri 包含正则表达式,并且不区分大小写。
    ^~ :用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的location后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
    @ : "@" 定义一个命名的 location,使用在内部定向时,例如 error_page
    /uri :不带任何修饰符,也表示前缀匹配,但是在正则匹配之后,如果没有正则命中,命中最长的规则
    / :通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

    uri 没有 “/” 结尾时location /abc/def 可以匹配 /abc/defghi 请求,也可以匹配 /abc/def/ghi 等
    uri 有 “/” 结尾时location /abc/def/ 不能匹配 /abc/defghi 请求只能匹配 /abc/def/anything 这样的请求☆☆☆☆☆

  2. proxy_pass 配置规则

url 结尾加上了 /相当于是绝对路径则 Nginx 不会把 location 中匹配的路径部分加入代理 url
url 结尾不加 /Nginx 则会把匹配的路径部分加入代理 uri

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
情景1:
proxy_pass后有/
访问地址:http://localhost:8081/WCP.Service/wcp/modeladapter/download/asc.shtml
最终代理:http://10.194.171.7:13082/modeladapter/download/asc.shtml
location /WCP.Service/wcp/modeladapter/download/ {
proxy_pass http://10.194.171.7:13082/modeladapter/download/;
}
访问地址:http://localhost:8081/model/asc.shtml
最终代理:http://127.0.0.1:8082/model/asc.shtml
location /model/ {
proxy_pass http://127.0.0.1:8082/model/;
}

情景2:
proxy_pass后有/
访问地址:http://localhost:8081/model/asc.shtml
最终代理:http://127.0.0.1:8082/asc.shtml
location /model/ {
proxy_pass http://127.0.0.1:8082/;
}

情景3:
proxy_pass后没有/
访问地址:http://localhost:8081/model/asc.shtml
最终代理:http://127.0.0.1:8082/model/asc.shtml
location /model/ {
proxy_pass http://127.0.0.1:8082;
}

情景4
proxy_pass后没有/
访问地址:http://localhost:8081/model/asc.shtml
最终代理:http://127.0.0.1:8082/AAAmodel/asc.shtml
location /model/ {
proxy_pass http://127.0.0.1:8082/AAA;
}

情景5
proxy_pass后有/
访问地址:http://localhost:8081/model/asc.shtml
最终代理:http://127.0.0.1:8082/asc.shtml
location /model {
proxy_pass http://127.0.0.1:8082/;
}

情景6
proxy_pass后有/
访问地址:http://localhost:8081/modelBBB/asc.shtml
最终代理:http://127.0.0.1:8082/asc.shtml
location /model {
proxy_pass http://127.0.0.1:8082/;
}

# Nginx 完整配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index 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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
}

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Hito Li 微信支付

微信支付

Hito Li 支付宝

支付宝