我在运行nginx时无法访问由gunicorn提供的Flask网站



我提前道歉,我对这一切很陌生!

我已经使用Flask-socketio编写了一个应用程序。该网站在开发服务器上运行良好,并通过 localhost:5000 访问它。

我正在使用 gunicorn 为应用程序提供服务,当我运行时

gunicorn --bind 0.0.0.0:5000 wsgi:app

我可以通过0.0.0.0:5000访问该网站。

现在我尝试使用 systemd 和 nginx 创建一个通往外部世界的接入点。nginx可以正常工作,但是如果我尝试在浏览器中打开它,它将不再工作。我已经尝试了各种各样的事情,使用服务器的IP地址并再次尝试0.0.0.0。

我通过用systemd启动nginx来调用它。也许这是错误的?

systemctl status nginx

我在这里不知所措,到目前为止,网上建议的任何解决方案都没有帮助我。

我的应用程序结构是:/opt/myproj/proj_srv/functions (如 application.py 和 wsgi.py(/opt/myproj/proj_srv/functions/static (所有静态文件,如 index.html(

我的项目的配置文件位于/etc/nginx/sites-enabled/NEMO

listen 80;
server_name FRTM999.com www.FRTM999.com;
location/ {
proxy_pass http://127.0.0.1:8080;
}
location /static/ {
autoindex on;
alias /opt/NEMO/NEMO_Srv/functions/static/;
}
}

nginx配置文件/etc/nginx/nginx.conf


events {
worker_connections 1024;
}
http {
sendfile on;
gzip              on;
gzip_http_version 1.0;
gzip_proxied      any;
gzip_min_length   500;
gzip_disable      "MSIE [1-6].";
gzip_types        text/plain text/xml text/css
text/comma-separated-values
text/javascript
pplication/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to serve static files 
location ^~ /static/  {
# Example:
# root /full/path/to/application/static/file/dir;
root /opt/NEMO/NEMO_Srv/functions/static/;
}
Serve a static file (ex. favico)
# outside /static directory
location = /favico.ico  {
root /opt/NEMO/NEMO_Srv/functions/static/favico.ico;
}
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass         http://app_servers;
proxy_redirect     off;
proxy_set_header   Host $host;
proxy_set_header   X-Real-IP $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

我的系统服务文件

[Unit]
Description=uWSGI instance to serve myproject
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/opt/NEMO/NEMO_Srv/functions
ExecStart=/usr/bin/env gunicorn wsgi:app -b 0.0.0.0:5000
[Install]
WantedBy=multi-user.target

我不确定还能提供什么,如果我能把我的问题说得更清楚,请告诉我。也感谢您抽出宝贵时间!我非常感谢!

查看您的代码,似乎nginx只是代理端口8080。

upstream app_servers {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
}
location / {
proxy_pass http://127.0.0.1:8080;
}

但是,gunicorn 似乎正在侦听端口 5000。

ExecStart=/usr/bin/env gunicorn wsgi:app -b 0.0.0.0:5000

这两个应用程序不在同一端口上通信,所以我认为这就是您出现问题的原因。

最新更新