After configuring nginx (reverse proxy) and gunicorn, env variables stop being detected (Django app on Ubuntu)


On an ubuntu machine hosting my Django app (with postgres backend),
my env variables were prefectly detected when I fired up gunicorn as
my sole webserver using the command gunicorn --bind 0.0.0.0:8080
--env DJANGO_SETTINGS_MODULE=myproject.settings
myproject.wsgi:application



Next, I installed nginx and configured it to act as a reverse proxy
with gunicorn (using a digital ocean href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04"
rel="nofollow">guide here). There is no supervisor. This new web
server configuration fired up correctly, except now it doesn't detect
the env variables at all.



Imagine my env variables are awake=1 and
secret=abc123. I've already tried putting export
awake=1
and export secret=abc123, in
/etc/default/nginx, in gunicorn.conf, in
/etc/environment (which sets them globally). I also tried
adding them to nginx.conf as env awake=1; and env
secret=abc123;
.



Nothing worked.



Now it seems that nginx




removes all environment variables inherited from its parent
process
except the TZ variable




Source: rel="nofollow">http://nginx.org/en/docs/ngx_core_module.html#env
Could this be the reason why nothing I'm trying has come close to
working? Nevertheless, echo $awake yields 1 on the
command line, which tells me that perhaps the variables are set, but
being bypassed or overlooked.



It's become very frustrating. Can anyone help me figure this one
out? Thanks in advance.






wsgi.py:



import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())


gunicorn.conf:



description "Gunicorn application server handling
myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid myuser
setgid www-data
chdir /home/myuser/directory/myproject/

exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn
--chdir=/home/myuser/directory/ --workers 3 --bind
unix:/home/myuser/directory/myproject/myproject.sock --env
DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application


/etc/nginx/sites-available/myproject:



server {
listen 80;
server_name myapp.cloudapp.net;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myuser/directory/myproject;
}

location / {
include proxy_params;
proxy_pass
http://unix:/home/myuser/directory/myproject/myproject.sock;
}
}


Note: Please ask for more information if you need it

I have a site hosted using nginx+gunicorn+django and have APIs
written in tastypie which is taking data from mysql models.



So, I want to detect where the data is changed when I fetch through
API.



Is that can be done using modified bit of which is present in
header of packet ?



Or any other way around ?



Thanks in advance :)

Network & Servers

I have a django site I am trying to deploy with gunicorn and nginx.
All routes work fine except for those with hyphens. These return a
400 error.



I have debug set to True, so it is not
the allowed_hosts issue I keep reading about. It is not
a problem with my regexp either. The site works as is on Apache.



Here is my nginx config:



server {
listen 80 default;
client_max_body_size 4G;
server_name mydomain.local

keepalive_timeout 5;

location / {
try_files $uri @proxy_to_app;
}

location /static/ {
alias /path/to/project/static/;
}

location /uploads/ {
alias /path/to/project/uploads/;
}

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


}



And here is my urls.py



from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
url(r'^$', 'Portfolio.views.get_home', name='home'),
url(r'^contact/$', 'Portfolio.views.get_contact', name='contact'),
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[w-]+)/$', 'Portfolio.views.get_gallery',
name='gallery')
)


As I said, it is only slugs containing a hyphen that cause the
issue.

Web Development

My Goal is to let client connect through https
connection but apache serve it to my application which is running on
same server over http. Here is my minimalistic apache
configuration file (for incoming http requests i am simply redirecting
all request to https):



NameVirtualHost 1.2.3.4:443
NameVirtualHost 1.2.3.4:80

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

<VirtualHost 1.2.3.4:443>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin admin@abc.com
ServerName abc.com
ServerAlias www.abc.com
RequestReadTimeout header=90 body=90

DocumentRoot /path/to/my/project
LogLevel warn
WSGIDaemonProcess abc_ssl processes=2 maximum-requests=500
threads=10
WSGIProcessGroup abc_ssl
WSGIScriptAlias / /path/to/my/project.wsgi
WSGIApplicationGroup %{GLOBAL}

SSLEngine on
SSLCertificateFile /home/django/.ssh/abc.crt
SSLCertificateKeyFile /home/django/.ssh/server.key
SSLCertificateChainFile /home/django/.ssh/abc.ca-bundle

RequestHeader set X-FORWARDED-SSL "on"
RequestHeader set X-FORWARDED_PROTO "https"
ProxyRequests off
ProxyPreserveHost on

