nginx + codeigniter
My goal is to implement a Mass dynamic virtual hosting system with support for custom domains. (here's a somewhat similar set up
tumblr uses this feature, they have it set up so that members' profiles are on sub domain system. for example when mrfoobar.tumblr.com points his custom domain name A record to the ip address of tumblr he can access his tumblr profile on mrfoobardomain.com and any subsequent requests also get mapped, like mrfoobar.tumblr.com/mypics is now accessible on mrfoobardomain.com/mypics
The set up I currently have thus far is a as follows.
members are able to view their profile like so: mywebsite.com/member-username
in my routes.php file
$route['404_override'] = 'public_profiles/check';
In the check method I simply get the value of the first uri segment and check it against my database to see if that is a match for a member's username. If it is I serve up their profile.
If its not I redirect back to my main default controller.
That works great if members don't use a custom domain.
However if they do decide to use a custom domain. Proper Nginx configuration settings are required, and this is what I came up with.
worker_processes 1;
events
{
worker_connections 1024;
}
http
{
include mime.types;
include /etc/nginx/conf/fastcgi.conf;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name mywebsite.com www.mywebsite.com ;
index index.html index.htm index.php;
root /srv/http/mywebsite.com/public;
#------------------ nginx logs -------------------#
access_log /srv/http/mywebsite.com/logs/access.log;
error_log /srv/http/mywebsite.com/logs/error.log;
#---------------- For CodeIgniter ----------------#
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
#---------------------------------------------------#
location /
{
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root html;
}
#----------- settings to get php to work with nginx -------#
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /etc/nginx/conf/fastcgi_params;
}
}#sever1
#--------------Server Block to handle anything else that is not mywebsite.com or www.mywebsite.com -------#
server{
listen 80 default_server;
server_name ~^(www\.)?(?<domain>.+)$; #lets capture host name incase we need it $domain
index index.php;
root /srv/http/mywebsite.com/public;
#---------------- For CodeIgniter ----------------#
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
#---------------------------------------------------#
location /
{
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root html;
}
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /etc/nginx/conf/fastcgi_params;
}
}#server2
}#http</domain>
what I would "like" to do in the second server block is to load up the publicprofiles/check and once, there I would use the $server['http_host'] php variable to check if that domain is in the database and load up the appropriate user data. Im pretty must stuck in connecting the nginx portion to the codeigniter portion. Can anybody help me out? any other strategies or suggestions are welcomed, I just cant get a handle on this.