django with gunicron and nginx - django

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

Related

AWS ELB keeps returning 404 Health checks failed with these codes: [404]

I'm serving my node js application with pm2.
nginx.conf file is
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
and inside the **conf.d/**folder I have create configuration file like below (with the name of api.conf)
server {
listen 80;
server_name dev.xxxxxxx.com;
underscores_in_headers on;
# return 301 https://$host$request_uri;
location / {
proxy_pass http://localhost:3004/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
when I stop the node application nginx server the ELB Health status is 502 and start when I start the nginx server then nginx server the ELB Health status is 404
I'm not able to find what is the issue. Could any one please help me to understand and solve
even I checked nginx error logs as below
[02/Feb/2021:17:21:29 +0000] "GET / HTTP/1.1" 502 157 "-" "ELB-HealthChecker/2.0" "-"
[02/Feb/2021:17:21:38 +0000] "GET / HTTP/1.1" 404 60 "-" "ELB-HealthChecker/2.0" "-"
When setting up a health check, the ALB does NOT include a HOST: header value.
If you modify the log you will see that the ELBHealthCheck requests come from a private IP address.
So when nginx is running, it gets a request from a private IP, which does not match any server elements. Since you don't have a default_server set, it defaults to the first one, which in this case will be the proxy to your node app.
Are there any documents in /usr/share/nginx/html? Try putting an index.html in there, and adding a default_server to the port configuration:
listen 80 default_server;
For rest endpoint users, you can define an endpoint for health check.Return status code 200 in response.
Firstly we need to configure Health check end point in AWS.
EC2-->Load Balancing-->Target Groups-->Select Target Group-->Actions-->Edit Health check Settings.
Define a health check path like /health.
save changes.
Now create an endpoint /health in your application.I did it in sprinboot so pasting same for reference.
#CrossOrigin(value = "*")
#RequestMapping(value = "/health",method = RequestMethod.GET)
public ResponseEntity<?> health() throws Exception {
try {
return ResponseEntity.status(200).body("Ok");
} catch (Exception e) {
return (ResponseEntity<?>) ResponseEntity.internalServerError().body(e.getMessage());
}
}
I'm too late here but still adding if someone gets the same issue. My Setup is
AWS ALB --> AWS EC2 Installed Nginx on EC2 and getting the same issue as above:
"GET /var/www/html/index.nginx-debian.html HTTP/1.1" 404 134 "-" "ELB-HealthChecker/2.0"
The mistake I did was I have added the full path in the Health checks tab as /var/www/html/index.nginx-debian.html and due to this, I was getting the above error.
Solution: Only need to add /index.nginx-debian.html in the Health Checks tab and it will start getting 200s.
Hope it helps some one and get an Upvote :-)

nginx serving from html instead of gunicorn

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;
}

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;
}

Nginx doesn't serve static files

I've just deployed my django website on an Ubunto server. But it doesn't load any static file.
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),]
STATIC_ROOT = 'staticfiles'
nginx/sites-available configuration:
server {
server_name 138.197.172.33;
access_log off;
location /staticfiles/ {
root staticfiles;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Errors:
...
[14/May/2018 05:28:28] "GET /static/vendors/underscore/underscore.js
HTTP/1.1" 404 113
[2018-05-14 05:28:29,252] base: WARNING - Not Found:
/static/vendors/bootstrap-notify-simple-wrapper/bootstrap-notify-
simple-wrapper.min.js
[14/May/2018 05:28:29] "GET /static/vendors/bootstrap-notify-simple-
wrapper/bootstrap-notify-simple-wrapper.min.js HTTP/1.1" 404 159
[2018-05-14 05:28:29,690] base: WARNING - Not Found:
/static/js/custom.js
[14/May/2018 05:28:29] "GET /static/js/custom.js HTTP/1.1" 404 93
[2018-05-14 05:28:30,236] base: WARNING - Not Found:
/static/js/converter.js
[14/May/2018 05:28:30] "GET /static/js/converter.js HTTP/1.1" 404 96
...
staticfiles folder includes:
admin ajaximage css datatable dm2bn fonts images js static
tinymce vendors
Looking at the error logs, the link is pointing to /static/:
[14/May/2018 05:28:28] "GET /static/ vendors/underscore/underscore.js
HTTP/1.1" 404 113
In your configuration, you have set location to /staticfiles, but your django configuration is saying the URL is /static.
So, location should be /static/, not /staticfiles/.
location is the URL, and the root is the full path to the staticfiles directory.
you should change nginx configuration like
location /static/ {
alias /path/to/staticfiles;
}
You should give absolute path in your nginx configuration.
Make sure all your static files are present in path /static of your project directory.
Once you deploy your app on the server don't forget to run :
python manage.py collectstatic --link
--link will create a symbolic link to each file into the directory /staticfiles.
Nginx config should be :
location /static/ {
alias /path/to/staticfiles;
}

Setup pretty local development URLs with nginx on Mac OS X

I have a Django server setup that I can access from 127.0.0.1:8000
I have just installed nginx on to Mac OS X Yosemite and can access the 'Welcome to nginx' screen at localhost:80
I want to be able to set up a local development environment on local.ingledow.co.uk but when I visit that URL, it redirects me to my live site: david.ingledow.co.uk.
I did have a wildcard URL forwarder with DNSimple that would redirect like this:
*.ingledow.co.uk > david.ingledow.co.uk
but I have removed that.
I have added this CNAME: local.ingledow.co.uk > 127.0.0.1
This shouldn't affect how I setup my local nginx servers.
So, how do I create an nginx server on Mac that shows my 127.0.0.1:8000 Django project on to a custom domain (local.ingledow.co.uk).
This is my nginx setup:
Default Nginx config at /usr/local/etc/nginx/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# INCLUDES
include /Users/dingledow/etc/nginx/*.conf;
}
ingledow.conf at /Users/dingledow/etc/nginx/ingledow.conf
server {
listen 80;
server_name local.ingledow.co.uk;
location / {
root /Users/dingledow/Documents/Work/Ingledow/git/my_site2/;
index index.html index.htm index.php;
autoindex on;
}
}
I'm not sure if Apache is conflicting with nginx? Or whether I've just setup nginx incorrectly?
Setting a CNAME with your registrar isn't going to help at all, and you should remove that.
The only way to make this work is to edit your local HOSTS file at /etc/hosts and add a pointer to the localhost:
127.0.0.1. local.ingledow.co.uk
Then you'll either need to run the dev server on port 80 instead of 8000, or configure nginx to run your site via gunicorn or uwsgi - the documentation tells to how to do that.