1、配置基于域名的虚拟主机
[root@web01 html]# egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >/application/nginx/conf/nginx.conf ####将配置文件中的注释和空行删除
[root@web01 html]# vim /application/nginx/conf/nginx.conf #####修改如下特殊颜色内容
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.suffergtf.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@web01 html]# mkdir /application/nginx/html/www ####创建www站点目录
[root@web01 html]# echo "hello,this is a test page for www" >/application/nginx/html/www/index.html ####添加www站点默认首页文件
[root@web01 html]# /application/nginx/sbin/nginx -t ###检查配置文件
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 html]# /application/nginx/sbin/nginx -s reload ####重新加载nginx
[root@web01 html]# lsof -i :80 ####检查nginx是否正常启动
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 30396 root 6u IPv4 38676 0t0 TCP *:http (LISTEN)
nginx 30399 nginx 6u IPv4 38676 0t0 TCP *:http (LISTEN)
[root@web01 html]# echo "192.168.127.11 www.suffergtf.com" >>/etc/hosts
[root@web01 html]# tail -1 /etc/hosts
192.168.127.11 www.suffergtf.com
[root@web01 html]# curl www.suffergtf.com
hello,this is a test page for www
2、配置多个基于域名的虚拟主机,并且将虚拟主机配置到单独的配置文件
[root@web01 ~]# cd /application/nginx/conf [root@web01 conf]# mkdir extra ####创建虚拟主机配置文件目录 [root@web01 conf]# cd extra/
[root@web01 extra]# vim www.conf ####编辑www虚拟主机,内容如下
server {
listen 80;
server_name www.suffergtf.com;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web01 extra]# cp www.conf blog.conf #####直接cp,并且修改blog虚拟主机配置
[root@web01 extra]# vim blog.conf
server {
listen 80;
server_name blog.suffergtf.com;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web01 extra]# cp www.conf bbs.conf
[root@web01 extra]# vim bbs.conf
server {
listen 80;
server_name bbs.suffergtf.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web01 extra]# vim ../nginx.conf ####配置nginx主配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
}
[root@web01 extra]# mkdir ../../html/{blog,bbs}
[root@web01 extra]# ll ../../html/
总用量 20
-rw-r--r-- 1 root root 537 6月 5 17:05 50x.html
drwxr-xr-x 2 root root 4096 6月 5 19:24 bbs
drwxr-xr-x 2 root root 4096 6月 5 19:24 blog
-rw-r--r-- 1 root root 612 6月 5 17:05 index.html
drwxr-xr-x 2 root root 4096 6月 5 18:52 www
[root@web01 extra]# echo "hello,this is test for blog" >../../html/blog/index.htm
[root@web01 extra]# echo "hello,this is test for bbs" >../../html/bbs/index.htm
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# echo "192.168.127.11 blog.suffergtf.com bbs.suffergtf.com" >>/etc/hosts
[root@web01 extra]# tail -2 /etc/hosts
192.168.127.11 www.suffergtf.com
192.168.127.11 blog.suffergtf.com bbs.suffergtf.com
[root@web01 extra]# curl www.suffergtf.com
hello,this is a test page for www
[root@web01 extra]# curl blog.suffergtf.com
hello,this is test for blog
[root@web01 extra]# curl bbs.suffergtf.com
hello,this is test for bbs
3、虚拟主机的别名配置
[root@web01 extra]# vim www.conf server { listen 80; server_name suffergtf.com www.suffergtf.com; ####添加域名 location / { root html/www; index index.html index.htm; } }
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# echo "192.168.127.11 suffergtf.com" >>/etc/hosts
[root@web01 extra]# curl suffergtf.com
hello,this is a test page for www
##########别名的用处:1、访问不同的域名,希望访问的是同一虚拟主机;2、集群环境中,所有节点都是同一域名,所以需要通过别名来检测每一个节
4、Nginx状态信息功能
[root@web01 extra]# ../../sbin/nginx -V ####查看编译参数 nginx version: nginx/1.6.3 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module ####需要开启这个信息功能参数 [root@web01 extra]# cat >>/application/nginx/conf/extra/status.conf<<eof ####配置状态信息站点 > server{ > listen 80; > server_name status.suffergtf.com; > location / { > stub_status on; ####开启状态信息 > access_log off; > } > } > eof [root@web01 extra]# vim ../nginx.conf [root@web01 extra]# ../../sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful [root@web01 extra]# ../../sbin/nginx -s reload [root@web01 extra]# echo "192.168.127.11 status.suffergtf.com" >>/etc/hosts [root@web01 extra]# curl status.suffergtf.com Active connections: 1 #####nginx正在处理的活动连接数 server accepts handled requests ###server表示nginx启动到现在共处理了8个连接;accepts表示从nginx启动到现在处理了8次握手;handled requests表示共处理了8次请求 8 8 8 Reading: 0 Writing: 1 Waiting: 0 ###reading表示nginx读取到客户端的header信息数;writing表示nginx返回给客户端的header信息数;waiting表示nginx已经处理完正在等待下一次请求的驻留连接waiting=active-(reading+writing)
5、nginx错误日志
error_log语法如下
error_log file level;
关键字 日志文件 错误日志级别 ####级别【debug|info|notice|warn|error|crit|alert|emerg】默认为error级别
错误日志可放置在:main,http,server,location模块;通常放在main中即可
[root@web01 extra]# vim ../nginx.conf
worker_processes 1;
error_log logs/error.log error; #######开启错误日志功能
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
include extra/status.conf;
}
6、nginx访问日志
nginx访问日志,主要由log_format,access_log控制,默认配置如下
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
将log_format放在nginx.conf配置文件的http模块,在虚拟主机server中配置access_log
[root@web01 extra]# vim ../nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
include extra/status.conf;
}
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name suffergtf.com www.suffergtf.com;
access_log logs/access_www.log main;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web01 extra]# vim blog.conf
server {
listen 80;
server_name blog.suffergtf.com;
access_log logs/access_blog.log main;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web01 extra]# vim bbs.conf
server {
listen 80;
server_name bbs.suffergtf.com;
access_log logs/access_bbs.log main;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# tail -1 ../../logs/access_www.log
[root@web01 extra]# curl www.suffergtf.com
hello,this is a test page for www
[root@web01 extra]# tail -1 ../../logs/access_www.log
192.168.127.11 - - [05/Jun/2018:22:18:21 +0800] "GET / HTTP/1.1" 200 34 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
#####在高并发可以将access_log增加buffer和flush选项,提升网站性能
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name suffergtf.com www.suffergtf.com;
access_log logs/access_www.log main gzip buffer=32k flush=5s;
location / {
root html/www;
index index.html index.htm;
}
}
7、nginx访问日志轮询切割
[root@web01 extra]# mkdir /server/scripts
[root@web01 extra]# vim /server/scripts/www_logrotate.sh ####编辑日志切割脚本
#!/bin/bash
######this a logrotate shell for www.suffergtf.com#######
Dateformat=$(date +%Y%m%d) ####定义日期,通过日期命名轮询的日志名称
Basedir="/application/nginx" ####定义nginx安装目录,方便后面重新加载
Nginxlogdir="$Basedir/logs" ####定义日志目录
Logname="access_www" ####日志名称
[ -d $Nginxlogdir ]&&cd $Nginxlogdir||exit 1 #####如果$Nginxlogdir存在则进入该目录,不存在则退出脚本
[ -f ${Logname}.log ] ||exit 1 #####若果${Logname}.log不存在则推出
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log ####改名
$Basedir/sbin/nginx -s reload ####重新加载nginx生成新的日志文件
find ${Nginxlogdir} -mtime +60 -name *_${Logname}.log |xargs rm -f ####删除60天以前的日志,日志保留60天
[root@web01 extra]# cat >>/var/spool/cron/root <<eof
> ####logrotate for www.suffergtf.com by suffergtf
> 00 00 * * * /bin/sh /server/scripts/www_logrotate.sh >/dev/null 2>&1
> eof
[root@web01 extra]# crontab -l
####logrotate for www.suffergtf.com by suffergtf
00 00 * * * /bin/sh /server/scripts/www_logrotate.sh >/dev/null 2>&1
8、nginx rewrite
rewrite语法
rewrite ^/(.*) http://www.suffergtf.com/$1 permanent;
rewrite是关键字,$1是前面括号里的部分(.*),结尾的permanent是永久重定向
- 同域名的rewrite
[root@web01 extra]# vim www.conf server { listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name suffergtf.com; rewrite ^/(.*) http://www.suffergtf.com/$1 permanent; ####suffergtf.com跳转到www.suffergtf.com }
- 不同于名的rewrite,配置用户访问blog.suffergtf.com时跳转到www.suffergtf.com/blog/suffergtf.html
[root@web01 extra]# vim blog.conf
[root@web01 extra]# vim blog.conf
server {
listen 80;
server_name blog.suffergtf.com;
access_log logs/access_blog.log main gzip buffer=32k flush=5s;
location / {
root html/blog;
index index.html index.htm;
}
if ($http_host ~* "^(.*)\.suffergtf\.com$"){
set $domain $1;
rewrite ^(.*) http://www.suffergtf.com/$domain/suffergtf.html break;
}
}
[root@web01 extra]# echo "this a test page for blog.suffergtf.com rewrite to www.suffergtf.com/blog/suffergtf.html">../../html/www/blog/suffergtf.html
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
9、nginx访问认证
nginx认证:auth_basic,auth_basic_user_file
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name www.suffergtf.com;
access_log logs/access_www.log main gzip buffer=32k flush=5s;
location / {
root html/www;
index index.html index.htm;
auth_basic "auth test"; ####认证提示信息
auth_basic_user_file /application/nginx/conf/htpasswd;
}
}
server {
listen 80;
server_name suffergtf.com;
rewrite ^/(.*) http://www.suffergtf.com/$1 permanent;
}
[root@web01 extra]# which htpasswd ###这里使用apache的htpasswd,这里如果没有安装请自行安装
/usr/bin/htpasswd
[root@web01 extra]# htpasswd -bc /application/nginx/conf/htpasswd suffergtf password
Adding password for user suffergtf
[root@web01 extra]# chmod 400 /application/nginx/conf/htpasswd
[root@web01 extra]# chown nginx.nginx /application/nginx/conf/htpasswd
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload