【问题标题】:headers missing in HTTP response with Nginx+uWsgi+Web.py使用 Nginx+uWsgi+Web.py 的 HTTP 响应中缺少标头
【发布时间】:2013-11-07 00:29:10
【问题描述】:

所有,

我的环境是 Nginx+uWsgi+Web.py。我得到了以下 HTTP 响应:

   Connection   keep-alive
   Date Mon, 28 Oct 2013 09:02:50 GMT
   Server   nginx
   Transfer-Encoding    chunked

但是,响应应该是 Apache+mod_wsgi 产生的这样的:

   Connection   Keep-Alive
   Content-Encoding gzip
   Content-Length   26225
   Content-Type text/html; charset=UTF-8
   Date Mon, 28 Oct 2013 08:08:33 GMT
   Keep-Alive   timeout=15, max=100
   Server   Apache
   Vary *

因此,缺少很多标题。想不通....

我的 nginx 配置如下:

http {
    charset utf-8;

    # Set the mime-types via the mime.types external file
    include mime.types;

    # And the fallback mime-type
    default_type application/octet-stream;
    #default_type text/html;

    # Click tracking!
    access_log /var/log/nginx/access.log;

    # Hide nginx version
    server_tokens off;

    # ~2 seconds is often enough for HTML/CSS, but connections in
    # Nginx are cheap, so generally it's safe to increase it
    keepalive_timeout 20;

    # You usually want to serve static files with Nginx
    sendfile on;

    tcp_nopush on; # off may be better for Comet/long-poll stuff
    tcp_nodelay off; # on may be better for Comet/long-poll stuff

    server_name_in_redirect off;
    types_hash_max_size 2048;

    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_min_length 512;
    gzip_buffers 4 8k;
    gzip_proxied any;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon;

    # This should be turned on if you are going to have pre-compressed copies (.gz) of
    # static files available. If not it should be left off as it will cause extra I/O
    # for the check. It would be better to enable this in a location {} block for
    # a specific directory:
    # gzip_static on;

    gzip_disable "msie6";
    gzip_vary on;

    # Upstream to abstract backend connection(s) for PHP
    upstream php {
        server unix:/run/php-fpm/php-fpm.sock;
    }
    server {
        ## listen for ipv4; this line is default and implied
        listen 80 default;
        ## listen for ipv6
        #listen [::]:80 default ipv6only=on; 

        # Make site accessible from http://localhost/
        server_name localhost;
        server_name_in_redirect off;
        charset utf-8;
        access_log  /log/access.log;
        error_log   /log/error.log debug;
        root /home/www;
        index index.html index.htm index.php;
        location ~ \.php$ { .... }

        location / {
            uwsgi_pass unix:/tmp/uwsgi.sock;
            include uwsgi_params;
        }

}

而我的 uWsgi 配置是:

[uwsgi]
plugins = python2
chdir = /home/pycode
module = code 
processes = 2
max-requests = 5000
chmod-socket = 666
master = True
vacuum = True
disable-logging = True
enable-threads = True
socket = /tmp/uwsgi.sock
pidfile = /run/uwsgi.pid
workers = 4
reload-mercy = 8
uid = http
gid = http

web.py 上的 python 代码使用 Jinja2 模板,没有任何特殊,在 Apache+mod_wsgi 中运行良好。

顺便说一句,在我的情况下,nginx+php-fpm 会产生正确的响应。所以我想问题出在 uwsgi 配置上。

有什么帮助吗?

提前感谢。

【问题讨论】:

  • 不清楚应用程序是否无法运行,或者您只是想要 gzip 压缩(您粘贴的日志中没有发生某些事情)

标签: python nginx web.py uwsgi


【解决方案1】:

由于您缺少的标头是由网络服务器添加的,因此没有缺少的标头。除非配置,否则每个配置(httpd+modwsgi 和 nginx+uwsgi)默认发送一些标头。

【讨论】:

  • 好吧,我想我问错了问题。所以 httpd+mod_wsgi 添加了比 nginx+uwsgi 更多的默认头。那么问题来了,我该如何更改nginx+uwsgi的header添加配置,例如,我什至在uwsgi文档中都找不到添加content-type header的命令?
  • 我也注意到了缺少的content-type。这通常是 wsgi 框架的责任(在您的情况下为 web.py)(apache 可能添加了默认文本/html)。我没用过所以真的不知道,抱歉。 :(
【解决方案2】:

我遇到了这个问题,这是因为缺少的标头的值没有被引用。一个纯 curl 请求显示了标题。而 Python 请求,Charles 和 httpie 没有显示带有未引用值的标头(在我的情况下,它是一个 ETag 标头)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2016-05-09
    • 1970-01-01
    • 2016-10-15
    • 2018-04-25
    • 2018-07-02
    相关资源
    最近更新 更多