根据server_name指令为fastcgi屏蔽/重写SERVER_NAME

时间:2016-04-29 03:06:09

标签: php nginx

我有一个PHP网站,为每个用户使用子域名。

我希望能够在内部将cname.otherdomain.com路由到otherdomain.domain.com并将其传递给PHP。

目前我有这个

server {
  listen 80;
  server_name cname.otherdomain.com;
  rewrite ^ $scheme://otherdomain.domain.com$request_uri; 
}

server {
    listen 80 default_server;

    server_name ~^(?<subdomain>[a-z\_\-\.]+)?\.?domain\.com$ "";

    root g:/www;
    index index.php;

    #error_log /var/log/nginx/debug.log debug;

    location ~ (?:application|modules|system) {
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php$request_uri;
    }

    location ^~ /index.php {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SERVER_NAME $http_host;
        fastcgi_pass 127.0.0.1:9999;
    }
}

1 个答案:

答案 0 :(得分:0)

如果要将其隐藏在用户之外,则需要将其保留在单个服务器块中。您可以创建一个服务器块来捕获子域并将其重用于fastcgi位置块:

server {
    server_name ~^cname\.(?<subdomain>.+)\.com$;
    ...
    location ... {
        fastcgi_param SERVER_NAME $subdomain.domain.com;
    }
}
server {
    listen 80 default_server;
    ...
}

nginx配置文件中的大量重复可以使用include语句进行管理。

相关问题