配置基于名稱的虛擬主機,我們需要提前做好DNS記錄,并且要求每個虛擬主機獨立放一個文件,然后在nginx.conf中include包含進來
案例:
域名: bbs.51pet.com
www.51pet.com
服務器IP: 10.2.11.245
網站目錄:
/application/bbs
/application/www
日志文件:
/applicaiton/bbs/logs
/application/www/logs
配置文件:
/etc/nginx/conf.d/bbs.conf
/etc/nginx/conf.d/www.conf
[root@static ~]# mkdir -p /application/{bbs,www}/logs
[root@static nginx]# mkdir conf.d
[root@static nginx]# cd conf.d/
配置虛擬主機一:bbs.51pet.com
[root@static conf.d]# vim bbs.conf
server {
listen 80;
server_name bbs.51pet.com;
#charset koi8-r;
access_log /application/bbs/logs/bbs.access.log main;
location / {
root /application/bbs/;
index index.php index.html index.htm;
#if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.php/$1 last;
#}
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php/?.*$ {
root /application/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
}
}
配置虛擬主機二:
server {
listen 80;
server_name www.51pet.com;
#charset koi8-r;
access_log /application/www/logs/www.access.log main;
location / {
root /application/bbs/;
index index.php index.html index.htm;
#if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.php/$1 last;
#}
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php/?.*$ {
root /application/bbs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
}
}
然后在nginx.conf包含進這兩個虛擬主機文件
include conf.d/*.conf; (配置在http段)
[root@static conf.d]# /usr/local/nginx/sbin/nginx -s reload
最后測試:
[root@static conf.d]# echo bbs.51pet.com > /application/bbs/index.html
[root@static conf.d]# echo www.51pet.com > /application/www/index.html
- 第一部分:Nginx Web服務
- 第一章: Nginx的初探
- 第二章:Nginx的安裝部署
- 第一節:在centos6上通過Yum安裝Nginx
- 第二節:在Centos7上通過Yum安裝nginx
- 第三節:編譯安裝Nginx服務
- 第四節: Nginx編譯參數詳解
- 第五節: Nginx的啟動腳本
- 第六節:Nginx的shell腳本安裝
- 第七節:nginx服務的平滑升級
- 第三章:Nginx服務基礎配置
- 第一節: 配置運行nginx服務器用戶
- 第二節:配置運行生成的worker processes進程數
- 第三節:配置Nginx進程PID存放路徑
- 第四節:定義MIME類型
- 第五節:自定義nginx的訪問日志
- 第六節:主配置段(main)常用的配置
- 第七節:http段(核心)常用的配置命令
- 第八節:配置nginx中的location段
- 第九節: 基于IP的訪問控制
- 第十節:基于用戶的訪問控制
- 第十一節:Nginx定義status狀態頁
- 第十二節:Nginx服務器Rewrite重寫
- 第十三節:Nginx服務配置防盜鏈
- 第十四節:nginx軟件的優點
- 第四章:Nginx服務器的優化配置
- 第一節:nginx.conf配置文件基本參數優化
- 第二節:fastcgi調優
- 第三節:Gzip調優
- 第四節:expires緩存調優
- 第五節:內核參數優化
- 第六節: Nginx之——413 修改上傳文件大小限制
- 第五章:Nginx虛擬主機的配置
- 第一節:基于名稱的虛擬主機
- 第二節:通過shell腳本添加虛擬主機
- 第六章:Nginx常用模塊指令詳解
- 第一節:ngx_http_realip_module
- 第二節:http_image_filter_module
- 第三節:tengine中配置consistent hash module
- 第四節:ngx_http_userid_module
- 第七章:Nginx服務器配置HTTPS訪問
- 第八章: Nginx服務器日志切割
- 第二部分:Nginx負載均衡反向代理服務
- 第一章:Nginx進階參數詳解
- 第一節:proxy_module參數詳解
- 第二節:upstream_module參數詳解
- 第四章:Nginx反向代理服務
- 第一節:Nginx反向代理配置參數詳解
- 第二節:Nginx反向代理服務配置案例
- 第三節:Nginx緩存
- 第四節: Nginx中fastcgi模塊
- 第五節:nginx+fastcgi實現動靜分離架構
- 第五章:Nginx負載均衡
- 第一節:ngx_http_upstream_module常見指令
- 第二節:調度方法
- 第三節:健康狀態檢查