12

I have following problem, i'm trying to put a Django app with an gunicorn server on my VPS running Nginx. My nginx config looks like this:

upstream app_name {
    server unix:/path/to/socket/file.sock fail_timeout=10;
}

server {

   listen 80 default_server;
   listen[::]:80 default_server ipv6only=on;
   root /webapps/;
   server_name my_hostname.com;

   location / {
      proxy_set_header Host $http_host;
}

   location /appname/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_pass http://app_name;

}

}

However when i navigate to my_server.com/appname/ i constantly get 404 error. I'm still new to Nginx, could someone point me in the right direction on how to set the proxy_pass for /appname/ path? I should point out that when location for /appname/ is replaced with / the django app is running fine.

Konrad Wąsowicz
  • 422
  • 2
  • 6
  • 12

1 Answers1

19

You just need a trailing slash for proxy_pass:

proxy_pass http://app_name/;

it helps you to cut the "appname" prefix so the config looks like:

upstream app_name {
    server unix:/path/to/socket/file.sock fail_timeout=10;
}

server {

   listen 80 default_server;
   listen[::]:80 default_server ipv6only=on;
   root /webapps/;
   server_name my_hostname.com;

   location /appname/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_pass http://app_name/;

}
Anatoly
  • 15,298
  • 5
  • 53
  • 77
  • @KonradWąsowicz you are welcome, make also sure you serve images straight through Nginx (by define another location with static assets), it's recommended way. – Anatoly Aug 24 '14 at 19:15
  • I did that, however i got to some strange problem afterwards, namely each time i click a link inside an app it keeps resetting the address to root for instance when i click `login` link instead of going to `hostname.com/appname/login` it redirects to `hostname.com/login`. – Konrad Wąsowicz Aug 24 '14 at 19:22
  • 2
    @KonradWąsowicz all in-app redirects needs to be rewritten with URL "/appname" prefix in your case. If it's hard to do, just mount app according to that [example](http://docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath) and remove trailing slash. – Anatoly Aug 24 '14 at 19:30
  • 1
    @mikhailov Adding `FORCE_SCRIPT_NAME = '/appname'` in *settings.py* worked for me. Thank you. – JJD Jan 26 '15 at 10:41