子域映射到nginx的网址(Mapping subdomains to URLs with ngin

2019-07-19 08:01发布

我很新的nginx的,所以请原谅我,如果我的解释是关闭。 我会尽我所能解释什么,我想要的目的。

使用WordPress和nginx的,我想用户帐户被映射到主域的子域。 例如,如果用户创建称为“样品”的帐户,该用户的子域将是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和管理。 他们应该通过这些规则会在请求被忽略。

如何在用户创建一个帐户可以自动我做到这一点? 这里的目标是自动化 - 一旦正确设置它,不用担心。 既然我已经从字面上刚开始前几天跟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文件,如果我希望它是自动的,而我积极努力学习如何实现这一目标。 我只是坚持我在哪里,现在,我期待的任何建议/帮助,会指出我在正确的方向。 我渴望学习!

Answer 1:

回答

经过搜索,扭捏和配置的日子里,我已经得到了下来子域映射到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的网址,任何内部重定向没有结尾的斜线。 从那里, resolver指令需要解决定义的URL proxy_pass ,所以我与谷歌的DNS解析。 我还使用了proxy_pass_header指令饼干送过来,以保持WordPress的登录认证机智。 proxy_pass定义URL映射到。

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

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

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



Answer 2:

下面应该这样做:

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;
}


Answer 3:

我想的.htaccess不与nginx的工作。 我使用Nginx的反向代理服务器的端口80和Apache的Web服务器HERE



文章来源: Mapping subdomains to URLs with nginx