在Nginx配置目录下,创建一个vhost目录。我的配置目录在/etc/nginx

mkdir /etc/nginx/vhost

创建网站a的配置文件

vi /etc/nginx/vhost/vhost_a.conf

server配置

server {
    listen  80;
    server_name  www.a.com;

    root /var/www/html/a;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html{
        root /usr/share/nginx/html;
    }

    location ~ .*\.php(\/.*)*$ {
        fastcgi_split_path_info ^(.+?.php)(/.*)$;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

同样创建网站b的配置文件,server_name和root改成对应的即可;然后编辑nginx.conf文件。

vi /etc/nginx/nginx.conf

在http里加入以下内容,再重启Nginx.

http {
    ...
    include /etc/nginx/vhost/vhost_*.conf;
}