需求:配置exmple.coma.exmple.comb.exmple.com指向不同的网站

1. 域名解析配置

A记录配置主域名、子域名全部指向 服务器外网IP地址
A > @ >服务器外网IP
A > a >服务器外网IP
A > b >服务器外网IP

2. nginx.conf配置:

# exmple.com
server {
    listen       80 default_server;

    # 域名可以有多个,用空格隔开
    server_name  exmple.com www.exmple.com;

    root         /home/www/exmple.com;

    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # 图片缓存时间设置
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 10d;
    }

    # JS和CSS缓存时间设置
    location ~ .*.(js|css)?$ {
        expires 1h;
    }

    # 将服务器错误页面重定向到静态页面/40x.html
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

# a.exmple.com
server {
    listen       80;

    # 域名可以有多个,用空格隔开
    server_name  a.exmple.com;

    root         /home/www/a.exmple.com;

    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # 图片缓存时间设置
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 10d;
    }

    # JS和CSS缓存时间设置
    location ~ .*.(js|css)?$ {
        expires 1h;
    }

    # 将服务器错误页面重定向到静态页面/40x.html
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

# b.exmple.com
server {
    listen       80;

    # 域名可以有多个,用空格隔开
    server_name  b.exmple.com;

    root         /home/www/b.exmple.com;

    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # 图片缓存时间设置
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 10d;
    }

    # JS和CSS缓存时间设置
    location ~ .*.(js|css)?$ {
        expires 1h;
    }

    # 将服务器错误页面重定向到静态页面/40x.html
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}