今天做了一个nginx的多虚拟主机的实验,遇到了一些问题。首先介绍一下配置文件如何修改。如下是我的主配置文件nginx.conf

user nobodynobody;

worker_processes2;

error_log/usr/local/nginx/logs/nginx_error.log crit;

pid/usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile51200;

events

{

    use epoll;

    worker_connections 6000;

}

http

 

{

   

    include /usr/local/nginx/conf/vhosts/*.conf;

    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/plainapplication/x-javascript text/css text/htm application/xml;

}

在主配置文件中不放server段的信息。然后在主配置文件添加一行include/usr/local/nginx/conf/vhosts/*.conf;这样,我们就可以在 conf/vhosts目录下创建虚拟主机配置文件了。我所添加的路径是灵活的,根据本机不同的情况进行修改。

我创建了三个虚拟主机配置文件分别为123.conf,456.conf,789.conf

123.conf如下:

server

{

    listen 80;

    server_name www.123.com;

    index index.html index.htm index.php;

    root /var/123;

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME/var/123$fastcgi_script_name;

    }

}

456.conf如下:

server

 

{

    listen 80 default_server;

    server_name www.456.com www.456.wangdi.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;

    }

}

 

789.conf

server

 

{

    listen 80 ;

    server_name www.789.com;

    index index.html index.htm index.php;

    root /var/789;

 

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME/var/789$fastcgi_script_name;

    }

}

由于我是在本地测试,所以在本地的/etc/hosts中添加域名(这里我添加了多个IP,其实单个IP也是可以的,因为域名已经区分了要访问的不同主机)

nginx中配置虚拟主机_local

主机上也增加了IP地址

nginx中配置虚拟主机_配置文件_02

 

在配置文件的所在路径创建index.php文件,例如:

<?php

   echo "this is 789";

?>

重启nginxphp-fpm

service nginxrestartservice php-fpm restart

然后访问这三个虚拟主机就可以了。