首先更改nginx的主配置文件,/usr/local/nginx/conf/nginx.conf 添加:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log error;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    include vhosts/*.conf;
    }

                            ##包含vhosts目录下的所有.conf的文件,这是必须要写的


这样,我们就可以在 /usr/local/nginx/conf/vhosts目录下创建虚拟主机配置文件了。mkdir  /usr/local/nginx/conf/vhosts


然后建立虚拟主机配置文件,比如1.conf 和2.conf 代表两个不同的域名和网站

1.conf 里写入:

server 
{
    listen 80 default_server;  ##默认主机
    server_name www.hu.com;   ##域名
    index index.html index.htm index.php;
    root /usr/local/nginx/html;   ##网站所在目录
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
}

2.conf 写入:

server 
{
    listen 80;
    server_name www.123.com;   ##网站域名
    index index.html index.htm index.php;
    root /data/www2;  ##网站目录
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;
    }
}

最后重启nginx服务,这样我们两个域名的添加就完成了!