⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 blog.csdn.net/zxd1435513775/article/details/102508463 「止步前行」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

一、引言

其他话不多说,下面来学习一下 Nginx 的反向代理。需要有 Linux 和 Nginx 环境。

二、何为反向代理?

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

正向代理: 如果把局域网外的 Internet 想象成一个巨大的资源库,则局域网中的客户端要访问 Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理。下面是正向代理的原理图。

由于工作环境原因,日常工作只能局限于单位的局域网,如果想要访问互联网,怎么办呢?这就需要用到正向代理。本人经常用正向代理来进行上网。

反向代理: 看下面原理图,就一目了然。其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。

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

这里有个问题:反向代理服务器,怎么选择挂在它后面的哪一台具体服务器呢?答案在后文揭晓,这就是负载均衡。

三、Nginx配置文件

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

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

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

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 localhost;

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

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

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

第一部分:全局块

worker_processes  1;

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

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

第二部分:events 块

events {
worker_connections 1024;
}

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

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

第三部分:http 块

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 块

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

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

1、全局 server 块

最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。

2、location 块

一个 server 块可以配置多个 location 块。

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

四、反向代理如何配置

1、反向代理实例一

实现效果:使用 Nginx 反向代理,访问 www.123.com 直接跳转到 127.0.0.1:8080。

注意:此处如果要想从www.123.com跳转到本机指定的ip,需要修改本机的hosts文件。此处略过

配置代码

server {
listen 80;
server_name 192.168.17.129;

location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080
}
}

如上配置,我们监听 80 端口,访问域名为 www.123.com(不加端口号时默认为 80 端口),故访问该域名时会跳转到 127.0.0.1:8080 路径上。

此处的意思为:nginx 反向代理服务监听 192.168.17.129的80端口,如果有请求过来,则转到proxy_pass配置的对应服务器上,仅此而已。

实验结果:

2、反向代理实例二

实现效果:使用 Nginx 反向代理,根据访问的路径跳转到不同端口的服务中,Nginx 监听端口为 9001。

  • 访问 http://127.0.0.1:9001/edu/ 直接跳转到 127.0.0.1:8081
  • 访问 http://127.0.0.1:9001/vod/ 直接跳转到 127.0.0.1:8082

第一步,需要准备两个 tomcat,一个 8001 端口,一个 8002 端口,并准备好测试的页面

第二步,修改 nginx 的配置文件,在 http 块中配置 server

server {
listen 9001;
server_name 192.168.17.129;

location ~ /edu/ {
proxy_pass http://127.0.0.1:8080
}

location ~ /vod/ {
proxy_pass http://127.0.0.1:8081
}
}

根据上面的配置,当请求到达 Nginx 反向代理服务器时,会根据请求进行分发到不同的服务上。

实验结果:

补充:location 指令说明

该指令用于匹配 URL, 语法如下:

location [ = | ~ | ~* | ^~] uri {

}

  • = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配, 如果匹配成功,就停止继续向下搜索并立即处理该请求。
  • ~:用于表示 uri 包含正则表达式,并且区分大小写。
  • ~*:用于表示 uri 包含正则表达式,并且不区分大小写。
  • ^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求

字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location块中的正则 uri 和请求字符串做匹配。

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识

Nginx完整配置文件

#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;
# }
#}

}

文章目录
  1. 1. 一、引言
  2. 2. 二、何为反向代理?
  3. 3. 三、Nginx配置文件
    1. 3.1. 第一部分:全局块
    2. 3.2. 第二部分:events 块
    3. 3.3. 第三部分:http 块
      1. 3.3.1. 1、全局 server 块
      2. 3.3.2. 2、location 块
  4. 4. 四、反向代理如何配置
    1. 4.1. 1、反向代理实例一
    2. 4.2. 2、反向代理实例二