Nginx hosted uwsgi django app: only accessible via curl, not by browser - django

I'm hosting a Django app using uwsgi and nginx.
If I call curl -v my_ip:port it connects. If I direct a browser towards my_ip:port it returns a ERR_CONNECTION_REFUSED and the connection doesn't show up in the log.
uWSGI:
file : data_collection_project.ini
[uwsgi]
project = data_collection_project
base = /data_nfs/data_collection_project
chdir = %(base)
# /%(project)
home = /home/rootadmin/.virtualenvs/data_collection
#plugins = python
#module = data_collection_project.wsgi:application
module = data_collection_project.wsgi:application
master = true
processes = 2
socket = %(base)/%(project).sock
chmod-socket = 666
nginx:
file: /etc/nginx/sites-available
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///data_nfs/data_collection_project/data_collection_project.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 8433;
# the domain name it will serve for
server_name my_ip; # 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 /data_nfs/data_collection_project/media; # your Django project's media files - amend as requir$ }
location /static {
alias /data_nfs/data_collection_project/static; # your Django project's static files - amend as requi$ }
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data_nfs/data_collection_project/uwsgi_params; # the uwsgi_params file you installed
}
}
In my understanding, this should make the website available over my_ip for outside browsers, but it isn't. I don't understand why. What am I missing?
EDIT: If I switch off uwsgi and then try to call the ip:port there is an errorlog added to /var/log/nginx/error.log connect () to unix:///...sock failed. (111:connection refused) This error makes sense, since uwsgi isn't handling the socket. This seems to mean that nginx is working, and does pass the request along to the socket when i do curl my_ip:port?

I'm not 100% sure but per your EDIT it seems that nginx is passing correctly. Maybe Django's ALLOWED_HOSTS? I'd follow these steps:
Make sure that data_collection_project.ini is the one ini used (maybe try command line arguments instead of an ini file as a one of test).
Add deamonize to your data_collection_project.ini to make uwsgi log to a file.
Check if it's not Django that is dropping your connection.

Related

Serving NGINX with DJANGO on Ubuntu

I am trying to Run my django(1.8) project on Nginx server since it is much faster. I am able to run by using uwsgi --ini commands to setup socket. so what I wants to do is by running NGINX alone want to run my django project, Is there any way to do that? Well the socket created by uwsgi is removing automatically when uwsgi --ini command ends.
NGINX config and .ini ia as shown below :
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///var/www/html/vir/crum/crumbs.sock;
}
# 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.com;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/html/alteryx_vir/crum/media;
}
location /static {
alias /var/www/html/vir/crum/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
/var/www/html/vir/crum/uwsgi_params;
}
}
>>> uwsgi.ini file :
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /var/www/html/vir/crumb/
# Django's wsgi file
module = crum.wsgi
# the virtualenv (/alteryx_vir/)
home = /var/www/html/alteryx_vir/
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket =
/var/www/html/alteryx_vir/crum/crum.sock
#socket = var/run/uwsgi/crum.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
Thanks in advance for you help.
What you're asking doesn't make sense. The socket is for communication between nginx and uWSGI. If uWSGI is not running, there would be nothing on the other end of the socket and nothing serving your app.
You need both nginx and uWSGI.
Yes Atlast i could automate everything byusing UWSGI EMPEROR mode.
Now All commands can be automate all you needs to do is to start NGINX server.
Emperror Mode:
Edit /etc/rc.local and add:
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

Django - static/CACHE files not found

