Nginx二级域名(泛域名)绑定问题

由于搭建的各个网站都在一台服务器上(运行环境PHP+Mysql+Nginx),所以在只能采取多域名绑定来实现比较好的区别站点。
举个例子我的站点根目录为: /usr/www 下面搭建着主网站,绑定的域名是:http://chzker.com。然后在 /usr/www/nav 下面安装的是导航站点。常规的访问方式是:http://chzker.com/nav ,然而这种地址看着让人恶心,远不如用二级域名 http://nav.chzker.com 来访问要漂亮。那怎么来实现呢?
首先我们把http://nav.chzker.com”>http://chzker.com,http://nav.chzker.com 都解析到相同的IP地址也就是 我的服务器地址,然后再Nginx.conf开启另外一个Server,代码如下:

server {

listen       80;

server_name  blog.chzker.com chzker.com;

root /var/www/html;

index index.html index.php;

if (!-e $request_filename)

{
rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last;

rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;

rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;

rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last;

rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;

rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;

rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;

rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;

rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;

rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;

#Force redirection for old rules

rewrite ^/read.php/([0-9]+).htm$ http://$host/post/$1/ permanent;

rewrite ^/post/([0-9]+).htm$ http://$host/post/$1/ permanent;

rewrite ^/post/([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/ permanent;

rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/$3/ permanent;

rewrite ^/index_([0-9]+)_([0-9]+).htm$ http://$host/page/$1/$2/ permanent;

rewrite ^/star_([0-9]+)_([0-9]+).htm$ http://$host/starred/$1/$2/ permanent;

rewrite ^/category_([0-9]+).htm$ http://$host/category/$1/ permanent;

rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/category/$1/$2/$3/ permanent;

rewrite ^/archive_([0-9]+)_([0-9]+).htm$ http://$host/archiver/$1/$2/ permanent;

rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/archiver/$1/$2/$3/$4/ permanent;

rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/date/$1/$2/$3/ permanent;

rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/date/$1/$2/$3/$4/$5/ permanent;

#Filename alias

rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;

}

#charset koi8-r;

#access_log  logs/host.access.log  main;

error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

#use tomcat

location ~ .jsp$ {

index index.jsp;

proxy_pass http://localhost:8080;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

location ~ .php$ {

root           /var/www/html;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;

include        fastcgi_params;

}

# deny access to .htaccess files, if Apache’s document root

# concurs with nginx’s one

#

#location ~ /.ht {

#    deny  all;

#}

}

#=============zi yu ming bang ding=========================

server {

listen       80;

server_name  nav.chzker.com;

root /var/www/html/nav;

index index.html index.php;

#charset koi8-r;

#access_log  logs/host.access.log  main;

error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

location ~ .php$ {

root           /var/www/html/nav;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /var/www/html/nav$fastcgi_script_name;

include        fastcgi_params;

}

}

server {

listen       80;

server_name  bbs.chzker.com;

root /var/www/html/bbs;

index index.html index.php;

#charset koi8-r;

#access_log  logs/host.access.log  main;

error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

location ~ .php$ {

root           /var/www/html/bbs;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /var/www/html/bbs$fastcgi_script_name;

include        fastcgi_params;

}

}

#==================zi yu ming bang ding ====================

当然开启多个是一样的,原理相同。

Nginx二级域名(泛域名)绑定问题》有一个想法

  1. Pingback引用通告: Nginx的反向代理负载均衡 | OB-NOTE

评论已关闭。