<Location /stream/>
Order Allow,Deny
Allow from All
</Location>

ProxyPass /stream/ http://127.0.0.1:8001/
ProxyPassReverse /stream/ http://127.0.0.1:8001/

</VirtualHost>


Clearly the gunicorn is running and listening on
http://127.0.0.1:8001/:



2013-08-31 05:05:51 [15025] [INFO] Starting gunicorn 0.17.2
2013-08-31 05:05:51 [15025] [INFO] Listening at: http://127.0.0.1:8001
(15025)
2013-08-31 05:05:51 [15025] [INFO] Using worker: eventlet
2013-08-31 05:05:51 [15044] [INFO] Booting worker with pid: 15044
2013-08-31 05:05:51 [15045] [INFO] Booting worker with pid: 15045
2013-08-31 05:05:51 [15046] [INFO] Booting worker with pid: 15046


But on browser i can only see NetworkError: 404 NOT FOUND -
https://abc.com/stream/
. Please help me i am stuck, really
appreciate that.

Network & Servers

This question may be asked many times, I have been reading but
still can't get what I want.



I have an application running in
172.17.1.10:8080/myapp I would like to setup nginx
reverse proxy accessing the application using
sub.domain.com only , hidding "myapp" in the address
bar.



my current setting is:



server{
listen 80;
server_name sub.domain.com;
location /myapp {
rewrite ^/myapp(.*) /$1 break;
proxy_pass http://172.17.1.10:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
include /etc/nginx/proxy_params;
}

}


but I keep viewing the Nginx index page with
sub.domain.com, and sub.domain.com/myapp the
tomcat index page... what am I missing?

Network & Servers

This question may be asked many times, I have been reading but
still can't get what I want.



I have an application running in
http://172.17.1.10:8080/myapp I would like to setup nginx
reverse proxy accessing the application using
http://sub.domain.com only , hidding "myapp" in the
address bar.



My current setting is:



server{
listen 80;
server_name sub.domain.com;
location /myapp {
rewrite ^/myapp(.*) /$1 break;
proxy_pass http://172.17.1.10:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
include /etc/nginx/proxy_params;
}

}


But I keep viewing the Nginx index page with
http://sub.domain.com, and
http://sub.domain.com/myapp the tomcat index page. What
am I missing?

Network & Servers

On an ubuntu machine hosting my Django app (with postgres backend),
my env variables were prefectly detected when I fired up gunicorn as
my sole webserver using the command gunicorn --bind 0.0.0.0:8080
--env DJANGO_SETTINGS_MODULE=myproject.settings
myproject.wsgi:application



Next, I installed nginx and configured it to act as a reverse proxy
with gunicorn (using a digital ocean href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04"
rel="nofollow">guide here). There is no supervisor. This new web
server configuration fired up correctly, except now it doesn't detect
the env variables at all.



Imagine my env variables are awake=1 and
secret=abc123. I've already tried putting export
awake=1
and export secret=abc123, in
/etc/default/nginx, in gunicorn.conf, in
/etc/environment (which sets them globally). I also tried
adding them to nginx.conf as env awake=1; and env
secret=abc123;
.



Nothing worked.



Now it seems that nginx




removes all environment variables inherited from its parent
process
except the TZ variable




Source: rel="nofollow">http://nginx.org/en/docs/ngx_core_module.html#env
Could this be the reason why nothing I'm trying has come close to
working? Nevertheless, echo $awake yields 1 on the
command line, which tells me that perhaps the variables are set, but
being bypassed or overlooked.



It's become very frustrating. Can anyone help me figure this one
out? Thanks in advance.






wsgi.py:



import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())


gunicorn.conf:



description "Gunicorn application server handling
myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid myuser
setgid www-data
chdir /home/myuser/directory/myproject/

exec /home/myuser/.virtualenvs/myvirtualenv/bin/gunicorn
--chdir=/home/myuser/directory/ --workers 3 --bind
unix:/home/myuser/directory/myproject/myproject.sock --env
DJANGO_SETTINGS_MODULE=myproject.settings myproject.wsgi:application


/etc/nginx/sites-available/myproject:



server {
listen 80;
server_name myapp.cloudapp.net;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myuser/directory/myproject;
}

location / {
include proxy_params;
proxy_pass
http://unix:/home/myuser/directory/myproject/myproject.sock;
}
}


Note: Please ask for more information if you need it

Network & Servers

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow