Nginx使用
命令集
#gcc gcc-c++ make
mount /dev/sr0 /media/cdrom
yum -y install pcre-devel openssl-devel
tar xf nginx-1.10.2.tar.gz -C /usr/src
cd /usr/src/nginx-1.10.2/
useradd -M -s /sbin/nologin nginx
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin
cd /usr/local/nginx/conf/
egrep -v "#|^$" nginx.conf.default > nginx.conf
echo "`hostname -I` www.`hostname`.com" >> /etc/hosts
echo "PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
source /etc/profile
编译安装
[[email protected] ~]# mount /dev/sr0 /media/cdrom
#安装他的支持插件
[[email protected] ~]# yum -y install pcre-devel openssl-devel
#编译安装
[[email protected] ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/nginx-1.10.2/
#先创建程序用户,因为编译时候需要输入自己创建的程序用户
[[email protected] nginx-1.10.2]# useradd -M -s /sbin/nologin nginx
[[email protected] nginx-1.10.2]# id nginx
[[email protected] nginx-1.10.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[[email protected] nginx-1.10.2]# make && make install
#改全局变量配置文件,添加程序用户的命令,使其找到
[[email protected] nginx-1.10.2]# vim /etc/profile
79 export PATH=$PATH:/usr/local/nginx/sbin #添加
#快速生效全局变量
[[email protected] nginx-1.10.2]# source /etc/profile
#映射文件
[[email protected] conf]# echo "`hostname -I` www.`hostname`.com" >> /etc/hosts
简单使用nginx
别忘了IP和域名添加到/etc/hosts中
window的映射文件:C:/Windows/System32/drivers/etc/hosts #复制粘贴到其他地方然后记事本打开,最后覆盖回原位置
修改配置文件
[[email protected] nginx-1.10.2]# cd /usr/local/nginx/conf
nginx.conf.default nginx.conf #默认的原始状态(编译时没加入任何我们的元素)
nginx.conf #自己编译的和默认的不同
mime.types mime.types.default #支持媒体类型,
fastcgi.conf fastcgi.conf.default
fastcgi_params fastcgi_params.default
#uwsgi_params #uwsgi_params.default #koi-utf #koi-win
#scgi_params #scgi_params.default #win-utf
开始简单使用,使用的时默认的配置
[[email protected] nginx-1.10.2]# cd /usr/local/nginx/conf/
[[email protected] conf]# cat nginx.conf.default |egrep -v “#|^$” > nginx.conf
[[email protected] conf]# vim nginx.conf
worker_processes 1; #主进程数量 是1,一般几核的虚拟机就几个
events {
worker_connections 1024; #默认进程里线程数1024 事件函数,只设置这上两个没有意义,还要配合设置Linux系统参数,比如文件打开数 我们修改为1024的20倍,最大并发1024个
}
http { #Web服务 #解释下面,此目录位置为相对配置路径/usr/local/ngin/conf/
include mime.types; #include引入;引入后面的文件 mime.types是支持文件类型
default_type application/octet-stream; #默认的就行
sendfile on; #默认就行 高效文件传输
keepalive_timeout 65; #默认就行,连接保持,为了节约服务器资源, nginx不在乎开不开,但是Apache需要考虑是否开启,他的接待能力低。
server { #这就是自制网页,一个server代表一个虚拟网站
listen 80; #默认端口 80
server_name localhost; #域名
location / {
root html; #root代表根 html代表目录相对路径, 首页等在里面 相对于Nginx安装目录 和上面的相对路径不同,除了include的相对于配置文件, 其他的都是相对于安装目录
index index.html index.htm; #索引,网站首页文件,修改首页
}
error_page 500 502 503 504 /50x.html; #错误信息,先删除
location = /50x.html {
root html;
}
}
}
修改后
worker_processes 1;
events {
worker_connections 20480;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.wanghaiyang.com;
location / {
root html;
index index.html index.htm;
}
}
}
安全 禁止非法访问
我们一般都是直接输入域名访问,通过域名解析DNS转换成IP地址。但是,黑客是直接输入IP地址访问,为了防止DNS公司解析到他的地址。
curl命令:模拟访问
curl IP 会默认直接访问到我们设置的第一个网站,我们为了防止黑客,要做个安全设置
server {
listen 80 default_server;
server_name www.wanghaiyang.com;
location / {
return 403;
}
}
这样就可以让只输入IP的黑客直接跳出
include 引用
相对于配置文件的位置/usr/local/nginx/conf目录下,把每一个网页单独创建个文件
比如 mkdir /usr/local/nginx/conf/wangye
cd /usr/local/nginx/conf/wangye
cat > /usr/local/nginx/conf/wangye/zhuye << qqq
内容
qqq
在配置文件里编写(可以相对路径)原配置文件的网页位置 降低耦合度
include wangye/zhuye;
重启服务
/usr/local/nginx/sbin/nginx -s reload
启动时没有pid
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
解决: ./nginx -c /usr/local/nginx/conf/nginx.conf
Nginx 状态信息检测模块
检测有多少人访问,处理了多少信息,多少次三次握手,这样的数据
编译安装时 有–with-http_stub_status_module
模块就行
别忘了映射
设定:也写到分支配置文件里,就是include引用
server {
listen 80 ;
server_name status.wanghaiyang.com;
location / {
stub_status on;#开启状态信息功能
access_log off; #不记录访问日志
allow IP;#允许的IP
deny all;#除了允许的都拒绝了
}
}
[[email protected] ~]# curl status.wanghaiyang.com
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
Active connections: 1 #处于活动的连接1次
server表示Nginx启动到现在共成功处理了2个连接
accepts表示Nginx启动到现在共成功创建了2次握手:请求丢失数=(握手数-连接数),可以看出本次连接没有丢失
handled requests 表示总共处理了2次请求
Reading为Nginx读取到哭护短的Header信息数
Writing为Nginx返回给客户端的Header信息数
Waiting为Nginx已经处理万正在等候下一次请求指令的驻留连接。在开启keep-alive的情况下,这个值等于active-(reading+writing)
#这个信息为了安全不可以对外看
Nginx不会自己切割日志。让它太大不行
所以我们需要写个脚本
cat /server/scripts/cut_nginx_log.sh
#!/bin/bash
#日志切割脚本可挂定时任务,每天00点整点执行
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nignx"
Nginxlogdir="$Basedir/logs"
Logname="access"
[ -d $Ngingxlogdir ] && $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
cat >> /var/spool/cron/root << kff
#cut nginx access log
00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh &>dev/null
Nginx location
非常重要的过滤函数,可以通过正则表达式来匹配过滤不同的URL里的URI部分
匹配到不同的URI进入到不同的location,也就剋进到不同的网页不同
过滤的优先级很重要,重点 https://blog.****.net/qq_15766181/article/details/72829672
优先级:从上到下
- 最高级 等号 “=” 精确匹配
- 特殊正则(^~)
- 普通匹配 (或*)
- 字符串前缀匹配(/images/)
- 默认匹配(/)
匹配两种特殊字符"~"和"~*"的区别:"~"用于区分大小写,"~*"不必区分大小写。还可以使用逻辑操作符"!",也就是取反:"! ~" 和 "! ~*" 。
此外"^~"的作用是在进行常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的哪个字符串匹配的location配置中有此前缀,不继续匹配正则表达式的选项。
server {
listen 80;
server_name www.wanghaiyang.com;
root html;
location / {
return 401; #默认的,最小级别
}
location = / { #精确找根 精确是最大级别
return 402;
}
location = /images/ { #精确查找根下images/(查找根下images都不行,少一个/符号)
return 501;
}
location /documents/ { #模糊查找以根开头的字符串(前缀型,必须从根部分开始)
return 403;
}
location ^~ /images/ { #模糊查找,前缀匹配,比区分大小写,并且支持正则
return 404;
}
location ~ /images/ { #模糊查找,前缀匹配,区分大小写,支持正则
return 501;
}
location ~* \.(gif|jpg|jpeg)$ { #不区分大小写,模糊查找以.gif等为结尾的
return 500;
}
}
Nginx rewrite
功能:实现URL地址重写
需要PCRE软件支持,即通过Perl兼容正则表达式语法进规则匹配,默认参数编译时,Nginx就会安装支持rewrite模块。
四种flag标记:
last:本条匹配完后,继续向下匹配新的location URI规则
break:本条规则匹配完成即终止,不再匹配后面的任何规则
redirect:返回302临时重定向,浏览器地址栏不会显示跳转后的URL地址
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
rewrite ^/(.*) http://www.why .com/$1 permanent;
Nginx 创建加密用户设置密码
yum-y install httpd
which htpasswd
htpasswd -bc /usr/local/nignx/conf/htpasswd yunjisuan 123123 (创建完,密码是加密的)
server {
listen 80 ;
server_name status.wanghaiyang.com;
location / {
stub_status on;#开启状态信息功能
access_log off; #不记录访问日志
allow IP;#允许的IP
deny all;#除了允许的都拒绝了
anth_basic "yunjisuan training";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
auth_basic:验证的基本信息选项(双引号里是验证窗口名字)
auth_basic_user_file:验证的用户文件(绝对路径)
加这个写在哪个server都行