Mapping subdomains to URLs with nginx Mapping subdomains to URLs with nginx wordpress wordpress

Mapping subdomains to URLs with nginx


ANSWER

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

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.

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');

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.


the following should do it:

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

(as an aside: the regex ^ matches all url's the most efficiently, and the standard nginx variable $request_uri holds the uri including arguments so you don't need the (.*) group in the rewrite)

additionally add a second serverblock for the domains you don't want redirected:

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


I think .htaccess is not working with nginx.I use Nginx As Reverse Proxy Server port 80 and Apache as web serverHERE