1

我正在使用 web.py 和 nginx 开发一个网站,到目前为止,我一直在使用内置的开发服务器在本地工作。现在是时候将站点转移到实时服务器了。我想部署该站点,以便根目录类似于examples.com/test但我所有的 url 处理内容都已损坏。我曾以为我可以创建一个url_prefix变量并将其添加到 web.py 代码中,但这确实看起来很脏。似乎最好的办法是让 nginx 从 url 中删除前缀,以便 web.py 应用程序永远不会看到它,但我不确定它是否可能。

有谁知道如何处理这种情况?

4

1 回答 1

2

使用 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 并通过运行以下命令启动应用程序(examplePython 模块的名称在哪里):

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_passHttpRewriteModule的 nginx 文档。

这将导致请求example.com/js/main.js映射到example-weby/static/js/main.js,因此它假定您的 web.py 模板没有添加/static/前缀。它还会导致static目录中的所有内容都对网络可见,因此请确保这是您想要的!

于 2013-09-02T06:04:00.310 回答