为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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值