将nginx中的子域url重写为后端服务器

时间:2013-03-22 20:38:17

标签: nginx rewrite reverse-proxy

我在我的django(gunicorn)应用程序面前运行nginx。我想要拨打电话:

api.mydomain.com

重定向到:

本地主机:8080 / API

我现在有了这个,但这显然不起作用:

    server {
        listen     80;
        server_name  api.mydomain.com;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

    location / {
       index  index.html index.htm;
       proxy_pass  http://localhost:8080/api;
              }
     }

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以将代理传递与重写相结合

server {
    listen     80;
    server_name  api.mydomain.com;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    location / {
       index  index.html index.htm;
       rewrite ^(.*)$ /api$1 break;
       proxy_pass   http://localhost:8080;
    }

}

答案 1 :(得分:1)

添加像这样的新位置块

location ~ api.mydomain.com
{
    fastcgi_pass localhost:8080;
    fastcgi_param SCRIPT_FILENAME $document_root/Django script's folder's name/$fastcgi_script_name;
}