I have a Flask app with a web service returning
Response(json.dumps({"msg": "Access denied"}), status=403, mimetype='application/json')
when the request is not authenticated (basic http auth).
When running the app using Flask's development server, I don't specify any user in my request (no -u option) I get, as expected
$ curl -v http://localhost:5000/api/get_predictions
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5000 (#0)
> POST /api/get_predictions HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:5000
> Accept: */*
> Content-Length: 964812
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------5a6165ae33f0142f
>
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 403 FORBIDDEN
< Content-Type: application/json
< Content-Length: 24
< WWW-Authenticate: Basic realm="Authentication Required"
* Server Werkzeug/0.11.3 Python/2.7.11 is not blacklisted
< Server: Werkzeug/0.11.3 Python/2.7.11
< Date: Wed, 13 Jan 2016 15:42:48 GMT
<
* Closing connection 0
{"msg": "Access denied"}
Now, if I run my app with uwsgi + nginx I get
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /api/get_predictions HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8000
> Accept: */*
> Content-Length: 964812
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------9ad7410b97d91f5d
>
< HTTP/1.1 100 Continue
< HTTP/1.1 403 FORBIDDEN
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Wed, 13 Jan 2016 15:45:48 GMT
< Content-Type: application/json
< Content-Length: 24
< Connection: keep-alive
< WWW-Authenticate: Basic realm="Authentication Required"
* HTTP error before end of send, stop sending
<
curl keep running (or waiting for something?) and I don't get the {"msg": "Access denied"} message.
I noticed the HTTP 1.0, assume close after body in the development server response, and I guess it has something to do with that, but I don't know how to achieve the same behaviour with nginx.
Anybody can help?
The app is configured with
/etc/nginx/sites-available/myflaskapp
server {
listen 8000;
server_name localhost;
client_max_body_size 50M;
location / {
include uwsgi_params;
uwsgi_pass unix:<path to the socket>;
}
}
and I left the default nginx config
/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}