1

I want to use nginx as a reverse for apache and gunicorn

location / should be handled by apache

location /webapps/should be handled by gunicorn

upstream gfbu_app_server {
        server unix:/webapps/gfbu/run/gunicorn.sock fail_timeout=0;
        }

server {
        listen  80;
        server_name eu-con.gfbu;

        client_max_body_size 4G;

        access_log /webapps/gfbu/logs/nginx-access.log;
        error_log /webapps/gfbu/logs/nginx-error.log;

        location /static/ {
                alias /webapps/gfbu/gfbu/static/;
        }
        location /media/ {
                alias /webapps/gfbu/gfbu/media/;
        }

        location /webapps/ {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        if (!-f $request_filename) {
                proxy_pass http://gfbu_app_server;
                break;
                }
        }

        error_page 500 502 503 504 /500.html;
        location = 500.html {
                root /webapps/gfbu/static/;
                }

        # everything to apache
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8090;
                }

This works kind of, but the request URL for gunicorn is http://<ipaddress>/webapps/ but it has to be / to work.

Gunicorn serves a Django Webapp and it cant match the http://<ipaddress>/webapps/ to any url pattern. Clearly I did something wrong here- comming from Apache and mod_wsgi it was possible to redirect the the request to the specified wsgi_script without the subdir part.

Iam not sure wich part of the nginx -> gunicorn -> django stack is misconfigured - could you help me out?

1 Answer 1

2

Replace

proxy_pass http://gfbu_app_server;

With:

proxy_pass http://gfbu_app_server/;

This will not work with the if block. To not break Djangos autogenerated URL (remember: Django thinks it operates at the document root) you have to add the FORCE_SCRIPT_NAME variable to the settings.py e.g:

FORCE_SCRIPT_NAME = "/webapps"

Also check if you need to change the redirects for LOGIN_URL and LOGOUT_URL

2
  • This works kind of - it breaks all the django autogenerated urls since they loose the subdirectory prefix and are given back to apache Jul 13, 2015 at 10:32
  • this can be resolved by adding: FORCE_SCRIPT_NAME = "/webapps" to the django settings.py file Jul 13, 2015 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .