Question

I'm sure I am missing something simple here. I can get PART of a python/flask script to be available through nginx, but the important bits are just not working. Here is my python:

#!/usr/bin/python
import flask
from flask import Flask, jsonify, render_template, request
import os

app = flask.Flask(__name__)
app.config['SERVER_NAME']='localhost'


@app.route("/stuff")
def index():
    return render_template('index.html')

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

def application(environ, start_response):

    start_response("200 OK", [("Content-Type", "text/plain")])
    return ["Hello World!"]

Here is my uwsgi start up:

 sudo -u www-data uwsgi -s /tmp/myApp.sock --module MyApp 

The socket is correctly linked and available to nginx.
And here is my nginx config snippet:

 location /test {
            uwsgi_pass unix:/tmp/myApp.sock;
            include uwsgi_params;
    }

When I go to myserver/test I get the "Hello World!" like I Would expect. But when I go to myserver/test/stuff, I ALSO get "Hello World!" rather than the contents of my index.html(Which is valid, I use it elsewhere). And if I enter myserver/test/garbage.html, I get a generic nginx 404, rather than my custom one.

Can anyone point me in a direction?

Thanks

--edit--

Thank you for the answer, it does help, but does not solve my entire issue.
Adding "--callable app" to my uwsgi startup line DOES link the uwsgi server to nginx. Yah! I can confirm this by the fact that my customized 404 file IS being returned by nginx.

But that 404 is ALL I can get. I cannot get to my index.html, which is present in the same directory as the 404.html, has the same owner, and the same rights. It is almost the same file really with slightly different text.

This MAY be a problem with expectations. I am expecting to find my index.html at http://(myserver)/test/stuff But I get a 404.

Am I looking in the wrong place? Or is there something off in my flask, uwsgi, or nginx? Thanks

Was it helpful?

Solution

You're application function does not call your flask app, which is why every route returns "Hello World", 200. I'm pretty sure you have two easy options.

The first is to drop the application function and replace it with application = app.

The second would be to change the uwsgi line to

sudo -u www-data uwsgi -s /tmp/myApp.sock --module MyApp --callable app

which renders your application function irrelevant anyway.

You can read more about using uwsgi here.

edit

As far as my knowledge about nginx goes, your nginx config should look like this

location = /test { rewrite ^ /test/; }
location /test { try_files $uri @test; }
location @test {
  include uwsgi_params;
  uwsgi_param SCRIPT_NAME /test;
  uwsgi_modifier1 30;
  uwsgi_pass unix:/tmp/myApp.sock;
}

It is the same as the recommended one on the linked uwsgi page above. You seem to be running not on the root url, so the basic config will not work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top