今天学了nginx配置虚拟主机,有3种方式,在windows下修改nginx.conf文件。
首先是基于端口号配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 403 /403.html;
error_page 404 =200 /403.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html1;
index index1.html index1.htm;
}
error_page 403 /403.html;
error_page 404 /403.html;
error_page 500 502 503 504 /50x.html;
}
还有基于ip配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 403 /403.html;
error_page 404 =200 /403.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 196.168.0.100;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 403 /403.html;
error_page 404 =200 /403.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
最后还有基于域名配置,首先在windows/System32/drivers/etc目录下修改hosts文件,添加域名及对应的网站
例如 196.168.0.100 www.xx.com,接下来在nginx.conf中配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 403 /403.html;
error_page 404 =200 /403.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.xx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 403 /403.html;
error_page 404 =200 /403.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
在url中输入www.xx.com,它就会显示对应的index.html文件。
如果在nginx.conf下配置多个虚拟主机,会造成nginx.conf文件可读性差,可以使用子配置文件引入的方式
在nginx.conf所在目录下新建一个vhost.conf文件,然后在文件中配置service模块,最后再nginx.conf中使用include引入。
include有两种方式
include vhost.conf//单个文件引入
include *.conf//通配符引入
另外,可以在service下配置autoindex on,当用户访问该站点时,如果没有设置对应的index索引文件,就会显示该站点的目录,没有设置则报403错误。