Django development server is intended only for use while developing. There are times that we need to test Django applications remotely through development server. Thought we may bind the development server to some public IPs to achieve this, it is not a good practice in general for various reasons. Ssh port forwarding provides a much secure way for remote testing of Django development server. Let serv be the server where Django development server resides, test the remote host where you want to conduct the tests. Here is a how-to.
1. On host serv, start your development server:
python manage.py runserver 8080
where 8080 is any port number of your choice.
2. On host test, start ssh port forwarding:
ssh -L 9090:localhost:8080 user@serv
where 9090 is the port number providing remote service.
3. On host test, point your browser to localhost:9090 to perform tests.
If you want to test the development server through other hosts, say third, you should enable gateway forwarding on host test by issuing:
ssh -g -L 9090:localhost:8080 user@serv
Then you may perform tests through host third by pointing your browser to test:9090.
A journal on information technology: things I studied, worked, thought, but can't stay in my memory.
Showing posts with label webapp. Show all posts
Showing posts with label webapp. Show all posts
Wednesday, April 3, 2013
Tuesday, January 1, 2013
deploying static files and django applications with Nginx
Just learned a trick to serve static files as well as django applications with nginx. In the following setup, nginx tries to serve static files first, if the requested file is not found, then it passes request to the (django) application server (gunicorn, if you wish).
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
If we want to enable user-dir mode, the configuration code should be put before these.
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
If we want to enable user-dir mode, the configuration code should be put before these.
User-based website directory with Nginx
User-dir mode can be done by using regex captures. Here is an example to configure your nginx:
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
deploying django with gunicorn and nginx
Gunicorn is a wsgi HTTP server. As its document says:
The nginx server can be configured as follows:
server {
listen 80; ## listen for ipv4;
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name your.app.domain.name;
location /static {
root /path/to/your/staticfiles;
}
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Start gunicorn to serve your django application. Be sure to bind gunicorn to the port number (8080) specified in the previous configuration file:
gunicorn -b localhost:8080 django_application.wsgi:application
it is best to use Gunicorn behind HTTP proxy server.Nginx is strongly suggested as the proxy server. Here is a quick note for deploying django with nginx and gunicorn.
The nginx server can be configured as follows:
server {
listen 80; ## listen for ipv4;
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name your.app.domain.name;
location /static {
root /path/to/your/staticfiles;
}
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Start gunicorn to serve your django application. Be sure to bind gunicorn to the port number (8080) specified in the previous configuration file:
gunicorn -b localhost:8080 django_application.wsgi:application
Tuesday, December 25, 2012
hooray
It works!
[django + mezzanine + apache2 + wsgi] on fedora 17 and ubuntu 12.04
how to:
[django + mezzanine + apache2 + wsgi] on fedora 17 and ubuntu 12.04
how to:
- collect static files for mezzanine
python manage.py collectstatic - put static and template files under the mezzaine application directory
- configure mod_wsgi (conf.d/wsgi.conf) for apache2
there are examples on site of djangoproject:
https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/
Subscribe to:
Posts (Atom)