本文介绍: 简而言之,Waitress一个专注于运行 Python Web 应用程序服务器,而 Nginx一个通用的高性能 Web 服务器,适用于托管各种类型的 Web 内容服务。在一些场景中,你可能会将 Waitress 与 Nginx 结合使用,其中 Nginx 充当反向代理服务器,而 Waitress 处理 Python Web 应用程序请求。这与我的开发环境生产环境有关,我使用环境皆是windows,而当我试图在windows安装。如果您的 Flask 应用程序在名为文件定义

1.首先介绍一下waitressnginx是啥,

  1. Waitress:

  2. Nginx:

简而言之,Waitress一个专注于运行 Python Web 应用程序的服务器,而 Nginx一个通用的高性能 Web 服务器,适用于托管各种类型的 Web 内容服务。在一些场景中,你可能会将 Waitress 与 Nginx 结合使用,其中 Nginx 充当反向代理服务器,而 Waitress 处理 Python Web 应用程序的请求。这样的组合可以充分利用 Nginx 的高性能和 Waitress专业性。

2.为什么使用Waitress而不是其他的服务器例如uWSGI,gunicorn

这与我的开发环境生产环境有关,我使用的环境皆是windows,而当我试图在windows上安装uWSGI,gunicorn出现错误修改最后还是放弃了这个方案

uWSGI的错误出现在安装它的时候

C:UsersAdministrator>pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/si
mple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting uwsgi
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/79/73/b5def500729e134d
1cb8dfc334e27fa2e9cfd4e639c1f60c6532d40edaed/uwsgi-2.0.23.tar.gz (810 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [19 lines of output]
      Traceback (most recent call last):
        File "C:UsersAdministratorAppDataLocalProgramsPythonPython39lib
site-packagespip_vendorpyproject_hooks_in_process_in_process.py", line 353,
 in <module>
          main()
        File "C:UsersAdministratorAppDataLocalProgramsPythonPython39lib
site-packagespip_vendorpyproject_hooks_in_process_in_process.py", line 335,
 in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "C:UsersAdministratorAppDataLocalProgramsPythonPython39lib
site-packagespip_vendorpyproject_hooks_in_process_in_process.py", line 118,
 in get_requires_for_build_wheel
          return hook(config_settings)
        File "C:UsersAdministratorAppDataLocalTemp2pip-build-env-m26e9hf4
overlayLibsite-packagessetuptoolsbuild_meta.py", line 325, in get_requires_
for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'
])
        File "C:UsersAdministratorAppDataLocalTemp2pip-build-env-m26e9hf4
overlayLibsite-packagessetuptoolsbuild_meta.py", line 295, in _get_build_re
quires
          self.run_setup()
        File "C:UsersAdministratorAppDataLocalTemp2pip-build-env-m26e9hf4
overlayLibsite-packagessetuptoolsbuild_meta.py", line 480, in run_setup
          super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_scri
pt)
        File "C:UsersAdministratorAppDataLocalTemp2pip-build-env-m26e9hf4
overlayLibsite-packagessetuptoolsbuild_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 3, in <module>
        File "C:UsersAdministratorAppDataLocalTemp2pip-install-pg6_kfalu
wsgi_05f18753100a4e25a475b36504fbabe5uwsgiconfig.py", line 8, in <module>
          uwsgi_os = os.uname()[0]
      AttributeError: module 'os' has no attribute 'uname'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem wit
h pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with
pip.

 gunicorn的错误出现在我试图使用它运行flask项目的时候

User
C:UsersAdministratorDesktop******>gunicorn -w 4 -t 30 -b 0.0.0.0
:5000 app:index
Traceback (most recent call last):
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39librunpy.
py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39librunpy.
py", line 87, in _run_code
    exec(code, run_globals)
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39Scriptsgu
nicorn.exe__main__.py", line 4, in <module>
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39libsite-p
ackagesgunicornappwsgiapp.py", line 9, in <module>
    from gunicorn.app.base import Application
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39libsite-p
ackagesgunicornappbase.py", line 11, in <module>
    from gunicorn import util
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython39libsite-p
ackagesgunicornutil.py", line 8, in <module>
    import fcntl
ModuleNotFoundError: No module named 'fcntl'

它们的运行或安装无一例外都是需要UNIX/Linux,都是默认在 UNIX/Linux 上运行,因此选择 Waitress作为windows上的服务器是最好选择

3.nginx命令

先来了解一点nginx命令方便下面操作

nginx.exe 启动nginx
nginx.exe -t 检查conf配置没有问题
nginx.exe -s stop 停止nginx
nginx.exe -s reload 更新nginx

4. Waitress命令

waitress-serve --host=0.0.0.0 --port=5000 myapp:application

修改成与flask项目配置有关的端口号地址

如果您的 Flask 应用程序在名为文件定义myapp.py,并且 WSGI 应用程序对象名为application

5.开始!

1.准备好你的flask项目

2.启动

3.使用 Waitress与它绑定监视来自nginx请求

waitress-serve --host=0.0.0.0 --port=5000 myapp:application

4.修改nginx配置文件,使其适用于你的项目

server {
    listen       8080;  # 修改为您的所需端口
    server_name  localhost;

    location / {
        # 允许所有请求方法,包括 POST
        allow all;
        proxy_pass ****;  # 更改为您的Flask应用程序的主机端口
    }

    location /static/ {
        root ******;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

5.启动nginx

nginx.exe 启动nginx
nginx.exe -t 检查conf配置有没有问题
nginx.exe -s reload 更新nginx
nginx.exe -s stop 停止nginx
nginx.exe 启动nginx

6.通过域名访问,大工告成!

原文地址:https://blog.csdn.net/m0_56366541/article/details/134750816

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49172.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注