I'm trying to implement a basic flask application in a docker container which uses ngnix container for request/response with the help of uWSGI module in python.
But, I'm running into a pid error which I'm totally confused of and banging my head. Please take a look to the files below
my flask application(run.py)
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Welcome to the site"
if __name__ == "__main__":
app.run()
my app.ini file for uWSGI
[uwsgi]
wsgi-file = run.py
callable = app
socket = :8000
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
my Ngnix conf file (default.conf)
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /sample {
include uwsgi_params;
uwsgi_pass flask:8000;
}
}
Note: flask is my container name and /sample is the page i want to reach which is flask ideally.
The error I run into while => curl http://localhost/sample
flask | [pid: 8|app: 0|req: 1/2] 172.23.0.1 () {32 vars in 343 bytes} [Tue Oct 4 03:00:29 2022] GET /thowbik => generated 207 bytes in 3 msecs (HTTP/1.1 404) 2 headers in 87 bytes (1 switches on core 0)
nginx | 172.23.0.1 - - [04/Oct/2022:03:00:29 +0000] "GET /sample HTTP/1.1" 404 207 "-" "curl/7.79.1" "-"
I checked all other setting, I hope, I did everything correct. While reaching / page it gives response nginx default page. but I cannot get response for /sample
For your Info https://www.youtube.com/watch?v=dVEjSmKFUVI, this is the tutorial I follow up to do.
Okay, I found the answer for the above question !!, the mount option in app.ini file should be declared as location. Like, mount = /sample=run.py and I get the response from flask
Related
I have a Flask back end that is functional without using uwsgi and nginx.
I'm trying to deploy it on an EC2 instance with its front-end.
No matter what I do, I can't reach the back-end. I opened all the ports for testing purposes but that does not help.
Here's my uwsgi ini file:
[uwsgi]
module = main
callable = app
master = true
processes = 1
socket = 0.0.0.0:5000
vacuum = true
die-on-term = true
Then I use this command to start the app:
uwsgi --ini uwsgi.ini
The message returned is
WSGI app 0 (mountpoint='') ready in 9 seconds.
spawned uWSGI worker 1 (and the only) PID: xxxx
Then here is my Nginx conf file:
server {
server_name my_name.com www.ny_name.com
location / {
root /home/ubuntu/front_end/dist/;
}
location /gan {
proxy_pass https:localhost:5000/gan;
}
## below https conf by certbot
}
If my understanding is correct, whenever a request reaches "my_name.com/gan..." it will be redirected to the localhost on the port 5000 where the back-end is started by uwsgi.
But I can't reach it. I'm trying to simply do a get request on "my_name.com/gan" on my browser (it should return a random image) but I get a 502 by nginx.
Important to note, the front-end works fine and I can access it on browser.
My guess is that url is not in proper form
Try
proxy_pass http://0.0.0.0:5000;
This error occurs for me solely when using flask restplus with an nginx server. The server works correctly when just flask is used and the flask-restplus api works fine when running the app locally or even with:
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:application
on my EC2 instance.
I have tried using gunicorn along side nginx (rather than uwsgi) but I get the same error in my /var/log/nginx/error.log file:
2017/07/03 20:30:02 [crit] 957#957: *1 connect() to unix:/home/jamie/my_app/my_app.sock failed
(2: No such file or directory) while connecting to upstream,
client: #.#.#.#, server: #.#.#.#, request: "GET /favicon.ico HTTP/1.1", upstream:
"uwsgi://unix:/home/jamie/my_app/my_app.sock:", host: "#.#.#.#", referrer: "http://#.#.#.#/"
The app is envoked through a wsgi.py that looks like this:
from main import application
if __name__ == "__main__":
application.run('0.0.0.0')
And the main.py file contains the following:
from boto3 import resource
from flask import json, request, abort
from flask import Flask
from flask_restplus import Api, Resource, fields
application = Flask(__name__)
api = Api(application)
#api.route('/users', methods=['GET', 'POST'])
class Users(Resource):
def get(self):
return users_get()
#api.expect(resource_fields)
def post(self):
return user_login()
#api.route('/users/<string:Username>', methods=['GET', 'PUT', 'DELETE'])
class User(Resource):
def get(self, Username):
return user_get(Username)
def put(self, Username):
return create_user(Username)
def delete(self, Username):
return delete_user(Username)
In order to configure the server I followed this digitalocean tutorial and my config is essentially the same. If anyone wants to see it I can provide it.
My uwsgi ini file is as follows:
[uwsgi]
module = wsgi:application
master = true
processes = 5
socket = my_app.sock
chmod-socket = 666
vacuum = true
die-on-term = true
My nginx sever config is at etc/nginx/sites-available/my_app
like so:
server {
listen 80;
server_name #.#.#.#;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/jamie/my_app/my_app.sock;
}
}
And finally my /etc/systemd/system/fitness_test_app.service file is like so:
[Unit]
Description=uWSGI instance to serve my_app
After=network.target
[Service]
User=jamie
Group=www-data
WorkingDirectory=/home/jamie/my_app
Environment="PATH=/home/jamie/my_app/myprojectenv/bin"
ExecStart=/home/jamie/my_app/myprojectenv/bin/uwsgi --ini my_app.ini
[Install]
WantedBy=multi-user.target
If anyone has any idea what could cause this problem an answer would be greatly appreciated as I have been unable to solve this issue for some time.
I am trying to get my Python application to run on port 80 so I can host my page to the Internet and see my temperature and all that remotely.
I get a 402 Bad Request error and I can't seem to figure out why. It seems it's having trouble writting my .sock file to a temp directory.
I am following this tutorial.
https://iotbytes.wordpress.com/python-flask-web-application-on-raspberry-pi-with-nginx-and-uwsgi/
/home/pi/sampleApp/sampleApp.py
from flask import Flask
first_app = Flask(__name__)
#first_app.route("/")
def first_function():
return "<html><body><h1 style='color:red'>I am hosted on Raspberry Pi !!!</h1></body></html>"
if __name__ == "__main__":
first_app.run(host='0.0.0.0')
/home/pi/sampleApp/uwsgi_config.ini
[uwsgi]
chdir = /home/pi/sampleApp
module = sample_app:first_app
master = true
processes = 1
threads = 2
uid = www-data
gid = www-data
socket = /tmp/sample_app.sock
chmod-socket = 664
vacuum = true
die-on-term = true
/etc/rc.local just before exit 0
/usr/local/bin/uwsgi --ini /home/pi/sampleApp/uwsgi_config.ini --uid www- data --gid www-data --daemonize /var/log/uwsgi.log
/etc/nginx/sites-available/sample_app_proxy and I verified this moved to sites-enabled after I linked it.
server {
listen 80;
server_name localhost;
location / { try_files $uri #app; }
location #app {
include uwsgi_params;
uwsgi_pass unix:/tmp/sample_app.sock;
}
}
I got all the way to the final step with 100 percent success. After I linked the sample_app_proxy file so it gets copied into /nginx/sites-enabled/ I do a service nginx restart. When I open my browser 'localhost' I get a 502 Bad Request.
I noticed in the nginx logs at the bottom that there was an error.
2017/01/29 14:49:08 [crit] 1883#0: *8 connect() to unix:///tmp/sample_app.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:///tmp/sample_app.sock:", host: "localhost", referrer: "http://localhost/"
My source code is exactly as you see it in the tutorial, I checked it over many times.
I looked at the /etc/logs/uwsgi.log and found this message at the bottom.
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
The -s/--socket option is missing and stdin is not a socket.
I am not sure what is going on and why it doesn't seem to write the .sock file to the /tmp/ directory. The test I did earlier in the tutorial worked fine and the sample_app.sock file showed up in /tmp/ But when I run the application it doesn't seem to work.
I did a lot of searching and I saw many posts saying to use "///" instead on "/" in the /etc/nginx/sites-available/sample_app_proxy file, but whether I use one or three, I still get the 502 error.
uwsgi_pass unix:///tmp/sample_app.sock;
Any help would be greatly appreciated as this is the last step I need to accomplish so I can do remote stuff to my home. Thanks!
I'm using a python app (modoboa) which used to be served with uwsgi to nginx via uwsgi-protocol.
I'm trying out h2o server now which doesn't speak the uwsgi protocol but http.
So I'm trying to migrate uwsgi from using a uwsgi-socket to a http-socket, but uwsgi throws an error "no python application found" at the point I am now.
This was in my nginx.conf:
location /modoboa/ {
root /usr/local/www/modoboa_default/modoboa_default;
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi-modoboa.sock;
uwsgi_param UWSGI_SCRIPT modoboa_default.wsgi:application;
uwsgi_param UWSGI_SCHEME https;
uwsgi_param SCRIPT_NAME /modoboa;
uwsgi_modifier1 30;
}
This was my uwsgi.ini:
[uwsgi]
chdir = /usr/local/www/modoboa_default
module = modoboa_default.wsgi:application
master = true
harakiri = 60
processes = 4
vhost = true
no-default-app = true
This is my uwsgi.ini now:
[uwsgi]
chdir = /usr/local/www/modoboa_default
module = modoboa_default.wsgi:application
master = true
harakiri = 60
processes = 4
vhost = true
no-default-app = true
http-socket=/tmp/uwsgi-http.sock
enable-threads=true
buffer-size=60000
http-keepalive=3000
I guess I'm missing something in my uwsgi.ini
update
this is what I have so far
h2o.conf
"/modoboa/":
proxy.reverse.url: "http://[unix:/tmp/uwsgi-http.sock]/"
proxy.timeout.keepalive: 1000
proxy.preserve-host: ON
"/modoboa/sitestatic/":
file.dir: /usr/local/www/modoboa_default/sitestatic/
"/modoboa/media/":
file.dir: /usr/local/www/modoboa_default/media/
uwsgi.ini
[uwsgi]
chdir = /usr/local/www/modoboa_default
harakiri = 60
processes = 4
http-socket = /tmp/uwsgi-http.sock
enable-threads = true
mount = /modoboa=modoboa_default.wsgi:application
manage-script-name = true
This works for the start page.
However, when I login I will get redirected to example.com/accounts/login/ instead of example.com/modoboa/accounts/login/
You should remove vhost and no-default-app options if you're explicitly setting module in uWSGI configuration. With that options set, uWSGI is expecting to get information about that from HTTP server, but H2O is not setting anything.
Also, it is insecure to user that settings with servers that can send proper headers unless you're exactly know what they're for, so remove them also from your existing configuration for nginx. You have module set in uWSGI config.
I think you must have the following in the [uwsgi] section (i.e. the same socket name in uwsgi.ini and nginx.conf)
socket = /tmp/uwsgi-modoboa.sock
chmod-socket = 644
If 644 doesn't work, try 666 or even 777 (be aware of the security issues with wide open permission on a shared server)
I installed uwsgi with nginx on my ubuntu 12.04 homerserver and try to test a simple Flask-App:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def application():
return 'Hello World!'
if __name__ == '__main__':
app.run()
with python app.py it works.
But not with uwsgi --socket 127.0.0.1:3031 --file /srv/www/test/app.py --callable application --catch-exceptions
I just get a this error TypeError: application() takes no arguments (2 given) and don't know why. Where this two arguments come from?
Here is my uwsgi.conf:
1 description "uWSGI Emperor"
2 start on runlevel [2345]
3 stop on runlevel [06]
4 respawn
5
6 exec uwsgi --master --die-on-term --emperor /etc/uwsgi/apps-enabled
and my nginx.conf
server {
94 listen 8000;
95 server_name localhost;
96 root /srv/www/test;
97
98 location /static/ {
99 alias /srv/www/test/static/;
100 expires 30d;
101 access_log off;
102 }
103
104 location / {
105 include uwsgi_params;
106 uwsgi_pass 127.0.0.1:3031;
107 }
108 }
i try it before with an .ini-file in apps-enabled but i get this way errors too.
I hope that someone can help me. :\
"application" is a flask callable (defined by you) not a WSGI callable (like you configured in uWSGI). Your WSGI callable is "app" (the main entry point). Just change --callable application to --callable app