Skip to main content
 首页 » 编程设计

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

2024年10月25日34xiaohuochai

您好,我是 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 代理所有内容要难一些。

代理通行证

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) 使用发行版的包管理器或 pip 或使用 setup.py install 安装 uwsgi。

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__' block 中即可;并确保 application = app.wsgifunc() 在外面以便 UWSGI 可以看到它。

然后启动一个UWSGI服务器:

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

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