2013-02-05 191 views
3

我对nginx非常陌生,所以如果我的解释关闭,请原谅我。我会尽我所能解释我想达到的目标。使用nginx将子域映射到URL

使用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文件,如果我希望它是自动的,而我积极努力学习如何为了达成这个。我只是卡在现在所在的位置,并且正在寻找任何建议/帮助,这些建议/帮助会指向正确的方向。我渴望学习!

+0

为什么你需要两个php相关的位置块?你有除WordPress以外的其他PHP页面?两者都指向相同的php-fpm套接字。 –

回答

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包括参数,所以你不需要(.*)组重写)

额外添加第二个serverblock你不想重定向域:

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

我得到这个错误:[emerg] invalid condition“then”在 – Thomas

+1

此外,这不会重定向用户吗?我不希望它们被重定向,而是映射到URL,以便子域URL保持在浏览器窗口中。 – Thomas

+0

哎呀,删除了那么(我的头红宝石:D)。是的,它会重定向用户,我认为这是你想要的?我误解了什么? – cobaco

2

ANSWER

后搜索,扭捏和配置的日子里,我已经得到了下来子域映射到URL完全一​​样在我的例子所需要的代码。这里是我的example.com的虚拟主机: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中定义的URL,因此我正在使用Google的DNS解决问题。我还使用proxy_pass_header指令发送cookie以保持WordPress登录验证的完整性。 proxy_pass定义了要映射到的URL。

还应当指出的是,如果你想使用登录验证以及与子域,你需要在wp-config.php这样来定义您的自定义Cookie域:

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

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

+0

这很好。但是,依靠外部DNS服务器(甚至谷歌的服务器)不利于性能。使用代理也会增加开销。 –

+0

也不要以为你需要'resolver'指令,它只在你的proxy_pass指令的域部分有变量时才有用。 –

0

我认为.htaccess不能与nginx一起使用。 我使用Nginx作为反向代理服务器端口80和Apache作为web服务器 HERE