I'm using Django 1.10 + uWsgi + nginx.
In Prod mode the static files do not show up, this is what my nginx error-log tells me:
404 ... static/CACHE/js/e3a6fa7588d1.js" failed (2: No such file or directory),
The folder static/CACHE remains empty (so the 404's make sense), but why?
I already set the permissions to 775 for static/CACHE .
nginx conf:
# /etc/nginx/sites/available/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream bdjango {
server unix:///tmp/mysite.sock; # for a file socket
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name dev.mysite.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 8M; # adjust to taste
# Django media
location /media {
alias /var/www/mysite/src/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/mysite/src/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass adjango;
include /var/www/mysite/src/uwsgi_params; # the uwsgi_params file you installed
}
}
Django settings contains
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__) + "../../../")
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
So, why are my js/css files not in static/CACHE ?
EDIT:
solved it: uwsgi_pass was pointing at the wrong upstream location (which happened to be a different website):
uwsgi_pass adjango;
Are you using CACHE?
Have you defiened STATIC_URL and STATIC_ROOT correctly.
Is DEBUG = False?
Are you using any 3rd party tool to cache/pipline your static files?
It could be that django-compressor is failing.
Have you tried having DEBUG = True and COMPRESS_ENABLED = True in your settings and seeing if it works?
https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_ENABLED
Alternatively, I suggest having ADMINS setup in production so it emails you any errors, or setup sentry in your production setup.

Django uwsgi Nginx not serving media files

Edit: I've made some progress trouble shooting this issue. For an update on the situation, read the comments and view this post on ServerFault:
https://serverfault.com/questions/690836/django-uwsgi-nginx-not-serving-media-files-django-returns-404-status-code?noredirect=1#comment851441_690836
I'm following this tutorial:
http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
Everything was going well until I reached the "Basic nginx test" section. When I stopped and started nginx and then added "media.png" to my media folder and went to
192.168.***.***:8000/media/media.png
it gave me a 404 status code. Note that this status code was given by Django (DEBUG=True), so it says:
Page not found (404)
Request Method: GET
Request URL: http://192.168.***.***:8000/media/media.png
Using the URLconf defined in CMS.urls, Django tried these URL patterns, in this order:
This is mysite_nginx.conf:
# 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 192.168.***.***; # 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/a/Documents/media; # your Django project's media files - amend as required
}
location /static {
alias /home/a/Documents/CMS/CMSApp/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/a/Documents/CMS/CMS/uwsgi_params; # the uwsgi_params file you installed
}
}
Any idea why I'm getting a 404 error when trying to view the media file?
Additional information: When I go to
http://192.168.***.***:8001/media/media.png
or
127.0.0.1:8001
it says "This web page is not available".

Is it possible to run Django and Wordpress on same server with selective urls with apache?

Is it possible to run Django and Wordpress on the same server? I have seen other examples where its possible if / is handled by django and /blog/ by wordpress but how about
/api/ -- eg. api/v1/user/show
/signin/,
/create/,
/dashboard/ -- eg. dashboard/reports/
By Django and
/
/about/
/security/
/contact/
/tos/
/privacy/
/faq/
/contact/
etc with Wordpress?
Yes, it is possible as far as I know if you're using nginx and uwsgi, you can config nginx like this:
upstream django {
server unix:/path/to/your/django/project/your.sock; # for a file socket
}
server {
location /about{
proxy_pass http://127.0.0.1:8080;# where your wordpress run
}
location /secirity{
proxy_pass http://127.0.0.1:8080;# where your wordpress run
}
# other url needed to handle by wordpress
# left are all handled by django
location / {
uwsgi_pass django;
include /path/to/your/django/project/uwsgi_params; # the uwsgi_params file you installed
}
}

Nginx x-accel-redirec wrong redirects

I am serving images processed by my django server using the x-accel-redirec header and Nginx. However, the behavior is really odd as sometimes I get the image, and sometimes Nginx redirects the image URI to my Django (uwsgi) application and I get, of course, a 404. My config is as follows:
upstream django {
server unix:///tmp/django.sock;
}
server {
listen 80;
server_name website.dev;
# max upload size
client_max_body_size 75M; # adjust to taste
location /images/ {
internal;
alias /home/bob/images/;
}
location / {
uwsgi_pass django;
include /home/bob/django_website/uwsgi_params;
}
}
This behavior happens with the same URL when I refresh several times. When I get the 404 error message from Django it says: "The current URL, home/bob/images/image3.jpg, didn't match any of these."
In apache with mod_xsendfile it was working perfectly.
Please help