nginx基於域名的虛擬主機配置
nginx服務的主要配置文件nginx.conf
[root@lnmp01 conf]# ls -l nginx.conf
-rw-r--r-- 1 root root 2788 Jan 14 17:41 nginx.conf
[root@lnmp01 conf]# pwd
/application/nginx/conf
去掉註釋及空行後的配置文件
[root@lnmp01 conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
默認虛擬主機為localhost,
這裏配置虛擬,可以配置兩個,中間用空格隔開
如 server_name www.tuwei.com tuwei.com
配置完後如下
[root@lnmp01 conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
listen 80;
server_name www.tuwei.org tuwei.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置站點文件
[root@lnmp01 conf]# echo "hi.Im tuwei,my 51cto‘s blog is http://blog.51cto.com/tuwei">../html/www/index.html
將IP和域名加到本地hosts文件中
echo "192.168.132.20 www.tuwei.org">>/etc/hosts
檢查語法
sbin/nginx -t
重啟nginx服務
[root@lnmp01 conf]# kill -HUP cat ../logs/nginx.pid
或者用
../sbin/nginx -c /application/nginx/conf/nginx.conf
../sbin/nginx -s reload
本機測試
[root@lnmp01 conf]# curl www.tuwei.org
hi.Im tuwei,my 51cto‘s blog is http://blog.51cto.com/tuwei
如果在電腦上訪問該域名,需要在電腦hosts文件中作解析。
基於端口的虛擬主機----次重要
基於IP的虛擬主機------不重要----企業一般用負載均衡配ip
listen IP:80;
server_name IP;
nginx日誌
worker_processes 1;
error_log /application/logs/err.log crit;------->crit為日誌級別
events {
use epoll;------------------------------->
worker_connections 1024;
}
日誌格式
配置文件中有,去掉註釋
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
訪問日誌
location / {
root html/www;
index index.html index.htm;
}
access_log /application/logs/host.access.log main;-------->
可以用命令awk ‘{print $1}‘ host.access.log |sort|uniq -c|sort -rn -k1 通過日誌分析訪問網站的IP情況
nginx基於域名的虛擬主機配置