Django + NGINX 404 Not Found - django

Whenever I run server using gunicorn --bind command, all static files load without any error.
but when I try to access my domain It gives 404 error.
Django
STATIC_ROOT = '/var/www/html/static'
STATIC_URL = '/static/'
Nginx
/etc/nginx/sites-available/default
server{
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 65.1.254.234;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Application_Code/app.sock;
}
location /static {
autoindex on;
alias /var/www/html;
}
}
Is there any problem happening due to .env file?

Related

nginx cannot find files in /home/ubuntu/

I don't understand why nginx cannot find files when the root is under home directory, e.g. /home/ubuntu/tabs. It will give 404 error.
It works fine when the root is /var/www/tabs.
server{
listen 80;
#root /var/www/tabs;
root /home/ubuntu/tabs;
server_name 18.191.229.199;
index main.html ;
location / {
try_files $uri $uri/ =404;
}
location /folder {
try_files /folder/main1.html /folder/main2.html =404;
}
}

When I deployed django project with nginx+uwsgi, there was always a "502 bad gateway error". What is the reason?

When I enter my IP in the browser, the page will appear 502 Bad Gateway. Is this my configuration file wrong?
/etc/nginx/sites-enabled/fefault:
server {
listen 80;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name 127.0.0.1;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass 47.101.157.128:8000;
}
location /static{
alias /var/www/sscc2019/static/;
}
/home/sscc/sscc2019/uwsgi.ini:
[uwsgi]
socket=127.0.0.1:8000
chdir=home/sscc/sscc2019
wsgi-file=sscc2019/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uswgi.log
module=sscc2019.wsgi:application
vacuum=true
virtualenv=/home/ssccenv

Nginx not serving files from /var/www/html after deploying Django app

I have a Django application stored at /var/www/w_gm .
Now I have used Nginx + Gunicorn to deploy it.
My default conf file at
root#dev:/etc/nginx/sites-enabled# ls
default w_gm
Default conf file :
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
w_gm conf file :
server {
listen 80;
server_name 119.00.00.100;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
}
Now the issue is that, when am tying in my IP address i.e, it's redirecting me to the Django app and it's working perfectly fine. But my other files which are in var/www/html doesn't get served i.e if I have <ip.addr>/work1 , it gives me error.
Now if I edit the w_gm conf file and add a suffix, let's say server_name 119.00.00.100/abc; , which obviously is wrong, my /var/www/html files start working.
I need a solution where if I type in <ip.addr>/something, then it should redirect to Django app, else serve the files which are in var/www/html.
You currently have two servers, but it sounds like you only want one server.
<ip.addr>/something is ambiguous, so you need Nginx to look for files in one root and send requests to the proxy only if they are not found.
You would need to combine your two server blocks into something like this:
root /var/www/html;
location / {
try_files $uri $uri/ #proxy;
}
location ~ \.php$ {
...
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location #proxy {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
This assumes that the /static/ URI is unambiguous, and that no URIs ending with .php are sent to the proxy. See this docuemnt for more.

How to have Nginx to proxy pass only on "/" location and serve index.html on the rest

I have a web app that uses Django for the backend and some frontend and ReactJS for stricly the frontend. I am setting up my Nginx configuration and I am trying to get Nginx to proxy_pass only on the "/" location and then on the rest of the locations I want it to serve the index.html file from React.
Here is my current Nginx configuration under sites-available. So the only url that does not have the prefix "/home/" or "/app/" is the homepage. I want Django to serve the homepage and then ReactJS to serve the rest.
server {
listen 80;
root /home/route/to/my/ReactJS/build;
server_name www.mydomain.com;
location = /favicon.ico { log_not_found off; }
location / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location /home/ {
try_files $uri $uri/ /index.html;
}
location /app/ {
try_files $uri $uri/ /index.html;
}
}
Let me know if you need any more details or if I could clear things up.
Thanks!
Just change it to the following: (replacing the last three location blocks)
location = / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location / {
try_files $uri $uri/ /index.html;
}
The location = / only matches the exact domain, everything else will be matched by location /.

Nginx - Django i serve perfect static files but not media files

(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.