简体   繁体   English

在现有的Nginx PHP配置中添加Flask应用

[英]Adding Flask app in existing nginx PHP configuration

The configuration below is the configuration for a web application in PHP, and it's working (I faked the site's name to https://sub.mysite.nl ). 下面的配置是PHP中Web应用程序的配置,并且可以正常工作(我将该站点的名称伪造为https://sub.mysite.nl )。

server {
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot

        ## some certificate info ##

        root /path/to/www;
        index index.php index.htm index.html;

        server_name sub.mysite.nl;

        location / {
                try_files               $uri $uri/ =404;
        }

        location ~ \.php$ {
                try_files               $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass            unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index           index.php;
                fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include                 fastcgi_params;
        }

        location ~ /\.ht {
            deny all;
        }

        ## some logging info ##

    }

server {
        if ($host = sub.mysite.nl) {
                return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 ipv6only=on default_server;

        server_name sub.mysite.nl;
        return 404; # managed by Certbot
}

Now I want to add a Flask app in a subfolder eg https://sub.mysite.nl/flaskapp . 现在,我想在一个子文件夹中添加一个Flask应用程序,例如https://sub.mysite.nl/flaskapp

The block below is what I got from the Flask Mega Tutorial I followed, see specifically this chapter: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux (under Setting Up Nginx). 下面的代码块是我从Flask Mega Tutorial中获得的,请参阅本章: https : //blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux (在设置Nginx下)。 I think I need to put this under location /flaskapp/ but I'm not sure how to proceed, because when I do this and go to https://sub.mysite.com/flaskapp it gives me 404 Not Found . 我想我需要将其放在location /flaskapp/但是我不确定如何继续,因为当我这样做并转到https://sub.mysite.com/flaskapp时,它会给我404 Not Found

    location /flaskapp {
            proxy_pass http://localhost:8000;
            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;
    }

Do I need to change the routing in my Flask app? 我需要在Flask应用中更改路由吗?

Ok, I've been fooling around a bit and it seems like editing the routes in my Flask app provides the easiest solution. 好的,我一直在鬼混,似乎在Flask应用中编辑路线提供了最简单的解决方案。 For this, I use the Flask nginx block from my original post at the bottom. 为此,我使用底部原始文章中的Flask nginx块。

So instead of @app.route('/') , I use @app.route('/flaskapp/') . 因此,我使用@app.route('/flaskapp/')而不是@app.route('/') @app.route('/flaskapp/')

And @app.route('/view_profile/<username>') becomes @app.route('/flaskapp/view_profile/<username>') . 然后@app.route('/view_profile/<username>')成为@app.route('/flaskapp/view_profile/<username>')

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

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