(Sorry for my bad english)
I'm having a peculiar problem... I configure Django and Nginx all perfect, my site shows perfect all static files are loaded perfect to. But..... Nginx is not serving the media files, i check my configurations and all seems to be ok. But i always get the 404 error and the route is the same that static files but with the media word
this is a static file route
http://project.com/static/css/custom.css
And this a media file
http://project.com/media/stores/logos/solutions_logo_rdWRcqQ.jpg
This is the nginx config
server {
listen 80;
server_name project.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username;
}
location /media/ {
root /home/username;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/username/project.sock;
}
}
And this are the django settings
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT=os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'media/')
Any idea???
A guess into the wild. Your static files are served directly by django and not by your nginx. (You can test that by removing the location /static { ... } section and restart your nginx.)
Then to fix your problem please try this:
server {
listen 80;
server_name project.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/username;
}
location /media/ {
alias /home/username;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/username/project.sock;
}
}
Also, dont forget to restart your nginx afterwards.
Related
I am getting an error with static files in nginx
custom_main.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
THis is my nginx config:
server {
listen 80;
server_name 94.154.13.214;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/django_direct/main_app/;
}
location / {
include proxy_params;
proxy_pass unix: /root/django_direct/django_direct.sock;
}
}
What do I do?
upd: settings.py:
STATIC_URL = '/static/'
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, 'main_app/static')
# ]
STATIC_ROOT = os.path.join(BASE_DIR, 'main_app/static')
family, Im having a little trouble to make nginx server load static file collected by django. here is my nginx sites-available
server {
listen 80;
server_name <my_ip_address>;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
}
location /asset/ {
autoindex on;
alias /var/www/html/dev2_assets/;
}
}
Down here is my Django STATIC_URL and STATIC_ROOT configurations
STATIC_URL = '/assets/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "assets"),
)
STATIC_ROOT = '/var/www/html/dev2_assets/'
When i run the application with ./manage.py runserver its loads all the static files. Any help. Thanks
Your problem is your Location.
Your not specifying a root for it, also in your settings.py your declaring assets but in your location your declaring asset with a missing s. try changing it to something like this:
location /assets/ {
autoindex on;
root /var/www/html/dev2_assets;
}
Also for debugging purposes try added this above location:
error_log /var/log/nginx/error.log;
Then you will get a specific error message about it not being able to retrive your static files.
Lastly are you sure your utilyzing nginx, django, and gunicorn correct?
Here is a copy of my site file for comparison:
# This redirects all incoming traffic on port 80 to 443
server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/domain.com.chained.crt;
ssl_certificate_key /etc/ssl/domain.com.key;
server_name helius.dk;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
#location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/projectname/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/projectname/gunicorn.sock;
}
}
I have a centOS with a virtualenv, i can use the project in my localhost , but when the project is upload to a server give an error :
502 - bad gateway
I think the problem probably is in my nginx file.
server {
listen 80;
server_name www.site.com.br site.com.br;
root /var/www/html/agrodez/src/;
if ($http_host != "www.site.com.br") {
rewrite ^ http://site.com.br$request_uri permanent;
}
location /static/ {
alias /var/www/html/site/src/sistema/static/;
}
location /{
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
}
I don't know much about django, but recently I had to help a team with server configuration, and I've used the following nginx virtualhost configuration:
upstream django.local {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name site.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://django.local/;
}
location /static {
autoindex on;
# this line would be STATIC_ROOT dir
alias /var/www/html/agrodez/src/staticfiles;
}
location /media {
autoindex on;
# this line would be MEDIA_ROOT dir;
alias /var/www/html/agrodez/src/media;
}
# If you want nginx logs, then can add these
error_log /var/log/nginx/site.com_error.log;
access_log /var/log/nginx/site.com_access.log;
}
You can give it a try.
Hi I am trying to serve statics using Django and Nginx on a VPS to get my project live. My settings.py includes following.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
My project folder includes:
static
----static
----static-only
----media
----templates
In Nginx I got following:
upstream gunicorn_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 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80;
server_name example.com;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/projectuser/logs/nginx-access.log;
error_log /home/projectuser/logs/nginx-error.log;
location /static/ {
alias /venv/project/static/static;
}
location /media/ {
alias /venv/project/static/media;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://gunicorn_server;
break;
}
}
}
I can't serve static no matter what and admin panel css is not showing up.
Please advise asap.
Try changing static and media blocks this way:
location /static/ {
root /venv/project/static;
}
location /media/ {
root /venv/project/static;
}
This way, the http://example.com/static/admin/css/dashboard.css will be searched as /venv/project/static/static/admin/css/dashboard.css on the filesystem.
P.S. Also instead of if (!-f $request_filename) it's better to use try_files: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
I'm new at Nginx, I've successfully bound my django project to Nginx. However I can't serve my static files and I guess I set my media folder's location wrongly. Here is my file tree:
root_directory
my_django_project
...
manage.py
app1
app2
media
admin
css
js
...
And my nginx.conf goes like :
server {
listen 192.168.1.9:80;
server_name localhost;
# site_media - folder in uri for static files
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off; # po co mi logi obrazków :)
expires 30d;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log;
}
}
When I open my admin page, all css pages give 404 error. Can you tell me that how can I set my media path correctly ?
Here's a example of how I have my nginx servers setup
server {
server_name example.com www.example.com;
location /static {
autoindex on;
alias /home/myusername/myproject/static/;
}
location /media {
autoindex on;
alias /home/myusername/myproject/media/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)
The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.
This ServerFault question may help.
You have set this:
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
This means that the location will be xxx/media/media. Nginx will go to [...]/portal/media and look for a folder called media.
It should rather be like this:
location /media/ {
root /home/nazmi/workspace/portal;
}
Also you should remove the trailing slash.
If the media file isn't serving, try to set media location as below in the conf file inside the sites-enabled directory. This works for me.
location /media {
root /home/username/projectname/;
The following code works for me:
server {
server_name example.com www.example.com;
location /static {
autoindex on;
**alias /home/myusername/myproject/;**
}
location /media {
autoindex on;
**alias /home/myusername/myproject/;**
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I bold the different parts according to the previous answer.
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
Try this code in nginx config to serve static and media files for a django applications (without autoindex on settings)