/ 中存储网

Nginx配置多个虚拟主机网站站点

2013-09-29 08:18:01 来源:ITJS.CN

编辑配置文件

vi /opt/nginx/conf/nginx.conf

user www www;

worker_processes 2;

error_log logs/error.log notice;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /data/logs/www-logs/http/access.log main;

sendfile on;

keepalive_timeout 65;

client_header_buffer_size 1k;

large_client_header_buffers 4 4k;

gzip on;

#虚拟主机1

server {

listen 80;

server_namewww.site1.com;

access_log /data/logs/www-logs/www.site1.com/host.access.log main;

location / {

root /data/web/www.site1.com/htdocs;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#虚拟主机2

server {

listen 80;

server_namewww.site2.com;

access_log /data/logs/www-logs/www.site2.com/host.access.log main;

location / {

root /data/web/www.site2.com/htdocs;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#虚拟主机3

server {

listen 80;

server_namewww.site3.com;

access_log /data/logs/www-logs/www.site3.com/host.access.log main;

location / {

root /data/web/www.site3.com/htdocs;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

创建 index.html

vi /opt/web/www.site1.com/htdocs/index.html

加入:<head><h1>This is SITE 1!!</head></h1>

vi /opt/web/www.site2.com/htdocs/index.html

加入:<head><h1>This is SITE 2!!</head></h1>

vi /opt/web/www.site3.com/htdocs/index.html

加入:<head><h1>This is SITE 3!!</head></h1>

启动Nginx

/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

加HOST测试

192.168.1.100 www.site1.com

192.168.1.100 www.site2.com

192.168.1.100 www.site3.com

DONE : )