重写nginx的子域网址到后端服务器(rewrite subdomain url in nginx

2019-08-17 21:57发布

我在我的Django(gunicorn)应用程序的前面跑nginx的。 我要拨打的电话:

api.mydomain.com

被重定向到:

本地主机:8080 /火

我现在有这个,但是这显然是行不通的:

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

谢谢!

Answer 1:

您可以将代理通过与重写

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

}


Answer 2:

添加一个新的位置,块这样的

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


文章来源: rewrite subdomain url in nginx to backend-server