nginx + uwsgi + flask - disabling custom error pages - python

Is it possible to disable nginx's custom error pages - if I may call them that - to display my framework's exception pages?
I can't really see my werkzeug debugger tool rendered in html...
UPDATE
OK, I got to make a very very simple flask application to work and I'll post the bits:
/home/my_user/.virtualenvs/nginx-test/etc/nginx.conf
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 5000;
server_name localhost;
access_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/access.log;
error_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/error.log;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
}
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
raise Exception()
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
PYTHONPATH environment variable:
$ echo $PYTHONPATH
/home/my_user/dev/
How I run uwsgi:
$ uwsgi -s /tmp/uwsgi.sock --module nginx_test --callable app
How I run nginx:
$ nginx -c ~/.virtualenvs/nginx-test/etc/nginx.conf -p ~/.virtualenvs/nginx-test/lib/nginx/
If I hit the root page:
If I run nginx manually like:
python /home/my_user/dev/nginx_test/___init___.py
I will see instead, and what I want to see:
Of course I made sure it would work when I didn't raise the exception, but returned 'Hello World' for example on my index() function.
This is referred to custom error pages in .NET. I want to disable this and let nginx/uwsgi pass the html generated by the debugger directly to the browser instead of the internal server error thing.
UPDATE 2
Now if I change my flask app to enable debugging mode by:
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
app.config.update(DEBUG=True)
#app.route('/')
def index():
raise Exception()
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
Then I get 502 error.
But if I instead of raise Exception:
/home/my_user/dev/nginx_test/___init___.py
from flask import Flask
app = Flask(__name__)
app.config.update(DEBUG=True)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)
I get 'Hello World' on my browser when I hit the page (http://localhost:5000).

This "Internal Server Error" page is not from nginx but from Flask. It does so when debug mode is off.
uwsgi is importing your code as a module, not running at as a script. __name__ == '__main__' is False and the if statement is not executed. Try setting debug mode outside of the if:
app = Flask(__name__)
app.debug = True
However, it is strongly recommended to never leave the debug mode on a server on the public internet, since the user can make the server run any code. This is a serious security issue.

Use Flask#errorhandler to register your own error handlers in flask. For example to replace the 404 you would do something like:
app = Flask()
#app.errorhandler(404)
def handel_404(error):
return render_template('404.html')

Simon Sapin has really given you the correct answer. You need to enable debug in Flask. Nginx does not return any custom error pages unless you explictly configure it to do so.
If you use the following code you will see your debug messages from flask, proxied via Nginx.
from flask import Flask
app = Flask(__name__)
app.debug = True
#app.route('/')
def index():
raise Exception()
As per your update 2. You are seeing a 502 (bad gateway) because Flask is simply not returning any response at all, or a response that Nginx does not understand. A 502 is not a problem with Nginx. It means that whatever Nginx is trying to talk (your flask app in this case) is not working properly at all.
However, in many ways you shouldn't be doing this. Debugging mode should only be enabled when you are running flask on your local dev machine. Which is the whole point of the if __name__ == "__main__": line anyway.

Related

Flask app not running on localhost, no error

Similar code to this was working fine, then suddenly not and I have no idea why. Hoping somebody can help. I have the following:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hi"
if __name__ == "__main__":
app.run(debug=True)
When I run that, the output is the following:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
There is no error, but also no "Running on http://localhost:5000/" and nothing loads at that URL ("This site can't be reached") etc.
I have tried specifying the host many ways and many other things. Still get the same. Previously it was working fine, said "Running on http://localhost:5000/," was loading fine in my browser, etc.
What am I missing here?
You can explicitly set the host and the port on your script:
# ...
if __name__ == "__main__":
app.run(host='localhost', port=5000, debug=True) # or setting host to '0.0.0.0'

FLASK: curl: (7) Failed to connect to 127.0.0.1 port 5500: Connection refused

I am trying to transition from making Rshiny apps to flask-react apps. Its definitely a steep learning curve so far!
I am sort of following a few tutorials e.g (https://dev.to/arnu515/build-a-fullstack-twitter-clone-using-flask-and-react-1j72) to try and get some basic functionality down .
However some reason curl can't seem to interact with my app. I've tried putting the urls with and without quotes but get the same response. Also I tried the default 5000 port as well. I am running the app in windows:
C:\Users\Marc\flaskTest\backend>curl "http://127.0.0.1:5500"
curl: (7) Failed to connect to 127.0.0.1 port 5500: Connection refused
app.py code
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
#app.route('/')
def index():
return "hello"
#app.route('/message', methods=["GET"])
def message():
message ="my message"
return jsonify({"message": message})
if __name__ == "__main__":
app.run(debug=True, port=5500)
You used jsonify in the view function but haven't imported it before, so there would be error when Flask app runs.
Actually you can just write code like return {"message": message}, it would do the same thing with jsonify does if you are using latest version of flask.
Try:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5500)
Also in windows cmd type ipconfig IPV4 address. Suppose your IPV4 address is 192.168.X.X, access the website as http://192.168.X.X:5500.
Read what it does: Externally Visible Server

Flask + gunicorn proxy setup?

I try to wire Flask, gunicorn and nginx together but it ends up with 400 Bad request and 500 Errors.
Hope that anyone can help.
nginx conf
server {
listen 8000;
server_name 127.0.0.1;
location /Hello {
uwsgi_pass 127.0.0.1:8081;
}
}
wsgi.py snippet:
from app import app
app.run(host="127.0.0.1", port=8081)
app.py
app = Flask(__name__)
app.secret_key = "Not A Secret Anymore By Now"
#app.route('/Hello')
def hello():
return("Hello")
#if __name__ == "__main__":
# app.run(host='0.0.0.0', port=8000)
run cmd
export FLASK_APP=app
export FLASK_ENV=development
gunicorn --bind 0.0.0.0:8081 wsgi:app
test cases
curl http://localhost/ >>> returns nginx homepage
curl http://localhost/Hello >>> returns 404 Not found
curl http://localhost:8000/ >>> returns nginx homepage
curl http://localhost:8000/Hello >>> returns 502 Bad Gateway
curl http://localhost:8081 >>> return Connection refused
I try to get the test cases working, but have not clue why the errors appears, (nginx is restarted).
Thank you.
A few problems here...
Your view function ends without a return statement, which is probably the cause of the first 500 error. Better use:
#app.route('/Hello')
def hello():
print("Hello")
return 'success'
Also as you metntion uwsgi.py is emtpy, there's no app object to import there. Best point the gunicorn command at a valid app object:
gunicorn --bind 0.0.0.0:8081 app:app
Also the nginx location block expects the request to go to http://example.com/Hello/Hello. So better make the location block like the following. (Also ensure the port is the same one you provded to gunicorn's --bind flag).
location / {
proxy_pass http://127.0.0.1:8081;
}
Here I've also used proxy_pass as per the gunicorn deployment (with nginx) docs.
Now the request should work:
# Direct to app server...
curl -i http://localhost:8081/Hello
HTTP/1.1 200 OK
Server: gunicorn/20.0.4
# ...
success
and also:
# Via nginx:
curl -i http://localhost/Hello
HTTP/1.1 200 OK
Server: nginx
...
success
Beware that running gunicorn on all interfaces (0.0.0.0) means external requests could reach the app server directly (bypassing nginx) if no firewall is in place to prevent this. Might be worth instead binding gunicorn to the local interface: --bind 127.0.0.1:8081.
Also be careful in general, there's lots of ways to make security slip-ups when configuring stuff like this yourself. Good luck.

Running a Flask app on nginx

I don't know exactly what I am doing but I am experimenting with running Flask on nginx. I am boiling it down to the simple bit of code below. First I have a test app in Flask like this:
from flask import Flask, render_template
app = Flask(__name__, static_folder='client', template_folder='client/html')
def show_home_page():
return render_template("home.html")
#app.route('/')
def server():
return show_home_page()
if __name__ == '__main__':
app.run(threaded=True)
If I run python app.py I can go to http://localhost:5000 and see the "Hello World". Next I read that I need to run uwsgi but its not clear what params I need to pass to it. I tried different things like:
uwsgi -s /tmp/app.sock --manage-script-name --mount ./=app:app
I also noted that I need to set my nginx conf file to match, but I am stuck on that as well (I just get a welcome from nginx on port 5000) and it doesnt seem to link with my Flask app. I googled around a bit but nothing has clicked yet.
server {
listen 5000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
You can use refer this for Flask with Nginx and uWSGI:
Python flask with Nginx and uWSGI

Serve a Flask app on a sub-url during localhost development

I have a Flask app as backend that serves a REST API and an AngularJS front-end app.
I use Grunt/Livereload to serve the front-end at the address: http://localhost:5000/
Is it possible to serve the Flask app on a sub-url of localhost during development, using app.run() or run_simple from werkzeug?
Specifically I would like to have the Flask app accessible at the address: http://localhost:5000/api
I found this solution but it has the disadvantage of serving a dummy app at http://localhost:5000/ which uses the address and doesn't let me serve the AngularJS app at that address.
The way that Flask and Yoman are set up you cannot actually do this - it is possible to have two processes share the same port, but it is generally done to allow one master process to pass off handling of individual requests to sub-processes, which is not quite what you are doing here. (In general, in production, you would run both the front and back end behind a proxy server like nginx.)
Fortunately, you do not have to re-invent the wheel or run a separate proxy server just to develop your app - there is a Grunt plugin called grunt-connect-proxy that will let you proxy requests to a sub-url to another location entirely. That will let you spin up your Flask backend server on a different port (say port 5001) and proxy requests to localhost:5000/api (for example) to localhost:5001/:
connect: {
options: {
port: 5000,
hostname: 'localhost'
},
proxies: [
{
context: '/api',
host: '127.0.0.1',
port: 5001,
https: false,
changeOrigin: false,
xforward: false
}
]
}
And then you can run your Flask app with app.run(port=5001).
This worked for me
from flask import Flask
prefix = '/abc'
app = Flask(__name__)
# redefine route
def route(path, *args, **kwargs):
return _route(prefix + path, *args, **kwargs)
_route = app.route
app.route = route
# Test function
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()

Categories

Resources