I have Django FastCGI/nginx working nicely on OS X. I start my FastCGI process from the terminal like so:
python manage.py runfcgi host=127.0.0.1 port=8000 --settings=settings
in my nginx.conf file I have:
server {
listen 8080;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:8000;
I have nginx starting automatically using launchd as detailed here.
My question is: what is the correct approach to getting the FastCGI process to start automatically when the machine boots?
Thank you!
I would suggest using wsgi in this case, given that the wsgi process is fired by nginx vs it being a "stand-alone" process. A typical nginx and wsgi config:
myproject.wsgi:
#!/usr/bin/python2.7
import djangorecipe.wsgi
application = djangorecipe.wsgi.main('settings', logfile='/var/log/django/myproject.wsgi.log')
myproject.nginx.conf:
server {
listen 80;
server_name myproject.com;
# static resources
location ^~ /static/ {
alias /var/www/myproject/static/;
expires 31d;
}
wsgi_var REQUEST_METHOD $request_method;
wsgi_var QUERY_STRING $query_string;
wsgi_var CONTENT_TYPE $content_type;
wsgi_var CONTENT_LENGTH $content_length;
wsgi_var SERVER_NAME $server_name;
wsgi_var SERVER_PORT $server_port;
wsgi_var SERVER_PROTOCOL $server_protocol;
# additional variables (not used if unavailable)
wsgi_var REQUEST_URI $request_uri;
wsgi_var DOCUMENT_URI $document_uri;
wsgi_var DOCUMENT_ROOT $document_root;
wsgi_var SERVER_SOFTWARE $nginx_version;
wsgi_var REMOTE_ADDR $remote_addr;
wsgi_var REMOTE_PORT $remote_port;
wsgi_var SERVER_ADDR $server_addr;
wsgi_var REMOTE_USER $remote_user;
location / {
client_body_buffer_size 50;
wsgi_pass /var/www/myproject/myproject.wsgi;
wsgi_pass_authorization off;
wsgi_script_reloading on;
wsgi_use_main_interpreter on;
}
# logging
access_log /var/log/nginx/myproject/access.log;
error_log /var/log/nginx/myproject/error.log;
}
Related
I am trying to run my site, when i run
uwsgi --socket 0.0.0.0:8080 --protocol=http --chdir /opt/virtualenv/landivarpj/ --wsgi-file /opt/virtualenv/landivarpj/landivarpj/wsgi.py
i can access 192.xxx.xxx.xxx:8080 fine and my test text appears,
but if i try to go trought 192.xxx.xxx.xxx i only get an nginx welcome page,
and if i try to go in trought my domain www.xxxxxxxx.com, then it doesnt work at all.
in my project folder(opt/virtualenv/landivarpj) i have a uswgi_params file with
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
then in (opt/virtualenv/landivarpj/landivarpj) the wsgi.py is
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "landivarpj.settings")
application = get_wsgi_application()
in etc/nginx/sites-available is have drlandivar.conf
upstream django {
server 192.xxx.xxx.xxx:8080; # for a web port socket (we'll use this first)
}
server {
listen 80;
server_name drlandivar.com www.drlandivar.com;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /opt/virtualenv/landivarpj/media;
}
location /static {
alias /opt/virtualenv/landivarpj/static;
}
location / {
uwsgi_pass django;
include /opt/virtualenv/landivarpj/uwsgi_params;
}
}
the site-available and site-enable are linked
what have i done wrong why it work on 192.xxx.xxx.xxx:8080
but not trought the domain and nginx
***** new edit as recommended on nginx/availables-site/drlandivar.conf
upstream django {
server 192.254.145.207:8080;
}
server {
listen 80;
server_name drlandivar.com www.drlandivar.com;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /opt/virtualenv/landivarpj/media;
}
location /static {
alias /opt/virtualenv/landivarpj/static;
}
location / {
proxy_pass http://django;
}
}
it still give me same problem page only load trought 192.xxx.xxx.xxx:8080 not trought drlandivar.com
please help
You need to proxy_pass instead. The following configuration should do the trick.
upstream django {
server 192.254.145.207:8080;
}
server {
...
location / {
proxy_pass http://django;
}
}
Comparing yours to a working example of mine, one thing stands out. Try
...
location / {
include uwsgi_params;
proxy_pass http://django;
}
Adding --logto /var/log/uwsgi/uwsgi.log to your uwsgi invocation (after creating /var/log/uswgi and making it writeable to the uwsgi process) might shed some additional light.
I was given django project from another developer. It based on windows server(Django+fastcgi+nginx).
In settings.py present option DEBUG=True but the anyone of the errorlog-files does not contain debugging information
nginx.conf
worker_processes auto;
error_log C:/PATH_TO_PROJECT/nginx/logs/error.log;
error_log C:/PATH_TO_PROJECT/nginx/logs/error.log notice;
error_log C:/PATH_TO_PROJECT/nginx/logs/error.log info;
error_log C:/PATH_TO_PROJECT/nginx/logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
client_max_body_size 32m;
error_log C:/PATH_TO_PROJECT/nginx/logs/db-rlocalhost.error_log;
error_log C:/PATH_TO_PROJECT/nginx/logs/db-rlocalhost.error_log notice;
error_log C:/PATH_TO_PROJECT/nginx/logs/db-rlocalhost.error_log info;
error_log C:/PATH_TO_PROJECT/nginx/logs/db-rlocalhost.error_log error;
location / {
fastcgi_pass 127.0.0.1:8888;
fastcgi_pass_header Authorization;
fastcgi_hide_header X-Accel-Redirect;
fastcgi_hide_header X-Sendfile;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
}
location /media/ {
alias H:/AUCTION/;
}
location /static/ {
alias C:/PATH_TO_PROJECT/static/;
}
location /static_ac_invoice/ {
alias C:/PATH_TO_PROJECT/tender/ac_invoice/static/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Running server:
c:\PATH_TO_PROJECT\Scripts\python.exe c:\PATH_TO_PROJECT\tender\manage.py runfcgi method=threaded host=127.0.0.1 port=8888
C:\PATH_TO_PROJECT\nginx\nginx.exe
How can I get debug info without reconfigure project for running in non fastcgi mode?
python manage.py runfcgi method=threaded host=127.0.0.1 port=8888 daemonize=false
Now I can see debug info.
Why is key daemonize=false not present in help output manage.py?
I'm running FastCGI behind Nginx, and need to detect when the url is accessed via HTTPS. However, my Django web application always reports that the connection is HTTP (request.is_secure() == False). However, SSL is setup correctly, and I've verified my https:// urls are secure with an SSL checker.
How can I get Django to correctly detect when the request is from an HTTPS url?
My Nginx settings are:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
listen 443 default ssl;
ssl_certificate /home/webapp/ssl.crt
ssl_certificate_key /home/webapp/ssl.key
server_name myapp.com;
access_log /home/webapp/access.log
error_log /home/webapp/error.log
root /home/mywebapp;
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8801;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
}
I start the Django FastCGI process with:
python /home/webapp/manage.py runfcgi method=threaded host=127.0.0.1 port=8801 pidfile=/home/webapp/fastcgi.pid
Thanks to Yuji for the answer. I've updated my server block to conditionally inject HTTPS on or HTTPS off, depending on $server_port:
{
server {
listen 80;
listen 443 default ssl;
if ($server_port = 443) { set $https on; }
if ($server_port = 80) { set $https off; }
ssl_certificate /home/webapp/ssl.crt
ssl_certificate_key /home/webapp/ssl.key
server_name myapp.com;
access_log /home/webapp/access.log
error_log /home/webapp/error.log
root /home/mywebapp;
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8801;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
fastcgi_param HTTPS $https;
}
}
}
Make sure nginx is sending fastcgi_param HTTPS on for connections on 443.
I have set up Django's FastCGI + NGINX, but it's only working for root url: any request to http://example.com/anything redirects to http://example.com. Django's internal server working fine, NGINX static file serving and FastCGI processing of the root URL is fine, error log is clear. Here is my config's server section:
server {
listen 80;
server_name example.com;
location / {
fastcgi_pass localhost:8000;
include fastcgi_params;
}
location /static/ {
alias /root/web_development/src/web_development/static/;
}
}
What am I doing wrong? Thanks in advance.
Try this configs:
server {
listen 80;
server_name example.com;
location / {
root /home/example.com/foo;
fastcgi_pass 127.0.0.1:8000;
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;
}
}
Make sure you've already informed nginx the port which django runs.
You may need to add this line to location /:
fastcgi_split_path_info ^()(.*)$;
from djangoandnginx
I would like to run several virtual hosts via nginx, each serving a different django app via fcgi. Is this possible? If so, does anyone have good resources on where/how to start? The nginx docs seem to be mostly examples, but none of the particular config I'm attempting...
There have been two pretty good blog posts lately about setting up nginx, but both are using nginx to serve static (or cached) content and apache + mod_wsgi to serve Django. I prefer this myself because mod_wsgi makes process management so much easier than fcgi (among other advantages).
Anyway, here are the posts:
The Django and Ubuntu Intrepid Almanac
django, nginx, memcached - the dynamic trio
EDIT: ok I dug out an old tar file with my nginx + django + virtual host config files from a year ago before I switched to apache + mod_wsgi. This was a development machine, so you'll want to adjust # of workers, connections, etc.
nginx.conf:
user nginx nginx;
worker_processes 2;
error_log /var/log/nginx/error_log info;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 4 2k;
request_pool_size 4k;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain text/html application/x-javascript text/xml text/css;
output_buffers 4 32k;
postpone_output 1460;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75 20;
ignore_invalid_headers on;
include vhosts.d/*.conf;
}
So, the main nginx.conf includes every file in the vhosts.d/ subdirectory. One of my files was for serving PHP on localhost (was probably running phpMyAdmin) like this:
vhosts.d/00_localhost:
server {
listen 127.0.0.1:80;
server_name localhost;
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/localhost/htdocs;
index index.php index.html index.htm;
location ~ .php$ {
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/localhost/htdocs$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
And then a local Django development server like this:
50_django.local:
server {
listen 80;
server_name django.local;
access_log /var/log/nginx/django.access.log main;
error_log /var/log/nginx/django.error.log info;
root /var/www/django.local;
location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
access_log off;
break;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
expires 30d;
break;
}
location / {
fastcgi_pass unix:/var/www/django.local/server.sock;
include fastcgi.conf;
}
location ^~ /admin/ {
fastcgi_pass unix:/var/www/django.local/server.sock;
include fastcgi.conf;
access_log off;
auth_basic "Welcome to admin";
}
}
Finally, each of the virtual servers included a fastcgi.conf for each location.
fastcgi.conf:
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
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_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_SOFTWARE "nginx";
fastcgi_param GATEWAY_INTERFACE "CGI/1.1";
fastcgi_param UID_SET $uid_set;
fastcgi_param UID_GOT $uid_got;
I'm not sure all of the above were required, but that was another one of the reasons I switched to mod_wsgi... superior support and documentation :)
Since this question was asked someone created a pip installable django package that will generate an apache or nginx vhost file for you from your settings.py
pip install django-vhost
Check it out here: https://bitbucket.org/djangostars/django-vhost