1.http协议
2.实现www服务的常用web软件
产品:nginx,Apache (静态web软件)
3.组合:
lamp:Linux,Apache,mysql,PHP
lnmp:Linux,nginx,mysql,PHP
4.nginx(engin x)介绍
Nginx:www服务软件,俄罗斯人开发的web产品
Nginx本身是一款静态(HTML,js,css,jpg等)的www软件,不能解析动态的PHP,JSP,do
最大特点:
静态文件小(1M),高并发,同时占用的资源很少。
5.Nginx服务从大的方面的功能
a.www web服务,邮件服务,邮件代理
b.负载均衡(反向代理proxy)
c.web cache
6.Nginx的特点
a.高并发
b.配置简单,灵活,轻量
c.资源占用少
d.支持epoll模型,使得Nginx可以支持高并发
7.Nginx的应用场合
a.提供静态服务,图片,视频服务
b.提供动态服务,Nginx+fastcgi的方式运行PHP,jsp
c.提供反向代理(proxy)服务,或者称为负载均衡
d.提供缓存服务
8.Nginx虚拟主机
a.基于域名的虚拟主机。通过域名来区分虚拟主机。==>应用:外部网站*****
b.基于端口的虚拟主机,通过端口来区分虚拟主机。==>应用:内部网站,后台***
9.Nginx安装
1>安装pcre
因为Nginx有HTTP rewrite模块
yum install pcre pcre-devel -y
安装OpenSSL
yum install openssl-devel -y
2>下载Nginx并解压
这里遇到一个需要思考的新问题:tar解压后Nginx目录的属主和属组都不是root
useradd nginx -s /sbin/nologin -M #添加用户
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
安装完成后创建软链接便于管理
报错:
checking for error:C conmpiler cc is not found
解决:未安装gcc
nginx: [emerg] getpwnam("nginx") failed
解决:未创建Nginx用户和组
安装完成后客户端浏览器输入Nginx服务地址无法进入"welcome to nginx"界面但能ping通
解决:查看Nginx服务端防火墙状态
3>安装完成后
在安装目录下会生成四个目录:
conf 配置
html 默认网站目录
logs 错误,访问日志
sbin 启动命令
一、基于域名的虚拟主机配置步骤:
www.ready.org ===> html/www
bbs.ready.org ===> html/bbs
a.配置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.ready.org; #设置虚拟主机的域名
location / {
root html/www; #网页根目录
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.ready.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
b.创建站点目录,并编辑首页文件的测试内容
mkdir ../html/{www,bbs}
echo "www.ready.org" > ../html/www/index.html
echo "bbs.ready.org" > ../html/bbs/index.html
*****c.检查语法,重新加载nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
错误:nginx: [error] invalid PID number "" in "/application/nginx-1.6.3/logs/nginx.pid"
因为是加载配置文件报的错,所以用-c 选项指定配置文件 ,如此指明后问题解决
/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
c.配置host,测试
188.188.188.119 www.ready.org bbs.ready.org >> /etc/hosts
curl www.ready.org
curl bbs.ready.org
二、基于端口的虚拟主机配置步骤:
a.备份nginx.conf
b.修改ngin.conf中虚拟主机的端口号
不同端口的域名可设置相同
三、基于IP的虚拟主机配置
四、若虚拟主机较多可用在主配置文件里用include命令进行管理
worker_processes 1; #启动进程数
events {
worker_connections 1024; #单进程最大连接数
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#nginx vhosts config
include extra/www.conf; #在nginx安装目录下的conf/目录定位/extra/目录的各文件
include extra/bbs.conf;
{
实质就是将配置好的虚拟主机配置单独生成文件,用主配置文件配置定向调用,便于管理
设置好后要先检查语法 -t
****nginx访问日志介绍
log_format 用来定义记录日志的格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr 记录访问网站的客户端地址
$remote_user 远程客户端用户名称
$time_local 记录访问时间与时区
$request 用户的HTTP请求起始信息
$status http状态码,记录请求返回的状态 如404,200等
$body_bytes_sent 服务器发送给客户端的响应body字节数
$http_user_agent 记录客户端访问信息,如:浏览器,手机客户端等
$http_referer 记录此请求是从哪个链接访问过来的,可以根据referer进行防盗链设置
$http_x_forwarded_for 当前端有代理服务器时,设置web节点客户端地址的配置,此参数生效的前提是服务器上也要进行相关的x_forwarded_for配置
****记录日志access_log参数说明
默认配置:#access_log logs/access.log main;
放置位置:http,server,location,if in location
access_log必须放在log_format模块下
新版的nginx配置需要放在server上端,否则会报错[warn] the "log_format" directive may be used only on "http" level
access_log优化:
企业生产中最好按时间断定时生成log文件,便于查看分析
参考脚本:
#!/bin/bash
#
Dateformat=`date +%Y%m%d` #时间格式
Basedir="/application/nginx" #nginx安装目录
Nginxlogdir="$Basedir/logs" #nginx的log目录
Logname="access_www" #配置文件中定义的log文件
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log #将默认的log文件重命名
$Basedir/sbin/nginx -s reload #重新生成默认log文件
知识点:
a.别名
b.iclude
c.nginx status
d.错误日志
e.访问日志
f.日志轮询,按天
g.rewrite模块*****
官方文档:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
转载于:https://blog.51cto.com/13322786/2161635