为Tornado配置Nginx反向代理

简介:

Tornado的确很给力,知乎、Facebook一直在用。不过Tornado也有自己的局限性,比如它就没有实现完整的HTTP协议,甚至一些REST方法都不支持。不过这也难为它了,本来就是一个Web Framework顺便兼职干一点Web Server的事情而已,有就不错了。好在Tornado现在有了好伙伴Nginx,像HTTPS,负载均衡,静态文件这种破事都可以交给Nginx去处理了。

下载

从源代码编译安装Nginx需要处理一些依赖
Nginx官网
PCRE官网
ZLib官网
下载好之后解压至同一根目录下。

编译

 
  1. $ ./configure
  2. --sbin-path=/usr/local/nginx/nginx
  3. --conf-path=/usr/local/nginx/nginx.conf
  4. --pid-path=/usr/local/nginx/nginx.pid
  5. --with-http_ssl_module
  6. --with-pcre=../pcre-8.38
  7. --with-zlib=../zlib-1.2.8
  8. --with-openssl=../openssl-1.0.2g
  9. #上面只是为了好看....
  10. $ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.2g
  11. $ make -j8 && sudo make install

需要注意的是,Mac下和Linux下默认生成的目录是不一样的。
nginx运行一般要求root权限。
nginx命令行参数相当简单,因为功夫全在配置文件里了……。

 
  1. #启动Nginx实例
  2. $ nginx
  3. #使用指定的配置文件启动nginx
  4. $ nginx -c <conf_file>
  5. #向Nginx发出信号
  6. $ nginx -s [stop| quit| reopen| reload]

Mac下的Nginx配置

 
  1. user nobody nobody;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. use kqueue;
  8. }
  9. http {
  10. # Enumerate all the Tornado servers here
  11. upstream frontends {
  12. server 127.0.0.1:8888;
  13. }
  14. include /usr/local/nginx/conf/mime.types;
  15. default_type application/octet-stream;
  16. access_log /var/log/nginx/access.log;
  17. keepalive_timeout 65;
  18. proxy_read_timeout 200;
  19. sendfile on;
  20. tcp_nopush on;
  21. tcp_nodelay on;
  22. gzip on;
  23. gzip_min_length 1000;
  24. gzip_proxied any;
  25. # Only retry if there was a communication error, not a timeout
  26. # on the Tornado server (to avoid propagating "queries of death"
  27. # to all frontends)
  28. proxy_next_upstream error;
  29. server {
  30. listen 80;
  31. # Allow file uploads
  32. client_max_body_size 50M;
  33. location ^~ /static/ {
  34. root /Volumes/Data/vserver;
  35. if ($query_string) {
  36. expires max;
  37. }
  38. }
  39. location ^~ /upload/ {
  40. root /Volumes/Data/vserver;
  41. if ($query_string) {
  42. expires max;
  43. }
  44. }
  45. location = /favicon.ico {
  46. access_log off;
  47. rewrite (.*) /static/other/favicon.ico;
  48. }
  49. location = /robots.txt {
  50. rewrite (.*) /static/other/robots.txt;
  51. }
  52. # Ali heartbeat dectection
  53. location = /status.taobao {
  54. access_log off;
  55. rewrite (.*) /static/other/status.taobao;
  56. }
  57. location / {
  58. proxy_pass_header Server;
  59. proxy_set_header Host $http_host;
  60. proxy_redirect off;
  61. proxy_set_header X-Real-IP $remote_addr;
  62. proxy_set_header X-Scheme $scheme;
  63. proxy_pass http://frontends;
  64. }
  65. }
  66. }

