使用 web 服务器(例如gunicorn )在本地端口上运行 web.py 应用程序,然后配置 nginx 以托管静态文件并反向代理 gunicorn 服务器。以下是一些配置片段,假设:
- 你的项目在
/var/www/example-webpy
- 你的静态文件在
example-webpy/static
- 你的 nginx 配置在
/etc/nginx
.
在应用程序中公开 WSGI 对象
看起来 web.py 默认情况下不执行此操作,因此您需要在您的app.py
(或引导您的应用程序的任何文件)中使用以下内容:
# For serving using any wsgi server
wsgi_app = web.application(urls, globals()).wsgifunc()
此 SO question中的更多信息。
运行您的应用程序服务器
安装 gunicorn 并通过运行以下命令启动应用程序(example
Python 模块的名称在哪里):
gunicorn example:wsgi_app -b localhost:3001
(您可能希望使用Supervisor之类的工具自动执行此操作,以便在您的服务器反弹时重新启动应用程序服务器。)
配置 nginx
将以下内容放入/etc/nginx/reverse-proxy.conf
(请参阅this SO answer)
# Serve / from local http server.
# Just add the following to individual vhost configs:
# proxy_pass http://localhost:3001/;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
然后在中配置您的域/etc/nginx/sites-enabled/example.com.conf
:
server {
server_name example.com
location /test/ {
include /etc/nginx/reverse-proxy.conf;
rewrite /test/(.*) /$1 break;
proxy_pass http://localhost:3001/;
}
location / {
root /var/www/example-webpy/static/;
}
}
请注意重写,这应确保您的 web.py 应用程序永远不会看到 /test/ URL 前缀。请参阅有关proxy_pass和HttpRewriteModule的 nginx 文档。
这将导致请求example.com/js/main.js
映射到example-weby/static/js/main.js
,因此它假定您的 web.py 模板没有添加/static/
前缀。它还会导致static
目录中的所有内容都对网络可见,因此请确保这是您想要的!