gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:47 25 4
gpt4 key购买 nike

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

import bottle

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

application=app

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

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 -----

我应该怎么做才能让我的应用程序或 Bottle 应用程序获得 environ['PATH_INFO']

最佳答案

根据您使用的服务器平台,应该有 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;

关于python - 如何使 environ ['PATH_INFO' ] 在我的 nginx + uwsgi 环境中可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22266881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com