Nginx虚拟主机配置
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Nginx虚拟主机配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                nginx的虛擬主機就是通過nginx.conf中server節(jié)點指定的,想要設(shè)置多個虛擬主機,配置多個server節(jié)點即可
先看一個最簡單的虛擬主機配置示例
server { listen 80; #監(jiān)聽的是80端口 server_name www.wl.com; #指定這個虛擬主機名為www.wl.com,當(dāng)用戶訪問www.wl.com時,就有這個虛機主機進行處理location / { #因為所有請求都是/開頭的,所以這行表示匹配所有請求 index index.html; #指定此虛擬主機的默認首頁為index.html root /home/www/host_a/; #指定此虛擬主機的物理根目錄為/home/www/host_a/ 即指定 location / 在那個物理目錄 } }虛擬主機名可以有4種格式:
(1)準(zhǔn)確的名字,例如此例中的a.test.com
(2)*號開頭的,例如 *.test.com
(3)*號結(jié)尾的,例如 mail.*
(4)正則表達式形式,例如
server_name ~^www\d+\.test\.com$; 注意,使用正則表達式形式時,必須以'~'開頭server_name也可以同時指定多個,例如:
server_name test.com www.test.com *.test.com;這時優(yōu)先級為:(1)確切的名字(2)最長的以*起始的通配符名字(3)最長的以*結(jié)束的通配符名字(4)第一個匹配的正則表達式名字案例
(1)對兩個域名配置相應(yīng)的虛擬主機,指定不同的目錄
a.test.com -> /home/www/a
b.test.com -> /home/www/b
配置:
server { listen 80; server_name a.test.com; autoindex on; #開啟網(wǎng)站目錄文件列表功能,訪問目錄時列出其中的文件列表,默認不開啟 index index.html; root /home/www/a/; }server { listen 80; server_name b.test.com; index index.html; root /home/www/b/; location /(self)/ { #禁止對self目錄的訪問 deny all; } }(2)對不同訪問目錄指定不同物理目錄
server { listen 80; server_name ~^\d+\.\d+\.\d+\.\d+$; #使用正則格式,這里表示接受任何ip index index.html index.htm; root /home/lg/www/;location /share { root /home/lg/Downloads; }location ^~ /Videos { root /home/lg/; autoindex on; autoindex_exact_size on; autoindex_localtime on; allow all; }location ^~ /html5 { root /home/lg/workspace/nodejs/; index index.html index.htm; }location = /404.html { root /usr/share/nginx/html; } } Nginx默認是不允許列出整個目錄的。如需此功能:autoindex_exact_size #默認為on,顯示出文件的確切大小,單位是bytes #改為off后,顯示出文件的大概大小,單位是kB或者MB或者GBautoindex_localtime #默認為off,顯示的文件時間為GMT時間。 #改為on后,顯示的文件時間為文件的服務(wù)器時間allow all; #允許所以訪問如: location /images {root /var/www/nginx-default/ibugaocn;autoindex on;}轉(zhuǎn)載于:https://www.cnblogs.com/xuyuQAQ/p/8728695.html
總結(jié)
以上是生活随笔為你收集整理的Nginx虚拟主机配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Codeforces - 961E Tu
- 下一篇: 污水处理概念有哪些上市公司 股民不得不重
