如何在我的nginx + uwsgi环境中使用environ ['PATH_INFO']?

时间:2014-03-08 08:16:10

标签: python nginx bottle

我在/www/app.py中写了一个简单的瓶子应用程序。

import bottle

app = bottle.Bottle()
@app.route('/')
def index():
    return 'hello from bottle'

application=app

我已将我的nginx虚拟主机配置在名为/ etc / nginx / sites-enabled / foo的文件中:

server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name foo;
    location / {
        uwsgi_pass 127.0.0.1:9090;
    }
}

我以这种方式启动nginx和uwsgi:

service nginx restart
uwsgi --socket 127.0.0.1:9090 --wsgi-file app.py

但是当我尝试访问http://foo/时,我在网页中收到此错误:

Critical error while processing request: /

我在uwsgi输出中得到了这个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/bottle.py", line 954, in wsgi
    out = self._cast(self._handle(environ))
  File "/usr/local/lib/python3.3/dist-packages/bottle.py", line 845, in _handle
    path = environ['bottle.raw_path'] = environ['PATH_INFO']
KeyError: 'PATH_INFO'

我怀疑nginx + uwsgi环境可能没有为我的应用程序提供environ['PATH_INFO']值,所以我编写了一个裸WSGI应用程序来确认它。我将app.py中的代码替换为:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    print('----- begun environ -----')
    for k, v in environ.items():
        print('environ[{}] = {}'.format(k, v))
    print('----- ended environ -----')
    return [b'<p>Hello World</p>']

我确实没有在uwsgi输出中看到PATH_INFO:

----- begun environ -----
environ[uwsgi.version] = b'2.0.2'
environ[HTTP_ACCEPT_ENCODING] = gzip, deflate
environ[HTTP_CACHE_CONTROL] = max-age=0
environ[wsgi.multithread] = False
environ[HTTP_HOST] = foo:8080
environ[wsgi.input] = <uwsgi._Input object at 0x7f990a7019d8>
environ[wsgi.url_scheme] = http
environ[HTTP_USER_AGENT] = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
environ[HTTP_ACCEPT_LANGUAGE] = en-US,en;q=0.5
environ[uwsgi.node] = b'nifty'
environ[wsgi.errors] = <_io.TextIOWrapper name=2 mode='w' encoding='UTF-8'>
environ[wsgi.multiprocess] = False
environ[wsgi.run_once] = False
environ[wsgi.version] = (1, 0)
environ[HTTP_CONNECTION] = keep-alive
environ[HTTP_ACCEPT] = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
environ[wsgi.file_wrapper] = <built-in function uwsgi_sendfile>
----- ended environ -----

我该怎么做才能让我的应用或瓶子应用获得environ['PATH_INFO']

1 个答案:

答案 0 :(得分:5)

根据您使用的服务器平台,应该有uwsgi_params配置文件,用于在/etc/nginx目录中设置这些参数。您可以将它包含在您的nginx配置中,如下所示:

server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name foo;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9090;
    }
}

如果您没有此文件,这是我本地nginx服务器的内容,这可能对您有用:

menno@mimic:/etc/nginx$ nginx -v
nginx version: nginx/1.1.19

menno@mimic:/etc/nginx$ cat uwsgi_params
uwsgi_param     QUERY_STRING            $query_string;
uwsgi_param     REQUEST_METHOD          $request_method;
uwsgi_param     CONTENT_TYPE            $content_type;
uwsgi_param     CONTENT_LENGTH          $content_length;

uwsgi_param     REQUEST_URI             $request_uri;
uwsgi_param     PATH_INFO               $document_uri;
uwsgi_param     DOCUMENT_ROOT           $document_root;
uwsgi_param     SERVER_PROTOCOL         $server_protocol;

uwsgi_param     REMOTE_ADDR             $remote_addr;
uwsgi_param     REMOTE_PORT             $remote_port;
uwsgi_param     SERVER_PORT             $server_port;
uwsgi_param     SERVER_NAME             $server_name;