使用nginx将子域映射到URL

时间:2013-02-05 16:01:51

标签: wordpress nginx rewrite subdomain

我对nginx很新,所以如果我的解释不对,请原谅我。我会尽力解释我想要实现的目标。

使用WordPress和nginx,我希望将用户帐户映射到主域的子域。例如,如果用户创建了一个名为“sample”的帐户,则该用户的子域名为sample.example.com

当用户转到sample.example.com时,子域应映射到example.com/sample/。同样,如果用户访问sample.example.com/account/,则应该映射到example.com/sample/account/,依此类推。请注意,example.com/sample/网址是此类结构的重写:example.com/index.php?user=sample

还有一些保留的子域名不应重定向,例如cdn和admin。如果要求,这些规则应忽略它们。

如何在用户创建帐户时自动实现此目的?这里的目标是自动化 - 正确设置它并且不用担心它。由于我几天前刚刚开始使用nginx,我不知道从哪里开始。任何让我朝着正确方向前进的建议都会非常有帮助。这是我当前的域配置文件:

server {
    listen          80;
    server_name     www.example.com;
    rewrite     ^(.*) $scheme://example.com$1 permanent;
}

server {
    listen          443 ssl;
    server_name     www.example.com;
    rewrite         ^(.*) $scheme://example.com$1 permanent;
}

server {
    listen      80;
    server_name example.com;

    access_log  /var/www/example.com/logs/access.log;
    error_log   /var/www/example.com/logs/error.log;

    root        /var/www/example.com/public;
    index       index.php;

    location / {
        try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
    }

    location @wordpress {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    # Pass the PHP scripts to FastCGI server listening on UNIX sockets.
    #
    location ~ \.php$ {
        try_files $uri @wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/public$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen                      443 ssl;
    ssl                         on;
    keepalive_timeout           70;
    server_name                 example.com;
    ssl_certificate             ssl/example.com.chained.crt;
    ssl_certificate_key         ssl/example.key;
    ssl_protocols               SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                 HIGH:!aNULL:!MD5;
    ssl_session_cache           shared:SSL:10m;
    ssl_session_timeout         10m;
    ssl_prefer_server_ciphers   on;

    root        /var/www/example.com/public;
    index       index.php;

    location / {
        try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
    }

    location @wordpress {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    # Pass the PHP scripts to FastCGI server listening on UNIX sockets.
    #
    location ~ \.php$ {
        try_files $uri @wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/public$fastcgi_script_name;
        include        fastcgi_params;
    }
}

我理解我想要实现的目标可能需要进入/etc/nginx/nginx.conf文件,如果我希望它自动化,我正在积极尝试学习如何实现这一目标。我只是被困在我现在的位置,正在寻找任何可以指向正确方向的建议/帮助。我渴望学习!

3 个答案:

答案 0 :(得分:2)

<强> ANSWER

经过几天的搜索,调整和配置后,我已经完成了将子域映射到URL所需的代码,就像在我的示例中一样。这是我对example.com的vhost:https://gist.github.com/thomasgriffin/4733283

server {
    listen      80;
    listen      443 ssl;
    server_name ~^(?<user>[a-zA-Z0-9-]+)\.example\.com$;

    location / {
        resolver            8.8.8.8;
        rewrite             ^([^.]*[^/])$ $1/ permanent;
        proxy_pass_header   Set-Cookie;
        proxy_pass          $scheme://example.com/user/$user$request_uri;
    }
}

server {
    listen          80;
    listen          443 ssl;
    server_name     www.example.com;
    return          301 $scheme://example.com$request_uri;
}

server {
    listen      80;
    server_name example.com;

    access_log  /var/www/example.com/logs/access.log;
    error_log   /var/www/example.com/logs/error.log;

    root        /var/www/example.com/public;
    index       index.php;

    location / {
        try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
    }

    location @wordpress {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    # Pass the PHP scripts to FastCGI server listening on UNIX sockets.
    #
    location ~ \.php$ {
        try_files $uri @wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen                      443 ssl;
    ssl                         on;
    keepalive_timeout           70;
    server_name                 example.com;
    ssl_certificate             ssl/example.com.chained.crt;
    ssl_certificate_key         ssl/example.key;
    ssl_protocols               SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                 HIGH:!aNULL:!MD5;
    ssl_session_cache           shared:SSL:10m;
    ssl_session_timeout         10m;
    ssl_prefer_server_ciphers   on;

    root        /var/www/example.com/public;
    index       index.php;

    location / {
        try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
    }

    location @wordpress {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    # Pass the PHP scripts to FastCGI server listening on UNIX sockets.
    #
    location ~ \.php$ {
        try_files $uri @wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

映射的主要块在第一个服务器块中完成。我的目标是任何子域名(我已经用其他不相关的代码淘汰了受限制的子域名)并重写它以确保它有一个尾部斜杠,以避免WordPress对没有尾部斜杠的URL进行任何内部重定向。从那里开始,resolver指令需要解析proxy_pass中定义的网址,因此我正在使用Google的DNS进行解析。我还使用proxy_pass_header指令发送cookie以保持WordPress登录身份验证。 proxy_pass定义要映射到的网址。

还应注意,如果您要使用登录身份验证以及子域名,则需要在wp-config.php中定义自定义Cookie域,如下所示:

define('COOKIE_DOMAIN', '.example.com');

那应该是它。您现在可以享受subdomain.example.com之类的网址,这些网址可以映射到example.com/user/subdomain/或您想要的任何内容。从那里,您可以利用WordPress的重写API将映射的URL映射到可以发送到$wp_query以加载自定义模板等的特定查询参数。

答案 1 :(得分:0)

以下应该这样做:

server {
  listen 80; listen 443;
  server_name *.example.com;

  if ($host ~ "^(.*)\.example\.com$" ) { set $subdomain $1;}
  rewrite ^ $scheme://example.com/$subdomain/$request_uri permanent;
}

(顺便说一句:正则表达式^最有效地匹配所有url,标准nginx变量$request_uri保存包含参数的uri,因此您不需要(.*)组在重写中)

另外为您不想重定向的域添加第二个服务器块:

server {
  listen 80; listen 443;
  server_name cdn.example.com admin.example.com;
  # do whatever with the requests of the reserved subdomains;
}

答案 2 :(得分:0)

我认为.htaccess无法使用nginx。 我使用Nginx作为反向代理服务器端口80和Apache作为Web服务器 HERE

相关问题