Linux下Nginx的配置

 
  1. # user nobody nobody;
  2. worker_processes 1;
  3. #error_log /var/log/nginx/error.log;
  4. #pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. # Enumerate all the Tornado servers here
  10. upstream frontends {
  11. server 127.0.0.1:8888;
  12. }
  13. include /usr/local/nginx/mime.types;
  14. default_type application/octet-stream;
  15. #access_log /var/log/nginx/access.log;
  16. keepalive_timeout 65;
  17. proxy_read_timeout 200;
  18. sendfile on;
  19. tcp_nopush on;
  20. tcp_nodelay on;
  21. gzip on;
  22. gzip_min_length 1000;
  23. gzip_proxied any;
  24. # Only retry if there was a communication error, not a timeout
  25. # on the Tornado server (to avoid propagating "queries of death"
  26. # to all frontends)
  27. proxy_next_upstream error;
  28. server {
  29. listen 80;
  30. # Allow file uploads
  31. client_max_body_size 50M;
  32. location ^~ /static/ {
  33. root /home/vonng/vserver;
  34. if ($query_string) {
  35. expires max;
  36. }
  37. }
  38. location ^~ /upload/ {
  39. root /home/vonng/vserver;
  40. if ($query_string) {
  41. expires max;
  42. }
  43. }
  44. location = /favicon.ico {
  45. access_log off;
  46. rewrite (.*) /static/other/favicon.ico;
  47. }
  48. location = /robots.txt {
  49. rewrite (.*) /static/other/robots.txt;
  50. }
  51. # Ali heartbeat dectection
  52. location = /status.taobao {
  53. access_log off;
  54. rewrite (.*) /static/other/status.taobao;
  55. }
  56. location / {
  57. proxy_pass_header Server;
  58. proxy_set_header Host $http_host;
  59. proxy_redirect off;
  60. proxy_set_header X-Real-IP $remote_addr;
  61. proxy_set_header X-Scheme $scheme;
  62. proxy_pass http://frontends;
  63. }
  64. }
  65. }
目录
相关文章
|
16天前
|
缓存 前端开发 JavaScript
终极 Nginx 配置指南(全网最详细)
本文详细介绍了Nginx配置文件`nginx.conf`的基本结构及其优化方法。首先通过删除注释简化了原始配置,使其更易理解。接着,文章将`nginx.conf`分为全局块、events块和http块三部分进行详细解析,帮助读者更好地掌握其功能与配置。此外,还介绍了如何通过简单修改实现网站上线,并提供了Nginx的优化技巧,包括解决前端History模式下的404问题、配置反向代理、开启gzip压缩、设置维护页面、在同一IP上部署多个网站以及实现动静分离等。最后,附上了Nginx的基础命令,如安装、启动、重启和关闭等操作,方便读者实践应用。
205 84
终极 Nginx 配置指南(全网最详细)
|
21天前
|
JavaScript Java 应用服务中间件
|
4天前
|
JavaScript 应用服务中间件 开发工具
vue尚品汇商城项目-day07【53.nginx反向代理配置】
vue尚品汇商城项目-day07【53.nginx反向代理配置】
14 4
|
4天前
|
缓存 应用服务中间件 nginx
nginx如何配置?配置项都是什么意思?
nginx如何配置?配置项都是什么意思?
15 1
|
8天前
|
应用服务中间件 nginx Docker
docker应用部署---nginx部署的配置
这篇文章介绍了如何使用Docker部署Nginx服务器,包括搜索和拉取Nginx镜像、创建容器并设置端口映射和目录映射,以及如何创建一个测试页面并使用外部机器访问Nginx服务器。
|
28天前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
63 3
|
28天前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
51 2
|
8天前
|
应用服务中间件 nginx 索引
7-15|Nginx配置
7-15|Nginx配置
|
2月前
|
应用服务中间件 Linux PHP
【Azure 应用服务】App Service For Linux 环境中,如何修改 Nginx 配置中 server_name的默认值 example.com
【Azure 应用服务】App Service For Linux 环境中,如何修改 Nginx 配置中 server_name的默认值 example.com
|
2月前
|
应用服务中间件 Linux nginx
【Azure 应用服务】App Service For Container 配置Nginx,设置/home/site/wwwroot/目录为启动目录,并配置反向代理
【Azure 应用服务】App Service For Container 配置Nginx,设置/home/site/wwwroot/目录为启动目录,并配置反向代理