【问题标题】:change server name returned to application using nginx config?使用 nginx 配置更改返回给应用程序的服务器名称?
【发布时间】:2015-08-13 16:09:22
【问题描述】:

短版:

是否可以让 nginx 将服务器名称“myexample.com”更改为“example.com”并将其返回给使用它的应用程序?应用程序在 nginx 的服务器标签中使用 uwsgi_pass。

加长版:

我有一个运行 Flask 应用程序的 nginx 服务器。它使用 uwsgi_pass 和一个套接字来运行应用程序。 我的 Flask 应用程序有

app.config['SERVER_NAME'] = 'example.com'

解析子域(我几乎没有处理子域的蓝图)。我还注册了另一个子域,例如 myexample.com,我想将其重定向到此服务器 IP 并执行完全相同的操作。 但我知道因为flask 使用那个“example.com”来解析子域,它不会起作用!

我找不到在烧瓶中执行此操作,但我想知道是否可以让“myexample.com”的 nginx 将服务器名称更改为“example.com”,然后将其传递给应用程序?我这样认为应该可以正常工作。

谢谢

【问题讨论】:

    标签: nginx flask http-headers


    【解决方案1】:

    这是可能的,因为 Nginx 支持正则表达式。

    不确定 Flask,但例如 PHP:

    server
    {
        server_name           ~^(?<project>.+)\.dev$;
        set $root /var/www/$project;
        root $root;
        error_log             /var/log/nginx/error.log;
        location ~ \.php$ {
            fastcgi_param MAGE_RUN_CODE $project;
            fastcgi_read_timeout 3600;
            include       /etc/nginx/fastcgi_params;
            fastcgi_pass  development;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        }
    }
    

    PHP 将有一个名为$MAGE_RUN_CODE 的变量,其值为$project,与正则表达式匹配。

    【讨论】:

      【解决方案2】:

      你可以用这个

        proxy_set_header   HOST     "example.com" ;
        proxy_pass http://127.0.0.1/;
        proxy_buffering off;
      

      【讨论】:

        猜你喜欢
        • 2019-01-27
        • 2018-02-03
        • 2017-09-05
        • 1970-01-01
        • 2020-02-18
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多