python - 在 nginx 中配置 web.py.. 困惑

标签 python nginx web.py

嗨,我是 nginx 服务器的新手,我已将我的 index.py 文件上传到 /var/www/pyth/index.py ...

我有点困惑,因为在我本地我可以自由奔跑 python index.py 并访问http://127.0.0.1:8080

我想知道如何在 nginx 中执行此操作,我已经运行了 python index.py 但无法访问 mysite.com:8080

这是我在 /etc/nginx/sites-available/default

中的配置
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;`

    #root /usr/share/nginx/html;
    #index index.php index.py index.html index.htm;
    root /var/www/mysite.com;
    index index.php index.py index.html index.htm;

    # Make site accessible from http://localhost/
    server_name mysite.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied reques$
    #location /RequestDenied {
    #       proxy_pass http://127.0.0.1:8080;
    #}

    #error_page 404 /404.html;

    ...

有人知道我的情况吗?任何帮助将不胜感激..提前致谢

最佳答案

您应该设置 uwsgi (或类似),或 proxy_pass 在 Nginx 中。 UWSGI 的选项更好,因为它将使用专为与 Web 服务器一起工作而设计的协议(protocol);尽管设置起来比仅仅通过 nginx 代理所有内容要困难一些。

proxy_pass

web.py 有一个仅用于开发目的的网络服务器,它不应该在生产环境中使用,因为在这种情况下它真的很慢且效率低下,并且使用 proxy_pass如果您打算发布它,这不是一个好主意。

使用 proxy_pass,您可以使 127.0.0.1:8080 服务器保持在线状态,然后在 nginx(同一服务器上)中进行如下设置:

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

proxy_pass选项将所有内容重定向到位于 127.0.0.1:8080 的 web.py 服务器,其他选项 - 重定向有关连接的数据(已连接客户端的 IP 和用于 nginx 端连接的主机)

UWSGI

使用UWSGI,简而言之,就是这样:

1) 安装uwsgi使用发行版的包管理器,或 pip ,或使用 setup.py install .

2) 在 nginx 中,设置一个服务器,将所有内容传递到 UWSGI 服务器: 服务器 { 听80;

    location / {
        include   uwsgi_params;
        uwsgi_pass  127.0.0.1:9000;
    }
}

3) 然后,在您的 web.py 应用程序中(假设它名为 yourappfile.py ),而不是 app.run() ,使用:

app = web.application(urls, globals())
application = app.wsgifunc()

您仍然可以拥有app.run() ,只需确保将其放入 if __name__ == '__main__' 内即可堵塞;并确保application = app.wsgifunc()在外面,所以 UWSGI 可以看到它。

然后启动UWSGI服务器:

uwsgi --http :9090 --wsgi-file yourappfile.py

看看这些手册,它可能对您有帮助:

关于python - 在 nginx 中配置 web.py.. 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337298/

相关文章:

ssl - 如何撤销客户端证书

python - UWSGI + NGINX 502 错误网关

python - 在 web.py 中打开和关闭文件的位置

Python argparse : "Dry run" flag that disable other flags using

python - Apache mod_wsgi 32 位/64 位 Python 兼容性

c# - C# 中的重要空格,如 Python 或 Haskell?

nginx - 如何设置 2 个不同的容器在 Kubernetes 中的 2 个不同的 DNS 名称上运行?

ssl - nginx ingress - 使用完整中间链 CA 证书生成 SSL 证书时出现意外错误 : Invalid certificate

python - 填写 groupby 的缺失日期

python - 如何使用 web.py 显示图像