本文將詳細介紹window for nginx 多站點(虛擬主機)配置過程,親測有效!需要了解的朋友可以參考下
1、進入conf文件夾,在該文件夾下新建一個vhost文件夾,用於存放多個虛擬主機配置文件,如下圖:
2、在nginx.conf裡http配置段內添加了一行 include vhost/*.conf;用來讀取vhost下的虛擬主機配置,如下圖:
3、將nginx.conf文件內的server配置段複製,放在一個文件裡,存到了conf/vhost下,以方便配置多個虛擬主機。
(上圖是我配置的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三個參數,其它參數可以不管。
閱讀更多 IT前哨站 的文章