使用Nginx将子域映射到URL [英] Mapping subdomains to URLs with nginx

查看:98
本文介绍了使用Nginx将子域映射到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对nginx还是很陌生,所以如果我的解释不对,请原谅我.我会尽力解释我要实现的目标.

I'm very new to nginx, so forgive me if my explanations are off. I'll do my best to explain what I am trying to achieve.

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

Using WordPress and nginx, I would like user accounts to be mapped to a subdomain of the main domain. For example, if the user creates an account called "sample", the subdomain for that user would be sample.example.com.

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

When the user goes to sample.example.com, the subdomain should be mapped to example.com/sample/. Similarly, if a user visits sample.example.com/account/, it should map to example.com/sample/account/, and so on and so forth. It should be noted that the example.com/sample/ URLs are rewrites of this type of structure: example.com/index.php?user=sample.

还有一些不应重定向的保留子域,例如cdn和admin.如果需要,这些规则应将其忽略.

There are also a few reserved subdomains that should not be redirected, such as cdn and admin. They should be ignored by these rules if they are requested.

当用户创建帐户时,如何自动实现此目的?这里的目标是自动化-正确设置一次,而不用担心.由于几天前我才刚开始使用nginx,所以我不确定该从哪里开始.任何将我带往正确方向的建议都将非常有用.这是我当前域的配置文件:

How can I achieve this automatically when a user creates an account? The goal here is automation - set it up once correctly and not worry about it. Since I have literally just started working with nginx a few days ago, I'm not sure where to start at all. Any advice to move me in the right direction would be incredibly helpful. Here is my current config file for the domain:

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文件中,并且我正在积极尝试学习如何实现这一目标.我只是停留在现在的位置,正在寻找可以为我指明正确方向的任何建议/帮助.我渴望学习!

I understand that what I am trying to achieve probably needs to go into the /etc/nginx/nginx.conf file if I want it to be automated, and I am actively trying to learn how to achieve this. I'm just stuck where I am at now and am looking for any advice/help that would point me in the right direction. I'm eager to learn!

推荐答案

ANSWER

经过几天的搜索,调整和配置,我得到了将子域映射到URL所需的代码,就像在我的示例中一样.这是我的example.com虚拟主机: https://gist.github.com/thomasgriffin/4733283

After days of searching, tweaking, and configuring, I've gotten down the code needed to map subdomains to URLs exactly like in my example. Here is my vhost for 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.

The main chunk of the mapping is done in the first server block. I'm targeting any subdomain (I will have already weeded out restricted subdomains with other non-relevant code) and rewriting it to ensure that it has a trailing slash to avoid any internal redirects by WordPress for URLs without a trailing slash. From there, the resolver directive is required to resolve URLs defined in proxy_pass, so I am resolving with Google's DNS. I'm also using the proxy_pass_header directive to send over cookies in order to keep WordPress login authentication in tact. proxy_pass defines the URL to map to.

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

It should also be noted that if you want to use login authentication as well with subdomains, you need to define your custom cookie domain in wp-config.php like this:

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

应该就是这样.现在,您可以使用诸如subdomain.example.com之类的URL映射到example.com/user/subdomain/或任何您想要的URL.从那里,您可以利用WordPress的Rewrite API将映射的URL映射到特定的查询参数,这些参数可以发送到$wp_query以便加载自定义模板等.

And that should be it. You can now enjoy URLs like subdomain.example.com that map to example.com/user/subdomain/ or whatever you want. From there, you can utilize WordPress' Rewrite API to map the mapped URL to specific query args that can be sent to $wp_query for loading custom templates, etc.

这篇关于使用Nginx将子域映射到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