pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • 基于Python轻松自建App服务器

    • 0App 与后端服务器通信方法简介
    • 1本小册要完成的通信场景功能
    • 2服务器端组件框架的选择与介绍
    • 3基于腾讯云的服务器端环境搭建
    • 4基于 Tornado 的 HTTP 服务器简介及代码组织框架
    • 5第一次数据请求 1:服务器接收用户注册信息
    • 6第一次数据请求 2:为用户处理模块增加 log 管理
    • 7第一次数据请求 3:将用户信息写入 MySQL 数据库
    • 8服务器接收客户端图片上传,并保存在硬盘中
    • 9服务器接收客户端请求,并返回 H5 页面
    • 10搭建基于 Nginx 的代理服务器
      • 安装 Nginx
      • 配置随系统自启动
      • 配置 Nginx
      • 其他配置项介绍
      • 重启 Nginx 服务
      • 请求测试
        • 服务端输出
        • 客户端加载结果
      • Nginx 配置下载
      • 小结
    • 11基于 HTTPS 的数据加密
    • 12大型 HTTP 服务器架构演进路线及思路
    • 13总结
  • 基于Python实现微信公众号爬虫

  • Xpath
  • python3 翻译
  • python3循环创建数据库表
  • python实用30个小技巧
  • pywin32
  • Python
  • 基于Python轻松自建App服务器
pursuewind
2020-11-23
目录

10搭建基于 Nginx 的代理服务器

# 搭建基于 Nginx 的代理服务器

Nginx (opens new window) 是俄罗斯人编写的十分轻量级的 HTTP 服务器,是一个高性能的 HTTP 和反向代理服务器。相较于 Apache (opens new window)、lighttpd (opens new window) ,它具有占有内存少、稳定性高等优势。它最常见的用途是提供反向代理服务。

在本小节中,我们将利用 Nginx 的反向代理及负载均衡能力。所谓的负载均衡,是当单台服务器的性能无法满足业务需求时,需横向添加多台服务器;负载均衡就是让访问流量均匀的落在这个服务器集群的每个服务器上。具体逻辑图如下:

App 客户端将请求发送至 Nginx,Nginx 收到请求后,将其转发给后端的服务器集群。在本小册中,我们的 Demo 只有一台虚拟机,现将 Nginx 和后端服务器放在一起,Nginx 架设在 Tornado 之前,其基本框架图如下。

# 安装 Nginx

输入 yum install nginx 安装 Nginx,安装完成后,输入 nginx -v 查看 Nginx 是否安装成功。

这里显示安装已成功,版本号为 1.12.2。

# 配置随系统自启动

配置 Nginx 随系统自启动,即 Linux 系统启动时,Nginx 能自启动,而不是人为干预启动。

chkconfig --levels 235 nginx on
service nginx start
1
2

# 配置 Nginx

进入 /etc/nginx/, 编辑 nginx.conf 文件。

在 nginx.conf 中,增加后端服务器 IP 和端口,由于 Nginx 和服务器在同一台机器上,这里填入:

upstream frontends {
    server 127.0.0.1:8000;
}
1
2
3

当 Nginx 和后端服务器不在同一台机器上,并有多台后端服务器设备时,则配置具体服务器的 IP:端口 即可,Nginx 会负载均衡的将流量均匀分配到这些服务器上。此时配置如下:

upstream frontends {
    server x.x.x.x:nnn;
    server y.y.y.y:mmm;
    server z.z.z.z:lll;
}
1
2
3
4
5

由于 Nginx 对大小超过 1MB 的文件上传有限制,这里将默认限制 1MB 修改为 50MB,即添加配置 client_max_body_size 50m;,具体位置如下图所示。

在 nignx.conf 文件中的 location 下,添加如下代理配置,即所有收到的请求,都转发到 frontends (如上所述的 upstream frontends)处理,具体如下:

    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect false;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://frontends;
1
2
3
4
5
6

具体位置如下:

至此,我们已完成了 Nginx 的配置。

# 其他配置项介绍

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
# worker_processes 一般设置与 cpu 个数相等,也可配置为auto
worker_processes auto;
# 全局错误日志及 pid 目录
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    # 单个后台 worker proces s进程的最大并发链接数 
    worker_connections 1024;
}

http {
    # 设置 log 格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # 配置上游服务器,此处为 Tornado 服务器 IP+Port
    upstream frontends {
        server 127.0.0.1:8000;
        #server 10.10.10.10:8001;
    }

    # 访问日志
    access_log  /var/log/nginx/access.log  main;

    # sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,普通应用,设置为 on 即可;
    # keepalive_timeout 配置超时时间;
    # types_hash_max_size 影响散列表的冲突率。types_hash_max_size 越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size 越小,消耗的内存就越小,但散列key的冲突率可能上升。
    # client_max_body_size 客户端上传的body的最大值。
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 50m; 

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        # 监听端口为 80
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # 默认网站根目录位置
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        # 设置默认请求代理,此处使用frontends,即请求代理至 Tronado 服务器
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect false;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
        
        # 定义 404 错误页
        error_page 404 /404.html;
            location = /40x.html {
        }

        # 定义 50x 错误页
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

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

# 重启 Nginx 服务

service nginx stop
service nginx start
1
2

接下来测试从 App 客户端向 Nginx 服务器发送 HTTP 请求,查看是否能正常转发至后端服务器上。在这里,我们看到 nginx.conf 有如下配置,即表示其对外服务的端口号为 80。实际项目中,可以根据具体情况进行修改。

# 请求测试

我们还是以上一小节 App 客户端请求加载 H5 页面为例。由于 Nginx 对外提供服务的端口号是 80,而 80 端口在 HTTP 请求中可以不用输入,此时客户端请求的 URL 为: http://150.109.33.132/users/login?phone=18866668888&password=demo123456

# 服务端输出

# 客户端加载结果

至此,我们已完成了 Nginx 的学习及服务器端的配置。

# Nginx 配置下载

链接:百度网盘 - nginx.conf (opens new window)
密码:xtmr

# 小结

本节我们完成了 Nginx 服务器的搭建,并通过一个简单的例子,讲解了 Nginx 作为反向代理服务器和负载均衡器的应用。

Last Updated: 2023/02/14, 18:02:00
9服务器接收客户端请求,并返回 H5 页面
11基于 HTTPS 的数据加密

← 9服务器接收客户端请求,并返回 H5 页面 11基于 HTTPS 的数据加密→

Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜
  • 网络DJ