Nginx server not using project config - django

I am trying to create a configuration for the Nginx web server for my Django project. I would like to store the configuration of the web server in my project files.
As I am using MacOS, the /sites-enabled directory was not present in the /usr/local/etc/nginx directory, so I've created it. In the /usr/local/etc/nginx/sites-enabled directory I have made a symbolic link to the project which seems to work.
In the nginx.config file in the /usr/local/etc/nginx directory I have updated the file to make it see the sites-enabled directory:
http {
include /usr/local/etc/nginx/sites-enabled/*;
The nginx -t command lists the following if I input incorrect data in my project nginx.conf file:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
and correctly recognizes changes to it if I make some intentional errors:
nginx: [emerg] invalid parameter "server_name" in /usr/local/etc/nginx/sites-enabled/linkem-all:6
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
The problem is that despite my changing the port to listen to in the nginx.conf file located in my project:
server {
# port nginx will be listening to
listen 8000;
# server ip address
server_name 127.0.0.1.;
# logs stored within the project
access_log path/to/nginx-acces.log;
error_log /path/to/nginx-error.log;
# pass traffic from nginx to the gunicorn server
location / {
proxy_pass http://127.0.0.1:5432;
}
# static files location served with alias for explicit path
location /static {
alias ./static;
}
}
after running the nginx server I get the error caused by nginx trying to bind to port 8080, its default setting.

Related

How to increase file upload size in Django application running on EC2 instance with Nginx as revese Proxy?

I've a Django application that is running on EC2 instance. It is uploading file upto 100MB without any problems, but above 100MB file size, it gives error 413 Request Entity Too Large.
I've tried in file /etc/nginx/sites-available/default under server configuration.
client_max_body_size 10G;
I've also applied the same configuration in my domain configuration files, but all in vein. This configuration works well in my other servers, where I am running php applications.
Note: I've used gunicorn with supervisor for running Django application.
The client_max_body_size has to be defined in both http and https as stated in this answer.
So your nginx.conf file under /etc/nginx/sites-available/default would look something like:
http {
server {
...
listen 80;
server_name xxxx.net;
client_max_body_size 10G;
}
server {
...
listen 443 default_server ssl;
server_name xxxx.net;
client_max_body_size 10G;
}
}
#Helge thanks, I've applied the configuration parameters in both of them.
The issue is resolved now. The issue was, initially it was throwing error due to configuration parameter defined below other parameters, after resolving that Cloudflare started throwing 413 error. So, my team lead made configuration changes for the same.
in you 01__django.config add
files:
"/etc/nginx/conf.d/proxy.conf" :
mode: "000755"
owner: root
group: root
content: |
client_max_body_size 20M;
#use These
or go to the your root via ssh
pass the commands
cd ..
cd ..
now use :
sudo nano /etc/nginx/nginx.conf
add :
client_max_body_size 20M; # these will add 20mb of size to your request body
Now Reload Your NGINX Server
using nginx -s restart

Django Deployment with nginx - gunicorn from another server

I'm trying to deploy my Django Project through different application server : Apache2, Nginx/gunicorn, ...
It works fine with Apache2 or Nginx, but I would like to dissociate these application server with my Django Projet. I would like to get an environment like this :
Server 1 (172.30.10.92) : Django Project & wsgi
Server 2 (172.30.10.93) : Nginx/gunicorn
Why ? Because later I will have some Django applications, but I would like to use just one application server.
I think I'm making a mistake with my configuration files syntax.
I have in /etc/nginx/sites-available/DatasystemsCORE.conf :
server {
listen 8000;
server_name 172.30.10.92;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root http://172.30.10.92:/var/www/html/;
}
location / {
include proxy_params;
proxy_pass http://172.30.10.92/unix:/var/www/html/DatasystemsCORE/dscore.sock;
}
}
server_name : Is it IP adress from Django server or nginx server ? I think it's the first one but I'm not sure.
proxy_pass : I think there is an issue in my path
Then, I'm executing this command :
gunicorn --daemon --workers 3 --bind 172.30.10.92/unix:/var/www/html/DatasystemsCORE/dscore.sock 172.30.10.92:/var/www/html/DatasystemsCORE/DatasystemsCORE.wsgi
One more time, I thing there is a syntax problem because I'm getting 502 Bad Request
How I can add IP address from a distant Django server there ?
Thank you !
I found How to solve my issue and I made a tutorial (based on my case) in order to help everyone who would like to make the same thing.
My file is there : Download tutorial file
But this is the same tutorial written in English.
My Django IP server : 172.30.10.92
My Nginx IP server : 172.30.10.93
1- Install and Configure wsgi (located on Django server)
WSGI is a file created with your Django project.
The file is located in /path/to/your/project/Myproject/wsgi.py
We have to edit this file like this :
import os
from django.core.wsgi import get_wsgi_application
import sys sys.path.append('/var/www/html/Myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Myproject.settings")
application = get_wsgi_application()
2- Install and Configure gunicorn/supervisor (located on Django server)
In order to install gunicorn/supervisor, you have to execute in your shell :
pip install gunicorn
pip install supervisor
Then, you have to create a new file in /etc/supervisor/conf.d/Myproject.conf which looks like this :
[program:Myproject]
command = /home/valentin/.virtualenvs/MyprojectEnv/bin/gunicorn Myproject.wsgi:application --name "Myproject" --workers=4 --bind=0.0.0.0:8080 -- user="valentin" --group="valentin" ; Command to start app
user = username #You have to replace by your username
stdout_logfile = /var/log/supervisor/supervisor.log
redirect_stderr = true
log
environment=LANG=fr_FR.UTF-8,LC_ALL=fr_FR.UTF-8
I specified port 8080 which is the communication port between my application server and my web server.
3- Edit hosts file on nginx server (located on nginx server)
You have to edit your hosts file located to /etc/hosts and add a new entry to your Django server :
127.0.0.1 localhost
127.0.1.1 valentin
172.30.10.92 Myproject
# The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
4- New config file in nginx repository (located on nginx server)
This new file should be placed in /etc/nginx/sites-available/Myproject.conf
server {
listen 8080;
server_name Myproject;
root /var/www/html/Myproject/;
location /static/ {
root /var/www/html/;
}
location / {
include proxy_params;
proxy_pass http://172.30.10.92:8080;
}
}
The IP address corresponds to my Django server address. I specified the listen port (8080), the path to my Django project & static directory.
Then, you have to create a symbolic link to sites-enabled.
After ths operation, restart nginx service :
sudo service nginx restart
5- Allow nginx IP address in Django (located on Django server)
You have to edit your settings.py file in order to allow nginx IP address in ALLOWED_HOSTS :
ALLOWED_HOSTS = ['localhost', '172.30.10.93', '127.0.0.1', '[::1]']
6- Finally execute gunicorn (located on Django server)
Finally, you have to start gunicorn. You should be inside your Django root project and execute :
gunicorn Myproject.wsgi:application --bind 172.30.10.92:8080
Now, in your browser, try to connect to your nginx server with the port :
http://172.30.10.93:8080
It works !
PS : This tutorial works for me, if it doesn't work for you, maybe I missed something, or maybe you didn't make exactly like me ;)
What you're trying to do doesn't make any sense. You can certainly use a single nginx on a separate server, but gunicorn is the application server and belongs with the application.
There is no "Django server" so your first question is irrelevant. gunicorn will need to serve on a port, not a socket, and the nginx proxy_pass will point to that port.

my nginx cannot load uwsgi on Ubuntu 16.04

trying to run django app "mysite" through uwsgi with nginx on Ubuntu 16.04, but when I start uwsgi and check in my browser, it just hangs.
i set django upstream socket to on port 8002 and nginx to listen on 8003. In the browser i visit 192.168.0.17:8003 prior to running uwsgi and it throws 502 which is expected, so I start uwsgi with
uwsgi --http :8002 --module mysite.wsgi --logto /tmp/uwsgi.log --master
and 8003 now hangs when I reload in the browser. I looked through /var/log/nginx/error.log but it's blank (so is access.log).
Here is nginx config, which is symlinked to /etc/nginx/sites-enabled:
sudo nano /etc/nginx/sites-available/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:8002; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8003;
# the domain name it will serve for
server_name 192.168.0.17; # 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/myusername/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/myusername/uwsgi-tutorial/mysite/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/myusername/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
I know that Django is running because in my app's settings.py I have ALLOWED_HOSTS = ['192.168.0.17','localhost','127.0.0.1'] and when I visit port 8002 in the browser I get the django "Congratulations!" page. And when I remove 192.168.0.17 from ALLOWED_HOSTS, django still runs on that machine from localhost or 127.0.0.1, so this seems that it must be something to do with how ngnix and uwsgi are talking to each other.
Any ideas??
It turns out systemd does not like lines in config files to be too long. I removed a couple long comments in /etc/systemd/system/uwsgi.service, restarted uwgsi service and all is well.
I found this out by running sudo journalctl -u uwsgi and finding the following error:
[/etc/systemd/system/uwsgi.service:5] Unbalanced quoting, ignoring: "/bin/bash -c 'mkdir -p /run/uwsgi; chown myusername:myusern
In researching Unbalanced quoting, found this git issue regarding maximum file line length.

404 not found error on Nginx

I'm very new to AWS and Nginx.
When I get an access to my elastic ip address for web server EC2 instance, I get a 404 not found error.
How come it shows 404 not found error although I have uploaded files to the directory I set as root directory in nginx.conf file?
After I edited the nginx.conf file, I did reload nginx as well.
On EC2
$ ls sites/
LICENSE README.md css fonts index.html js
nginx.conf
events {}
http {
server {
listen 80;
server_name MY_IP_ADDRESS_HERE;
root /sites/;
}
}
Additional Information
I get a default nginx page when I set the root directory to "/usr/share/nginx/html/" in nginx.conf file, so the server is working ok.

nginx unknown directive "upstream"

I'm using nginx as a proxy server to forward requests onto my gunicorn server. When I run sudo nginx -t -c /etc/nginx/sites-enabled/mysite I get the following error.
[emerg]: unknown directive "upstream" in /etc/nginx/sites-enabled/mysite:1
configuration file /etc/nginx/sites-enabled/mysite test failed
Any idea how to fix it? This is my nginx config:
upstream gunicorn_mysite {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name example.com;
access_log /usr/local/django/logs/nginx/mysite_access.log;
error_log /usr/local/django/logs/nginx/mysite_error.log;
location / {
proxy_pass http://gunicorn_mysite;
}
}
I'm running Ubuntu 10.04 and my nginx version is 0.7.65 which I installed from apt.
This is the output when I run nginx -V
nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-http_gzip_static_module --with-http_realip_module --with-mail --with-mail_ssl_module --with-ipv6 --add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair
When you tell nginx to load that file directly, it starts at the global context. The upstream directive is only valid in the http context. When that file is included normally by nginx.conf, it is included already inside the http context:
events { }
http {
include /etc/nginx/sites-enabled/*;
}
You either need to use -c /etc/nginx/nginx.conf or make a small wrapper like the above block and nginx -c it.
I am using nginx version: nginx/1.4.1 on EC2 Ubuntu. I was getting this error:
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/nginx.conf
To get my instance running I had to wrap the upstream and server sections in an http { } section. It then complained about missing event section. So I added that as follows:
events {
worker_connections 1024;
}
It worked fine after those fixes, this is my first effort so I was guessing my way through.
Turns my nginx config was ok. The problem was with my gunicorn server was not running properly.