How do I set subdirectory in nginx with Django - python

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.

Related

nginx django uwsgi page not found error

I am trying to setup uwsgi and Django on Nginx but showing page not found error and error logs are empty.
I cannot identify the error because the error logs are empty.
Error log /var/log/nginx/error.log:
-rw-r--r-- 1 www-data root 0 Feb 26 12:31 error.log
uswgi is running properly because I tested this on following method:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir
/home/flybegins/python/django/sample -w sample.wsgi
virtual host
server {
listen 80;
server_name test.aaaaaaa.com;
error_log /var/log/nginx/error.log
location /static/ {
root /home/flybegins/python/django/sample/
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
} }
Virtual host permission:
-rw-r--r-- 1 root root 333 Feb 27 08:54 test.aaaa.com
Thanks in advance!
You need to install python plugin for uwsgi
sudo apt-get install uwsgi-plugin-python
or for python 3
sudo apt-get install uwsgi-plugin-python3
You are running your project using port 8080 with this code:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi
And you are trying to bind NGINX to a socket file that doesn't exist using this configuration:
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
}
That why it doesn't work.
I did two mistakes One is nginx virtual host configuration and another one is socket permission error
uWSGI Configuration
[uwsgi]
project = prd
base = /home/flybegins/python/django
chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application
master = true
processes = 5
gid = www-data
uid = www-data
socket = /var/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true
To create the space for the socket to exist, you just have to pick a persistent directory (e.g. not /run or /tmp) and make www-data (the user nginx runs as) the owner of it, as such:
$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi
My nginx virtual host configuration
server {
listen 80;
server_name testserver1.com;
access_log /home/flybegins/log/python/testserver1.com/access.log;
error_log /home/flybegins/log/python/testserver1.com/error.log error;
location /static {
alias /home/flybegins/python/django/prd/static_files/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/var/uwsgi/prd.sock;
}
}

How to run django project on nginx and uwsgi

I'm trying to run my first Django project using nginx and uwsgi. And I faced with the problem with nginx. I get the message in the brouser "Unable to connect. Firefox can't establish a connection to the server at localhost:8000". I'm following the instructions from this link Everything is fine with uwsgi and installing nginx. Then I downloaded uwsgi_params file and created mysite_nginx.conf file as the instruction says.
Here's mysite_nginx.conf file:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/myproject/mysite/friends_plans/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/myproject/mysite/friends_plans/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/myproject/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
It worked with server name "localhost" and port 8000 when I was checking uwsgi according to the intruction. I made the commands sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/ , python manage.py collectstatic , sudo /etc/init.d/nginx restart. In my settinds.py: STATIC_ROOT = "/home/ubuntu/myproject/mysite/friends_plans/static/". The command about static files worked properly. But I can't set the connection with the server. Could you please tell me where I have a mistake and how to correct it? Thank you very much in advance.

django cms - The requested URL /en/ was not found on this server - postgresql, nginx, gunicorn

Program versions:
django cms 3.0
python 3.4.2
django 1.7.3
Settings:
DEBUG = False
ALLOWED_HOSTS = ['localhost','localhost:8000',]
When using Sqlite, everything works fine.
With PostgreSQL, I get: The requested URL /en/ was not found on this server.
(With DEBUG - True, with either database, works fine.)
What am I doing wrong?
I forgot to mention that:
I run django-cms on gunicorn and
I use nGinx for static content
So, after I did some research and debugging, this is how I have solved my problem:
In nGinx config , i had this:
location / {
proxy_pass http://localhost:8000;
}
when DEBUG = False
ALLOWED_HOSTS = ['.example.com','.example.com.',]
was not working because
get_host() was returning localhost:8000
and should return example.com
This fixed it:
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $http_host;
}

Where should the uwsgi_params file be located and what is its extension?

I am having trouble wrapping my head around how this file works. I seem to see it in every example. See below:
server {
listen 80;
server_name sivusto3.fi;
access_log /var/log/customersite3/access_log;
location / {
root /var/www/customersite3;
uwsgi_pass 127.0.0.1:3033;
include uwsgi_params;
}
}
uwsgi_params file is located inside Nginx directory (/etc/nginx on my Linux machine)
me:~$ ls /etc/nginx | grep uwsgi
uwsgi_params
and has no extension.

how to get env_vars with nginx UWSGI_SETENV in uwsgi

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>

Categories