• nginx+gunicorn


    wsgi接口,使用gunicorn作为server,想在外层加nginx。

    配置了

    proxy_pass   http://127.0.0.1:9008;

    访问报301。

    参考gunicorn 官网配置:

     1 worker_processes 1;
     2 
     3 user nobody nogroup;
     4 pid /tmp/nginx.pid;
     5 error_log /tmp/nginx.error.log;
     6 
     7 events {
     8     worker_connections 1024;
     9     accept_mutex off;
    10 }
    11 
    12 http {
    13     include mime.types;
    14     default_type application/octet-stream;
    15     access_log /tmp/nginx.access.log combined;
    16     sendfile on;
    17 
    18     upstream app_server {
    19         server unix:/tmp/gunicorn.sock fail_timeout=0;
    20         # For a TCP configuration:
    21         # server 192.168.0.7:8000 fail_timeout=0;
    22     }
    23 
    24     server {
    25         listen 80 default;
    26         client_max_body_size 4G;
    27         server_name _;
    28 
    29         keepalive_timeout 5;
    30 
    31         # path for static files
    32         root /path/to/app/current/public;
    33 
    34         location / {
    35             # checks for static file, if not found proxy to app
    36             try_files $uri @proxy_to_app;
    37         }
    38 
    39         location @proxy_to_app {
    40             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    41             proxy_set_header Host $http_host;
    42             proxy_redirect off;
    43 
    44             proxy_pass   http://app_server;
    45         }
    46 
    47         error_page 500 502 503 504 /500.html;
    48         location = /500.html {
    49             root /path/to/app/current/public;
    50         }
    51     }
    52 }

    OK了。

    查找了一下关于下面两句的资料:
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host;

    nginx为了实现反向代理的需求而增加了一个ngx_http_proxy_module模块。其中proxy_set_header指令就是该模块需要读取的配置文件。在这里,所有设置的值的含义和http请求同中的含义完全相同,除了Host外还有X-Forward-For。
    Host的含义是表明请求的主机名,因为nginx作为反向代理使用,而如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败【默认反向代理服务器会向后端真实服务器发送请求,并且请求头中的host字段应为proxy_pass指令设置的服务器】。
    同理,X_Forward_For字段表示该条http请求是有谁发起的?如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。因此,在配置用作反向代理的nginx中一般会增加两条配置,修改http的请求头:
    proxy_set_header Host $http_host;
    proxy_set_header X-Forward-For $remote_addr;

    这里的$http_host和$remote_addr都是nginx的导出变量,可以再配置文件中直接使用。如果Host请求头部没有出现在请求头中,则$http_host值为空,但是$host值为主域名。因此,一般而言,会用$host代替$http_host变量,从而避免http请求中丢失Host头部的情况下Host不被重写的失误。

    参考:

    http://www.cnblogs.com/huangjingzhou/articles/2155892.html

    http://zhidao.baidu.com/link?url=IouBn0tyJttkX0GNUR65nBdiCy8TLY9i--xyqXcjH_FDwIt8hAnQRTpEEkS6VRb4BVpD7IPU7aenEoJpcZmBBq

    发现注释掉两个header,也是可以访问的,不会报错。看来跟header没关系。应该是之前的配置那里写错了。

  • 相关阅读:
    对“一道有趣的题目”的解答
    在Debian中使用Notify
    Debian服务器安装详细流程
    Lighttpd1.4.20源码分析之插件系统(1)plugin结构体和插件接口
    Lighttpd1.4.20源码分析之etag.c(h) HTTP/1.1中的Etag域
    Lighttpd1.4.20源码分析之bitset.c(h) 位集合的使用
    伸展树
    GCC中的弱符号与强符号
    Lighttpd1.4.20源码分析之fdevent系统(3) 使用
    Lighttpd1.4.20源码分析之插件系统(3)PLUGIN_TO_SLOT宏
  • 原文地址:https://www.cnblogs.com/sudawei/p/3345808.html
Copyright © 2020-2023  润新知