在Nginx上配置多个站点

1.建立vhost文件(/usr/local/nginx/conf/vhost)

2.配置nginx.conf,加入:include vhost/*.conf;

user  nginx nginx;
worker_processes  4;


#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
 

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
	 fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 128k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
  location / {
            autoindex on;
            root   /usr/local/nginx/html;     #指定索引文件的目录
            index  index.html index.htm index.php;
        }

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


        location  ~ \.php$  {
		    autoindex on;
            root           /usr/local/nginx/html;
			try_files  $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }

    }


 include vhost/*.conf;

}

 3.再vhost 建立两个子网站配置nice.conf,nice1.conf

server {
        listen       80;
        server_name  www.nice.com;
        root   /usr/local/nginx/html/www.nice.com;
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}
server {
        listen       80;
        server_name  www.nice1.com;
        root   /usr/local/nginx/html/www.nice1.com;
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}

比如hosts中配置的映射:

127.0.0.1    www.nice.com
127.0.0.1    www.nice1.com

 

配置后重启nginx service nginx restart

posted @ 2019-08-05 22:16  爱一流网  阅读(289)  评论(0)    收藏  举报