Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django - nginx location path with proxy_pass

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...