一:使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。
二:虚拟机的分类:
1:基于域名虚拟主机(一个ip地址对应多个域名,不同域名就是不同的站点,其内容也不一样)
2:基于端口虚拟主机(服务器只有一个ip地址,不同端口就是不同的站点,其内容也不一样)
3:基于ip虚拟主机(服务器有多个ip地址,不同ip就是不同的站点,其内容也不一样)
二:nginx -t 用于检测配置文件语法
如下报错1:配置文件43行出现错误
[root@www ~]# nginx -t
nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:43
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
如下错误2:worker里面工作区出现问题
[root@www ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
解决办法:
[root@www ~]# ulimit -n 10240
=============================================================
nginx 源包下载网站 http://nginx.org
安装前要检查并卸载HTTPD服务,以防端口冲突
检查HTTPD是否安装
[root@localhost ~]# rpm -q httpd
卸载 yum -y remove httpd //有httpd软件必须删除
安装支持软件
[root@localhost ~]#yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
完毕!
创建运行用户、组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
导入nginx软件包
[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.16.0.tar.gz original-ks.cfg
[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src // 解压文件到/usr/src
[root@localhost ~]# cd /usr/src/nginx-1.16.0/
编译安装 Nginx
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
注:配置前可以参考:./configure --help给出说明
--prefix 设定Nginx的安装目录
--user和--group 指定Nginx运行用户和组
--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计
--with-http_ssl_module 启用SSL模块
--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件
为主程序 nginx 创建链接文件
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 9月 10 17:12 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
Nginx 的运行控制方法
手动方法控制 Nginx:
nginx -t 检测配置文件语法
执行 nginx 主程序启动 Nginx
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx //启动nginx服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 64726/nginx: master
通过检查 Nginx程序的监听状态,或者在浏览器中访问此WEB服务(默认页面将显示"Welcome to nginx!"),可以确认Nginx服务是否正常运行。
主程序Nginx支持标准的进程信号,通过kill或者killall命令传送
l HUP 重载配置 等同于-1
l QUIT 退出进程 等同于-3
l KILL 杀死进程 等同于-9
[root@nginx ~]# killall -s HUP nginx
[root@nginx ~]# killall -s QUIT nginx
当Nginx进程运行时,PID号默认存放在/usr/local/nginx/logs/目录下的nginx.pid文件中,因此若改用kill命令,也可以根据nginx.pid文件中的PID号来进行控制。
编写 nginx 服务脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Scripts shell
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig --list nginx
nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost ~]# /etc/init.d/nginx status
Nginx is stopped
[root@localhost ~]# /etc/init.d/nginx stop
[root@localhost ~]# /etc/init.d/nginx start
[root@localhost ~]# /etc/init.d/nginx status
Nginx is running
[root@localhost ~]# /etc/init.d/nginx reload
Nginx 配置文件分析
Nginx主配置文件位置一般放在/usr/local/nginx/conf/nginx.conf
配置语句格式为 “关键字 值;” ( 例:keyname key; )
在http内可以有多个server
在server内可以有多个loctation
2)I/O事件配置
events { use epoll; //使用epoll模型 worker_connections 10240; //每个进程允许的最多的连接数默认为1024一般10000以下 }
3)HTTP配置
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
(定义日志输出格式,输出格式名字为main )【$开头的内容为Nginx的内置变量:
$remote_addr 客户端地址
$remote_user 客户端用户
$remote_user 时间
$request 请求 请求方法
$status 状态,包含返回值,例,200,302,301
$body_bytes_sent 指定主体大小
$http_referer 指定来源于什么连接
$http_user_agent 指定客户端浏览器相关信息
$http_x_forwarded_for 写客户端IP,一般为空值】
access_log logs/access.log main; //(访问日志存放位置,遵循main格式输出) sendfile on; //(是否允许文件下载或传输,on为允许) keepalive_timeout 65; //(指定长连接超时时间) server { //web服务的监听配置 listen 80; //监听地址及端口(IP:PORT)
server_name www.crushlinux.com; //网站名称(FQDN)
charset utf-8; //网页的默认字符集
access_log logs/www.crushlinux.com.access.log main; //指定虚拟主机访问日志,*可以不要 location / { //location 匹配【/根】
root html; //当访问【/根】的时候,要去html里面找页面
index index.html index.htm; //默认首页 用来定义默认主页首页,在html里面找以上类型文件
}
location /status {
stub_status on; //打开状态统计状态
ZZ //关闭此位置的日志记录 }
error_page 500 502 503 504 /50x.html; //内部错误的反馈页面
location =/50x.html { //错误页面配置
root html;
}
}
}
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# mkdir ../html/mailcom
[root@localhost conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024 //报错
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# ulimit -n 65536 //解决
[root@localhost conf]# nginx -t //检查
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful //问题解决了
[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# iptables -F
[root@localhost conf]# setenforce 0
[root@localhost conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@localhost conf]# /etc/init.d/nginx reload
Active connections 表示当前活跃的连接数,
第三行的三个数字表示Nginx当前总共处理了2个连接,成功创建2次握手,总共处理了2个请求。
Reading //正在读的没有 Writing //正在写的1个 Waiting //正在等待的1个
=======================================================================
虚拟主机应用:(创建多个基于域名的虚拟主机并测试)
要创建两个站点www.crushlinux.com和www.cloud.com
为两个虚拟WEB主机分别建立根目录,并准备测试首页
[root@localhost~]#mkdir /usr/local/nginx/html/crushlinux
[root@localhost~]#mkdir /usr/local/nginx/html/cloud
[root@localhost~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html
[root@localhost~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html
[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; worker_processes 2; worker_cpu_affinity 00000001 00000010; error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 10240; } http { include mime.types; default_type application/octet-stream; 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; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.crushlinux.com; charset utf-8; access_log logs/www.crushlinux.com.access.log main; location / { root html/crushlinux; index index.html index.htm; }
location ~ /status {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.cloud.com;
charset utf-8;
access_log logs/cloud.access.log main;
location / {
root html/cloud;
index index.html index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@www ~]# killall -3 nginx //正常停止
[root@www ~]# nginx //正常启动
在windows 环境测试
修改hosts 文件
所在位置:
windows
c:windowssystem32driversetc