收起左侧

[Python] 使用Nginx作为Python Web的反向代理实战

0
回复
[复制链接]

450

主题

635

回帖

4万

积分
发表于 2017-8-4 16:40:39 | 显示全部楼层 |阅读模式
使用Nginx作为Python Web的反向代理实战
一、反向代理简介:
在电脑网络中,反向代理是代理服务器的一种。它根据客户端的请求,从后端的服务器(如Web服务器)上获取资源,然后再将这些资源返回给客户端。与前向代理不同,前向代理作为一个媒介将互联网上获取的资源返回给相关联的客户端,而反向代理是在服务器端(如Web服务器)作为代理使用,而不是客户端。
5979e29e00010efd13500750.png
二、反向代理功能:
1.加密和SSL加速
2.负载均衡
3.缓存静态内容
4.压缩
5.减速上传
6.安全
7.外网发布

三、下载编译安装Nginx
1.安装依赖包
  1. yum -y install gcc openssl-devel pcre-devel httpd-tools gcc-c++
复制代码
5979e29e00010efd13500750.png
2.下载Nginx
  1. wget http://nginx.org/download/nginx-1.10.2.tar.gz
复制代码
5979e29e00010efd13500750.png
3.编译安装Nginx
  1. tar xf nginx-1.10.2.tar.gz #解压nginx
  2. cd nginx-1.10.2/ #进入解压目录
  3. useradd nginx #创建用户
  4. ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module #检查配置
复制代码

5979e29e00010efd13500750.png
  1. make && make install #编译并安装
复制代码
5979e29e00010efd13500750.png
  1. ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ #制作快捷方式
  2. nginx #启动服务
  3. netstat -anptu | grep nginx #检查端口
复制代码
5979e29e00010efd13500750.png
浏览器检查:http://192.168.4.1
四、配置Nginx作为Tornado的反向代理
1.配置文件
  1. vim /usr/local/nginx/conf/nginx.conf
复制代码
编辑以下配置:
  1. > worker_processes 1;
  2. > events {
  3. >   use epoll;
  4. >   worker_connections 65535;
  5. > }
  6. > http {
  7. >   include mime.types;
  8. >   default_type application/octet-stream;
  9. >   sendfile on;
  10. >   server_tokens off;
  11. >   keepalive_timeout 10;
  12. >   tcp_nodelay on;
  13. >   gzip on;
  14. >   upstream fkcloud {
  15. >     server 192.168.4.1:8000;
  16. >   }
  17. > server {
  18. >     listen 80;
  19. >     server_name localhost;
  20. >     location / {
  21. >        proxy_pass_header Server;
  22. >        proxy_set_header Host $http_host;
  23. >        proxy_redirect off;
  24. >        proxy_set_header X-Real-IP $remote_addr;
  25. >        proxy_set_header X-Scheme $scheme;
  26. >        proxy_http_version 1.1;
  27. >        proxy_set_header Upgrade $http_upgrade;
  28. >        proxy_set_header Connection "Upgrade";
  29. >        proxy_pass http://fkcloud;
  30. >             }
  31. >     }
  32. > }
复制代码
5979e41a0001b4a615021706.png
2.tornado代码
222(1).jpg

3.刷新配置,启动tornado
  1. > nginx -s reload #重启服务
  2. > nohup python index.py & #启动tornado服务
  3. > netstat -anptu | grep 80 检查80端口
  4. > netstat -anptu | grep 8000 #检测8000端口
复制代码

222(1).png

4.浏览器检测
  1. 浏览器检查:http://192.168.4.1
复制代码
1.png


  1. 浏览器检查:http://192.168.4.1:8000
复制代码
2.png






IT教程吧走一走,高薪迟早有!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则