nginx serving from html instead of gunicorn - django

I have a nginx / gunicorn / django app I've set up following
https://medium.com/#_christopher/deploying-my-django-app-to-a-real-server-part-ii-f0c277c338f4
It works when I go to the main IP from the browser (I got Django tried these URL patterns, in this order: admin/...) but when I go to /admin I got a 404. Nginx logs are as follows:
2019/07/05 00:30:28 [error] 13600#13600: *1 open() "/usr/share/nginx/html/admin" failed (2: No such file or directory), client: 186.190.207.228, server: 127.0.0.1, request: "GET /admin HTTP/1.1", host: "128.199.62.118"
So it is trying to serve files from html/ instead of serving gunicorn, why?
Nginx config:
server {
listen 80;
server_name 127.0.0.1;
location = /favicon.ico {access_log off;log_not_found off;}
location = /static/ {
root /home/juan/site;
}
location = /media/ {
root /home/juan/site;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/juan/site/site.sock;
}
}

Remove the = from all the Location directives except the first. That means exact match, instead of the prefix match you want.
location = /favicon.ico {access_log off;log_not_found off;}
location /static/ {
root /home/juan/site;
}
location /media/ {
root /home/juan/site;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/juan/site/site.sock;
}

Related

Allowing post request through Nginx

Trying to serve a react app, django with nginx and docker. I am not able to make a post call from docker on port 8080 to nginx on port 8082.
Following is the error thrown by Nginx
iris-frontend-frontend-1 | 2022/10/13 20:16:18 [error] 35#35: *4 open()
"/usr/share/nginx/html/add-to-waiting" failed (2: No such file or directory), client:
172.18.0.1, server: localhost, request: "POST /add-to-waiting HTTP/1.1", host:
"localhost:8080", referrer: "http://localhost:8080/"
Nginx configurations are:
add-to-waiting is the api call.
upstream django {
server website:8000;
}
upstream react_app {
server frontend:8080;
}
server {
listen 80;
client_max_body_size 100M;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
proxy_pass http://react_app;
}
location /admin {
proxy_pass http://django;
}
location /add-to-waiting {
proxy_pass http://django;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /app/forex/static/admin/;
}
}
What configurations need to change to make frontend able to call the api?

how to add new django app to deployed django project (using nginx,gunicorn)?

While running the django project locally, I can access my home, admin, app1, app2 directory (i.e
localhost:portnum , localhost:portnum/admin ,
localhost:portnum/app1 , localhost:portnum/app2 )
The problem begins when I deployed the app in a server ( I used nginx and gunicorn for django deployment with the help of this guide )
Problem : -
I'm unable able to access example.com/admin, example.com/app1 , example.com/app2.
I'm able to access my home example.com anyway.
When I trying to access example.com/app1/ the page give an error 403 forbidden
2018/11/17 18:00:55 [error] 28459#28459: *8 directory index of "/home/ubuntu/project/app/" is forbidden, client: 172.68.146.88, server: example.com, request: "GET /events/ HTTP/1.1", host: "www.example.com"
2018/11/17 18:00:58 [error] 28459#28459: *13 open() "/usr/share/nginx/html/app" failed (2: No such file or directory), client: 172.68.146.10, server: example.com, request: "GET /events HTTP/1.1", host: "www.example.com"
Some solutions which I tried to follow before this question::-
Django: when trying domain.com/admin gives 404 with an old unexisting urls file
Nginx 403 error: directory index of [folder] is forbidden
My nginx config
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /home/ubuntu/certs/cert.pem;
ssl_certificate_key /home/ubuntu/certs/cert.key;
server_name example.com;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /static/ {
root /home/ubuntu/example_project/app1;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}
}
Thank You for trying to solve my problem.
When you use= in a location directive, it only applies for that exact path. Instead you should remove those for both of your locations and let nginx match for all prefixes.
location /static/ {
root /home/ubuntu/example_project/app1;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}

django, nginx, gunicorn 404 on any other page than the root

My issue is that whenever I try to view a webpage that isn't the root webpage aka /user/ nginx returns a 404 error and the error log states
"/usr/share/nginx/html/user/login/index.html" is not found.
current nginx configuration
server {
listen 80;
server_name ip_address;
location = /static {
root /opt/scrumban/kanbanboard/;
autoindex off;
}
location = / {
include proxy_params;
proxy_pass http://unix:/opt/scrumban/kanbanboard/kanban.sock;
}
}
Remove those = signs; they mean that only the paths "/" and "/static" are matched. Without the symbol they match as prefixes, which is what you want.

Django server - 502 error bad gateway

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.

django with gunicron and nginx

I have developed django application and now deploying this on freebsd server using gunicron and nginx. I have added guncorn in projects settings files under installedapps. and placed gunicorn.conf.py in root as:
import os
def numCPUs():
if not hasattr(os, "sysconf"):
raise RuntimeError("No sysconf detected.")
return os.sysconf("SC_NPROCESSORS_ONLN")
user = root
workers = numCPUs() * 2 + 1
bind = "127.0.0.1:8000"
pidfile = "/tmp/gunicorn-srv.pid"
backlog = 2048
logfile = "/data/logs/gunicorn_srv.log"
loglevel = "info"
Then in nginx.conf updated server tag with:
listen 80;
server_name localhost;
access_log /var/log/nginx/nginx-access.log;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8000;
#root /usr/local/www/nginx;
#index index.html index.htm;
}
location /media/ {
root /data/webs/uni_rate;
}
location /static/ {
root /data/webs/uni_rate;
}
Now I have started nginx and guncorn. If I brows my public ip page is shown but static files are not served. If I directly hit
http://myip/static/folder/file.css
then 403 error is returned. Any Idea what could be the reason or what else information should be provided to diagnose it.
My media and static directories are respectively:
/data/webs/uni_rate/media
/data/webs/uni_rate/static
here is an output of nginx-access.log
182.178.6.248 - - [01/Apr/2013:11:14:54 -0500] "GET /static/bootstrap_toolkit_extras/css/bootstrap.css HTTP/1.1" 403 168 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv
:16.0) Gecko/20100101 Firefox/16.0"
182.178.6.248 - - [01/Apr/2013:11:15:00 -0500] "-" 400 0 "-" "-"
Have you checked the read permission of all of the parent folders of /data/webs/uni_rate/? Nginx user has to have read access to all of them. For example, /data, /data/webs, /data/webs/uni_rate.
Try to replace your location for static and media with something like this:
location /static {
alias /var/www/domain1/media/;
autoindex on;
access_log off;
}
The trick is in 'alias' instead of 'root'.