将Nginx中的子域URL重写到后端服务器

问题描述 投票:1回答:2

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

api.mydomain.com

将重定向到:

localhost: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;
    }
}

谢谢!

nginx url-rewriting reverse-proxy
2个回答
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
投票

添加这样的新位置块

location ~ api.mydomain.com
{
    fastcgi_pass localhost:8080;
    fastcgi_param SCRIPT_FILENAME $document_root/Django script's folder's name/$fastcgi_script_name;
}
© www.soinside.com 2019 - 2024. All rights reserved.