my nginx conf:
location / {
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /usr;
uwsgi_pass unix:/var/run/uwsgi-python/uwsgi/socket;
uwsgi_param UWSGI_CHDIR /var/www/my_site;
uwsgi_param UWSGI_SCRIPT my_site:app;
uwsgi_param SERVER_NAME my_site;
uwsgi_param UWSGI_SETENV DEPLOY_VERSION=Development;
}
my uwsgi para:
/usr/local/bin/uwsgi --master --processes 2 --logdate --chmod-socket=666 --uid www --gid www --limit-as 512 --harakiri 60 --max-requests 1000 --no-orphans —-reload-os-env --daemonize /var/log/uwsgi-python/uwsgi.log --pidfile /var/run/uwsgi-python/uwsgi/pid --socket /var/run/uwsgi-python/uwsgi/socket --xmlconfig /etc/uwsgi-python/apps-enabled/uwsgi.xml
uwsgi xml:
<uwsgi>
<master/>
<vhost/>
<memory-report/>
<no-site/>
</uwsgi>
In my flask app
print os.environ.get('DEPLOY_VERSION', 'NONE') #pring NONE
How I can get the env_vars?
Maybe I can not get the env_vars setting by UWSGI_SETENV in <vhost/><no-site/> mode?
btw: How you deploy multi version(Development/Beta/Release) of app in one machine without virtual env?
Instead of:
uwsgi_param UWSGI_SETENV DEPLOY_VERSION=Development;
You could set it as a per request variable in nginx: uwsgi_param DEPLOY_VERSION 'Development';
And then within Flask, access the variable via request.environ: request.environ['DEPLOY_VERSION']
(I had a similar problem and was pointed to the above solution on the uwsgi mailing list)
I had a similar issue defining environment configurations for a django Mezzanine CMS deployment.
As the DEPLOY_VERSION seems to target the underlying application and not the uWSGI service, I think the correct place to put it to be the uWSGI configuration file instead of the Nginx one.
Try changing the .xml file to:
<uwsgi>
<master/>
<vhost/>
<memory-report/>
<env>DEPLOY_VERSION=Development</env> <!-- this -->
<no-site/>
</uwsgi>
Related
I was following this tutorial to setup my flask server.
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04#step-6-%E2%80%94-securing-the-application
When I got to step 6 I see that they are setting flask for the whole url but I would like to point it to a specific port.
This is the code I have for my nginx which points. This currenly produces a 404.
server {
listen 5000;
server_name site.com;
location / {
include proxy_params;
proxy_pass http://unix:/home/user/project/project.sock;
}
}
All the other files are the same as the tutorial. I have tried to modify the .sock file but it seems like it was generated automatically and it can't be modified. In addition I need to find a way for nginx to handle this before I worry about handling it from gunicorn.
My end goal is to have nginx foward requests to flask running when a request is sent to 0.0.0.0:5000 and have all other requests 0.0.0.0 , 0.0.0.0/* be handled by nginx.
Any help to undestand all this is really appreciated got lost at this point.
EDIT
my nginx configuration in sites-available
server {
server_name domain www.domain;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8080/;
}
}
If you want flask to be open to a port instead of a file you should override
[service] to
[service]
...
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 -m 007 wsgi:app
And change your nginx config to proxy_pass http://127.0.0.1:8000/;
This way you can have access to port 8000 for checking how are working gunicorn and flask. Remember to be careful with firewall rules to secure port 8000. For a good discussion on which one is better you can try: gunicorn + nginx: Server via socket or proxy?
Centos7,when I connect to my website, shows 502 Bad Gateway,
I test my website with command
uwsgi --ini
systemctl start nginx
And I cant figure out what's happened,please help me!
here's nginx.conf
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
charset utf-8;
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass django;
}
location /static/ {
alias /usr/local/etc/dmp/static/;
}
}
and uwsgi setting
[uwsgi]
chdir = /usr/local/etc/dmp
module = DMP_python.wsgi
plugins = python3
socket = :8000
chmod-socket = 666
master = true
processes = 2
vacuum = true
You're using the wrong setting to tell uwsgi to use an HTTP port. You need http-socket rather than socket.
There can be multiple reasons for which an upstream might return invalid or even do not return any response
Verify if upstream uwsgi is actually running locally in the centos instance and can handle incoming requests
for this to verify run it as http-socket = :8000 in uwsgi.ini and then run uwsgi --ini uwsgi.ini
if uwsgi running fine on localhost, then change config back to socket = :8000
On centos 7.x SELinux package is enabled and runs in enforcing mode. So it won't allow nginx to write/connect to a socket.
verify if SELinux has the policy for nginx to write to sockets
check if read/connect/write to socket is allowed grep nginx /var/log/audit/audit.log | audit2allow -m nginx
do so by grep nginx /var/log/audit/audit.log | audit2allow -M nginx
and finally semodule -i nginx.pp
permission to connect to socket issue should be resolved by now
verify if nginx can do network connection with upstream. Check nginx error.log or getsebool -a | grep httpd
to allow it run setsebool httpd_can_network_connect on -P
I have a Flask app that I'm trying to host with Gunicorn and Nginx. I've followed several tutorials and I cannot seem to get Nginx to stop rendering the "Welcome to Nginx" page.
I've already confirmed that Supervisor successfully launches three Gunicorn workers, and Nginx is running in the background as well. SSL is confirmed working and Nginx picks up traffic on port 443. I've attempted to add Nginx to the same group as the user who owns the sock shared by Gunicorn and Nginx.
This is the bash script which Supervisor uses to start Gunicorn. This is confirmed working.
#!/bin/bash
NAME="Simon"
FLASKDIR=/var/www/Simon
SOCKFILE=/var/www/Simon/simon.sock
USER=glen
GROUP=glen
NUM_WORKERS=3
FLASK_SETTINGS_MODULE=config.py
FLASK_WSGI_MODULE=Simon.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $FLASKDIR
source Simon/bin/activate
export FLASK_SETTINGS_MODULE=$FLASK_SETTINGS_MODULE
export PYTHONPATH=$FLASKDIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
exec gunicorn Simon:simon \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
This the site config:
upstream simon_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/var/www/Simon/simon.sock fail_timeout=0;
}
server {
listen 443 ssl;
server_name glencoverx.com;
client_max_body_size 4G;
access_log /var/www/Simon/logs/nginx-access.log;
error_log /var/www/Simon/logs/nginx-error.log;
location /static/ {
alias /var/www/Simon/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if ($scheme != "https") {
return 301 https://$host$request_uri;}
}
ssl_certificate /etc/letsencrypt/live/glencoverx.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/glencoverx.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}
Weirdly enough, the logs show no problems. I've completely deleted the Nginx default site config and removed its symlink. I don't understand why Nginx will not communicate with Gunicorn.
first it would be helpful if you would do something like:
listen 443 default ssl;
This will make that site default.
If you haven't already, service nginx restart and make sure that your cache is purged. Try using telnet to do a get to make sure that it's still enabled.
Check your nginx/conf.d and sites-enabled dirs to see if the symlink or the default website is still there.
Environment:
uwsgi
nginx
django 1.3
I'm using the domain www.example.com with Django and nginx, and I want to access the Django by www.example.com/abc/ , but I don't know how to set the subdirectory.
This is the nginx conf file:
server {
listen 80;
server_name www.example.com;
error_log /var/log/nginx/xxx.error_log info;
root /home/web/abc; # this is the directory of the django program
location ~* ^.+\.(jpg|jpeg|png|gif|css|js|ico){
root /home/web/abc;
access_log off;
expires 1h;
}
location ~ /abc/ { # I want to bind the django program to the domian's subdirectory
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
When I open the website www.example.com/abc/, the django urls.py doesn't match, it only match the site like ^index$.
How can I modify the nginx location to set django to www.example.com/abc?
According to the uWSGI on Nginx docs, you just have to pass the SCRIPT_NAME to django.
location /abc {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
uwsgi_param SCRIPT_NAME /abc;
}
Django will still "see" /abc, but it should deal with it so that it gets stripped off before your urls are matched. You want this to happen, if django didn't see /abc, it would generate incorrect urls for your site and none of your links would work.
Now that uwsgi_modifier1 30 is removed in the latest versions of Nginx and uWSGI, I had to use a newer method to get it working:
uWSGI config:
[uwsgi]
route-run = fixpathinfo:
Nginx config:
location /abc {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
uwsgi_param SCRIPT_NAME /abc; # Pass the URL prefix to uWSGI so the "fixpathinfo:" route-rule can strip it out
}
IF THAT DOESN'T FIX IT: Try installing libpcre and libpcre-dev, then reinstall uwsgi with pip install -I --no-cache-dir uwsgi. uWSGI's internal routing subsystem requires the PCRE library to be installed before uWSGI is compiled/installed. More information on uWSGI and PCRE.
am trying to configure uwsgi and in the process it says on a tutorial that I must run
uwsgi -s /tmp/uwsgi.sock -w myapp:app
the problem is -w is an invalid option. Can anyone help me point out why or what should I do?
Thanks
maybe you are using debian-supplied packages. They are fully modular so you need to install/load the required plugins:
http://projects.unbit.it/uwsgi/wiki/Quickstart
My uwsgi app configuration looks like that
/etc/uwsgi/apps-enabled/mysite.ini
[uwsgi]
socket=/tmp/uwsgi_mysite.sock
chmod-socket=666
abstract-socket=false
master=true
workers=2
uid=altera
gid=altera
chdir=/home/altera/www/mysite ; Current dir
pp=/home/altera/www/mysite ; Python Path (to your application)
pyhome=/home/altera/vpy/mysite ; Path to virtual environment
plugins=python3
module=main ; *.py file name application starting from
post-buffering=8192
/etc/nginx/sites-available/mysite
server {
server_name mysite;
root /home/altera/www/mysite;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_mysite.sock;
}
}