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
Related
I am trying to deploy my django app on google cloud platform. I am using nginx and gunicorn. I am following this guide.
I have created a file - le_website - under the folder sites-available. This is the code -
server {
listen 80;
server_name 10.xxx.x.x;
location = /favicon.ico {access_log off;log_not_found off;}
location = /static/ {
root /home/jainpriyanshu1991/learnEarn/le-webiste;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/jainpriyanshu1991/learnEarn/le-webiste/le_website.sock;
}
}
When I try the url myIPaddress/static/ , it works and shows the folders inside it. But it does not work for any subfolder within static. It gives /usr/share/nginx/html/static/img/index.html is not found for img folder inside static. Similarly, when I try the url myIPaddress/ it opens the homepage of website but again, it does not work for any other link and gives error. For about page it gives error /usr/share/nginx/html/about failed (2: No such file or directory).
There is an error in the tutorial you were following. location = ... exactly matches a single URI, whereas you need to match all URIs below /static/.
Use:
location = /favicon.ico { ... }
location /static/ { ... }
location / { ... }
See this document for details.
I am trying to serve protected media files with Nginx sendfile and X-Accel-Redirect using Django 2.0.
This is my Nginx configuration:
server {
listen 8000;
server_name localhost;
charset utf-8;
sendfile on;
# Protected media
location /protected {
internal;
alias /Users/username/Documents/sat23/venv/media/;
}
# Django static
location /static {
alias /Users/username/Documents/sat23/venv/static/;
}
# All other requests.
location / {
uwsgi_pass django;
include /Users/username/Documents/sat23/venv/uwsgi_params;
}
}
Then in my urls.py I added a simple view that should serve my media files (I'll configure permissions later):
def serveMedia(request):
url = request.path.replace('media', 'protected')
response = HttpResponse('')
response['X-Accel-Redirect'] = url
response['Content-Type'] = ''
return response
urlpatterns += [
path('/media/', serveMedia, name='protected_media')
]
However whenever I call localhost:8000/media/users/user35.jpg, I just get a Django (not nginx) 404 page, saying that Django tried all the configured paths and it couldn't find the requested one.
So I had a suspicion that my view just doesn't work. I then rewrote it like this:
def serveMedia(request):
return HttpResponse(content=b'Hello there')
And sure enough it doesn't get called. But I have no idea why. Could someone help me out?
P.S. Any recommendations on configuring nginx conf are also very welcome!
There are no capturing groups in your Django path call there, so that will only match /media/ verbatim.
You might want to use the old-school url route with a regexp like
urlpatterns += [
url(r'/media/.+', serveMedia),
]
to capture everything that starts with /media/
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.
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 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
}
}