03.27 windows for nginx如何配置虚拟主机(多站点)

本文将详细介绍window for nginx 多站点(虚拟主机)配置过程,亲测有效!需要了解的朋友可以参考下

1、进入conf文件夹,在该文件夹下新建一个vhost文件夹,用于存放多个虚拟主机配置文件,如下图:

windows for nginx如何配置虚拟主机(多站点)

2、在nginx.conf里http配置段内添加了一行 include vhost/*.conf;用来读取vhost下的虚拟主机配置,如下图:

windows for nginx如何配置虚拟主机(多站点)

3、将nginx.conf文件内的server配置段复制,放在一个文件里,存到了conf/vhost下,以方便配置多个虚拟主机。

windows for nginx如何配置虚拟主机(多站点)

(上图是我配置的9个虚拟站点)

4、在C:\\WINDOWS\\system32\\drivers\\etc\\hosts文件中添加如下记录,使域名能够指向主机。

127.0.0.1 www.zl266.com(你自己的域名)

127.0.0.1 www.mifans.top(你自己的域名)

通过上面四步,nginx虚拟主机就算配置完成了。下面是一些文件的详细信息。

虚拟主机文件内容如下:

server {

listen 202.***.***.***:80; #换成你的IP地址

client_max_body_size 100M;

server_name www.test.com; #换成你的域名

charset gb2312;

index index.html index.htm index.php;

root /host/wwwroot/test; #你的站点路径

#打开目录浏览,这样当没有找到index文件,就也已浏览目录中的文件

autoindex on;

if (-d $request_filename) {

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

error_page 404 /404.html;

location = /40x.html {

root /host/wwwroot/test; #你的站点路径

charset on;

}

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /host/wwwroot/test; #你的站点路径

charset on;

}

#将客户端的请求转交给fastcgi

location ~ .*\\.(php|php5|php4|shtml|xhtml|phtml)?$ {

fastcgi_pass 127.0.0.1:9000;

include /host/nginx/conf/fastcgi_params;

}

#网站的图片较多,更改较少,将它们在浏览器本地缓存15天

location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 15d;

}

#网站会加载很多JS、CSS,将它们在浏览器本地缓存1天

location ~ .*\\.(js|css)?$

{

expires 1d;

}

location /(WEB-INF)/ {

deny all;

}

#设定日志格式

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

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

#设定本虚拟主机的访问日志

access_log /host/nginx/logs/down/access.log access; #日志的路径,每个虚拟机一个,不能相同

server_name_in_redirect off;

[color=#0000ff]}

[/color]

主要配置listen、server_name和root三个参数,其它参数可以不管。

今天的知识就暂时介绍到这里,如果有任何疑问或者经验欢迎在下方评论区与大家沟通交流。


分享到:


相關文章: