Flask + Gunicorn + Nginx,使用来自非根位置块的proxy_pass出现404错误

问题描述 投票:0回答:1

我想接受一些用户输入,运行几行Python,并在网络上显示结果。不出所料,这是一件繁琐的事情。

我有一个域指向DigitalOcean上的服务器,并且正在关注本教程:https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

我已经完成了本教程,并且确实可以使用,但是我当然希望我的网站不被短语“ Hello there!”完全覆盖。我想在非根目录位置显示结果,例如https://example.com/myproject/

我已经使用Let's Encrypt&CertBot保护的域。

我正在使用一个名为default的单个nginx配置文件-我完全按照本教程的其余部分操作。问题似乎出在proxy_pass指令中。当我将其移动到/ location块时,它可以工作,并且我的索引页被“ Hello there!”覆盖。当我将proxy_params和proxy_pass移到/ myproject /位置块时,出现404错误。我尝试了一些事情,并试图更好地理解位置限制,但无济于事。

这里是Nginx配置文件:

# Default server configuration
#
server {

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php;

    server_name example.com www.example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    location /myproject/ {
        include proxy_params;
        proxy_pass http://unix:/home/adude/myproject/myproject.sock;
    }

    # pass the PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

任何帮助将不胜感激。谢谢!

python nginx flask reverse-proxy gunicorn
1个回答
0
投票

想通了。我需要直接将.py文件中的@ app.route装饰器更改为正确的值,并且我认为至关重要的是指定GETPOST方法。

from flask import Flask
app = Flask(__name__)

@app.route("/myproject/", methods=['GET','POST','PUT'])
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

© www.soinside.com 2019 - 2024. All rights reserved.