if ( $host ~* (.*)\.(.*)\.(.*))
{
set $domain $1;
}
location =/
{
rewrite (.*) /html/$domain.html last;
}
# location =/ {
# index index.html index.htm;
# if (!-f $request_filename){
# rewrite (.*) /template.action;
# }
# }
以前的配置是将/转发到/template.action
现在需要将子域名转发到对应的html页面。
如man.frady.info转发到man.frady.info/html/man.html
完整的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#add www.frady.info
upstream mybalance {
ip_hash;
server 192.168.1.109:8080;
server 192.168.1.109:28080;
}
server {
listen 80;
server_name *.frady.info ;
set $htdocs D:/java/tomcat6/webapps/ROOT/;#此处定义了htdocs
root $htdocs;
charset utf-8;
rewrite ^/products/([0-9]+)\.html$ /showWare.action?ware.wareId=$1 last;
location ~^/(WEB-INF)/{
deny all;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js|html)$ {
expires 30d;
}
location /zanwork/(shopUpload|upload) {
proxy_pass http://mybalance;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~*.(jsp|do|action)$
{
index index.jsp;
proxy_pass http://mybalance;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
if ( $host ~* (.*)\.(.*)\.(.*))
{
set $domain $1;
}
location =/
{
rewrite (.*) /html/$domain.html last;
}
# location =/ {
# index index.html index.htm;
# if (!-f $request_filename){
# rewrite (.*) /template.action;
# }
# }
}
}
实际上,你可能还需要以下配置,这样才能更好的兼容没有静态页面的用户
if ( $host ~* (.*)\.(.*)\.(.*))
{
set $domain $1; #设置域名变量,以提供转发使用
}
location =/
{
set $zanHtmlFile $htdocs/htmlpage/indexHtml/$domain/index.html;#设置静态文件的位置
if (-f $zanHtmlFile){#如果存在静态文件,则跳转到静态文件
rewrite / /htmlpage/indexHtml/$domain/index.html last;
}
if (!-f $zanHtmlFile){#如果不存在静态文件,则跳转到动态地址
rewrite / /template4.action last;
}
}