【问题标题】:Flask + Gunicorn + Nginx, 404 error using proxy_pass from non-root location blockFlask + Gunicorn + Nginx,使用来自非根位置块的 proxy_pass 出现 404 错误
【发布时间】:2020-07-06 08:58:58
【问题描述】:

我想获取一些用户输入,运行几行 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 指令中。当我将它移动到 / 位置块时,它可以工作,并且我的索引页面被“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/jackson/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】:

    我需要将 .py 文件中的 @app.route 装饰器直接更改为正确的,我认为关键是指定 GETPOST 方法。

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/myproject/", methods=['GET','POST'])
    def hello():
        return "<h1 style='color:blue'>Hello There!</h1>"
    
    if __name__ == "__main__":
        app.run(host='0.0.0.0')
    
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-01-20
      • 2023-04-03
      • 1970-01-01
      • 2020-09-03
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多