【问题标题】:rewrite subdomain url in nginx to backend-server将 nginx 中的子域 url 重写为后端服务器
【发布时间】:2013-03-12 20:20:18
【问题描述】:

我在我的 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


    【解决方案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;
        }
    
    }
    

    【讨论】:

    • 如果我还想在“mydomain.com”上提供我的前端应用程序该怎么办?
    • 只需使用 server_name mydomain.com 创建一个单独的服务器块,并将根指令设置为包含您要服务的前端文件的目录。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 2017-11-13
      • 2012-04-29
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多