简体   繁体   English

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

[英]Flask + Gunicorn + Nginx, 404 error using proxy_pass from non-root location block

I would like to take some user input, run a few lines of Python, and display the results on the web.我想获取一些用户输入,运行几行 Python,然后在 Web 上显示结果。

I have a domain pointed to a server on DigitalOcean, and am following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04我有一个域指向 DigitalOcean 上的服务器,并且正在学习本教程: https : //www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on -ubuntu-18-04

I've been able to get through the tutorial and it does work, however I'd of course like for my site not to be completely overridden by the phrase "Hello there!".我已经能够完成教程并且它确实有效,但是我当然希望我的网站不会被“你好!”这句话完全覆盖。 I would like to display the results at a non-root location such as https://example.com/myproject/ .我想在非 root 位置显示结果,例如https://example.com/myproject/

The domain I have has already been secured using Let's Encrypt & CertBot.我已经使用 Let's Encrypt & CertBot 保护了我的域。

I am using a single nginx config file called default - the rest of the tutorial I followed exactly.我正在使用一个名为 default 的 nginx 配置文件 - 我完全遵循本教程的其余部分。 The problem seems to be in the proxy_pass directive.问题似乎出在 proxy_pass 指令中。 When I move it to the / location block, it works and my index page is overridden with "Hello there!".当我将它移动到 / location 块时,它可以工作并且我的索引页面被“Hello there!”覆盖。 When I move proxy_params and proxy_pass to a /myproject/ location block, I get a 404 error.当我将 proxy_params 和 proxy_pass 移动到 /myproject/ 位置块时,我收到 404 错误。 I've tried a handful of things and tried to understand location blocks better, but to no avail.我尝试了一些东西并试图更好地理解位置块,但无济于事。

Here is the Nginx config file:这是 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

}

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you!谢谢!

I needed to change the @app.route decorator in the .py file to the correct directly, and I think crucially specify the GET and POST methods.我需要将 .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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM