martin
martin

Reputation: 31

nginx reverse proxy for nginx+gunicorn+django server

A django app is running on django_server.com and I can't get the nginx reverse proxy working to serve that app. When connecting directly to the django server, let's say django_server.com every thing works fine, the user is redirected to django_server.com/user/login. The app is running on gunicorn + nginx (upstream and unix socket)

Now all traffic in our organization has to go over one server, lets say https://www.reverse_proxy.com, so I added a location https://www.reverse_proxy.com/django_app, and I can not get this connection working. The connection to reverse_proxy.com is https, the connection from reverse_proxy.com to django_server.com is http. The reverse_proxy is serving several other site and works, I also tested a connection to the django_server.com with a static html-site, that works, too. So the problem seems so be the django-side, but I can't figure out the error. There are no error-messages in the error logs of both nginxs, it is simply an 404 error.

Here is the nginx-configuration on the django_server.com:

#upstream gunicorn_socket { commented out for debugging
#    server unix:/home/django/w_plan/django/fakw/fakw.sock fail_timeout=0;
#}
server {
    server_name django_server.com;
    listen 80 default_server;

location ~ ^/django_app(.*)$ { 
    try_files $uri $1 /$1;
}


location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_set_header Host $http_host;
    proxy_redirect off;

    #proxy_pass http://gunicorn_socket/; debugging
    proxy_pass http://localhost:8000; # debugging

    }

}

And here the reverse_proxy:

...
location /django_app {
    proxy_pass http://django_server.com:80;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
    proxy_read_timeout 60;
}

Any ideas?

EDIT I: I've replaced the gunicorn with the django server for debugging and also added the suggestion from @danielgpm

In the django app, for debugging I modified my views.py file:

def index(request):
    #return HttpResponse("HelloWorld") # that works
    print(request.META)
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/user/login/') #does not work

urls.py

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^user/', include('userauth.urls', namespace='userauth')),
...

Now the request.META information should tell me, what goes wrong?

Upvotes: 0

Views: 3351

Answers (1)

danielgpm
danielgpm

Reputation: 1672

It seems that your django app does not have a router for /django_app path. You will have to either do a rewrite from /django_app in the reverse_proxy to / on the "django_server.com" server or configure a route in your app.

try adding the location below to your django_server.com server:

... 

location ~ ^/django_app(.*)$ {
  try_files $uri $1 /$1;
}

location / {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;

  proxy_set_header Host $http_host;
  proxy_redirect off;

  proxy_pass http://gunicorn_socket/;
}

Upvotes: 1

Related Questions