就是在一台服务器启动多个网站。
那么如何区分不同的网站,一般有两种方式:
1、域名不同
2、端口不同
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #一个server节点就是一个虚拟主机 server { #端口 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #Html是nginx安装目录下的html目录 root html; index index.html index.htm; } } }
可以配置多个server,配置了多个虚拟主机。
添加虚拟主机:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } } #复制一份server之后,修改相关信息就可以了 server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html81; index index.html index.htm; } } }
重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload
复制html目录
[root@nginx nginx]# cp -r html html81
修改相关内容
[root@nginx nginx]# vi html81/index.html
重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload
通过浏览器访问:
成功!