带有proxy_pass的nginx位置路径

时间:2014-08-24 18:35:16

标签: django nginx proxy gunicorn

我有以下问题,我试图在运行Nginx的VPS上安装带有gunicorn服务器的Django应用程序。我的nginx配置如下所示:

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;

}

}

然而,当我导航到my_server.com/appname/时,我经常会遇到404错误。我还是Nginx的新手,有人能指出我如何设置/appname/路径的proxy_pass吗?我应该指出,当/appname/的位置替换为/时,django应用程序正常运行。

1 个答案:

答案 0 :(得分:11)

您只需要proxy_pass的尾部斜杠:

proxy_pass http://app_name/;

它可以帮助您剪切“appname”前缀,使配置如下所示:

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

}