Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。php
Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
延伸版本:tengine(淘宝)、openresrt(章亦春)。html
代码或软件包必须从官网下载,第三方的软件包可能会有后门、安全漏洞等风险。nginx
$ wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src # 下载nginx $ cd /usr/src $ tar xvf nginx-1.15.5.tar.gz $ cd nginx-1.15.5 $ yum -y install gcc # nginx是c写的 $ yum -y install pcre-devel # url重写用到的包 $ yum -y install zlib zlib-devel # 解压缩用到的包
configure经常使用参数有两个:--prefix=path;--help 配置参数web
$ ./configure --prefix=/usr/local/nginx .... Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" # nginx安装路径 nginx binary file: "/usr/local/nginx/sbin/nginx" # nginx启动文件 nginx modules path: "/usr/local/nginx/modules" # nginx所使用的模块 nginx configuration prefix: "/usr/local/nginx/conf" # nginx配置文件目录 nginx configuration file: "/usr/local/nginx/conf/nginx.conf" # nginx配置文件 nginx pid file: "/usr/local/nginx/logs/nginx.pid" # pid文件,进程号 nginx error log file: "/usr/local/nginx/logs/error.log" # nginx错误日志 nginx http access log file: "/usr/local/nginx/logs/access.log" # nginx访问日志(谁访问了网站) nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
# 编译源码生成可执行程序 依赖gcc $ make -j4 # 安装程序 $ make install
一个服务有三要素:端口、监听地址、协议。nginx使用的是七层协议(http),使用端口:80,监听地址默认是0.0.0.0(全部机器)。
在nginx启动前,须要先检查端口是否被占用:chrome
$ lsof -i:80 # 第一种方法 # contos7须要安装netstat工具 $ yum install net-tools $ netstat -ntpl # 第二种方法 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1317/master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1095/sshd tcp6 0 0 ::1:25 :::* LISTEN 1317/master tcp6 0 0 :::22 :::* LISTEN 1095/sshd
查看发现没有人使用80端口,启动nginx程序:apache
$ /usr/local/nginx/sbin/nginx $ lsof -i :80 # 查看服务是否启动 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 11524 root 6u IPv4 29204 0t0 TCP *:http (LISTEN) nginx 11525 nobody 6u IPv4 29204 0t0 TCP *:http (LISTEN)
$ yum install elinks # 除了elinks还有curl、lynx等文本浏览器 $ elinks http://192.168.10.42 -dump
查看文件:/usr/local/nginx/conf/nginx.confjson
#启动子进程程序默认用户 #user nobody; #一个主进程和多个工做进程。工做进程是单进程的,且不须要特殊受权便可运行;这里定义的是工做进程数量 worker_processes 1; #全局错误日志的位置及日志格式 #error_log是指令定义的是错误日志 后面是路径及日志级别 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #定义pid文件在哪,文件中的是主进程号,/usr/local/nginx/logs/nginx.pid #pid logs/nginx.pid; events { #每一个工做进程最大的并发数(一个工做进程能够有多少个线程) worker_connections 1024; } #http服务器设置 http { #设定mime类型,类型由mime.type文件定义 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"'; #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址; #$remote_user:用来记录客户端用户名称; #$time_local: 用来记录访问时间与时区; #$request: 用来记录请求的url与http协议; #$status: 用来记录请求状态;成功是200, #$body_bytes_sent :记录发送给客户端文件主体内容大小; #$http_referer:用来记录从那个页面连接访问过来的; #$http_user_agent:记录客户浏览器的相关信息; #全局访问日志路径 #access_log logs/access.log main; # 日志放置的位置 #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。若是用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,下降系统uptime。 sendfile on; #此选项容许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用 #tcp_nopush on; #长链接超时时间 #keepalive_timeout 0; keepalive_timeout 65; #开启压缩 #gzip on; #配置虚拟主机 server { #虚拟主机使用的端口 listen 80; #虚拟主机域名 server_name localhost; #虚拟主机支持的字符集 #charset koi8-r; #虚拟主机的访问日志路径 #access_log logs/host.access.log main; #定义web根路径 location / { #根目录路径 root html; #索引页 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #根据错误码 返回对应的页面 error_page 500 502 503 504 /50x.html; #定义页面路径 location = /50x.html { root html; } #定义反向代理服务器 数据服务器是lamp模型(apache发布) # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} #定义PHP为本机服务的模型(经过nginx发布) # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #拒绝apache DR目录及子目录下的.htaccess文件访问 #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} #https的配置方案 # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
这一行指定了启动子进程程序的默认用户。centos
$ lsof -i :80 # 查看服务是否启动 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 11524 root 6u IPv4 29204 0t0 TCP *:http (LISTEN) nginx 11525 nobody 6u IPv4 29204 0t0 TCP *:http (LISTEN)
父进程是由root启动的,子进程则是用nobody启动的。浏览器
一个主进程和多个工程进程(子进程)。工做进程是单进程的,且不须要特殊受权便可运行。
这里定义的是工做进程数量。通常状况建议有几个核就写几个。
# 安装kellall命令 $ yum search killall $ yum -y install psmisc # 杀死nginx $ killall nginx $ lsof -i :80 # 查看是否杀死程序 # 添加新用户 $ useradd -s /sbin/nologin -r www # 修改nginx.conf的进程 user www; worker_processes 4; # 再次启动nginx $ /usr/local/nginx/sbin/nginx $ lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 11661 root 6u IPv4 30504 0t0 TCP *:http (LISTEN) nginx 11662 www 6u IPv4 30504 0t0 TCP *:http (LISTEN) nginx 11663 www 6u IPv4 30504 0t0 TCP *:http (LISTEN) nginx 11664 www 6u IPv4 30504 0t0 TCP *:http (LISTEN) nginx 11665 www 6u IPv4 30504 0t0 TCP *:http (LISTEN)
这个指定的是每一个工做进程(子进程)最大的并发数,也就是一个工做进程能够有多少个线程。
工做进程 * 线程 就能够获得并发的数量。
一个server叫作一个虚拟主机,server的主要配置。
默认网站概念:当Nginx配置文件中有且只有一个Server的时候,该Server就被Nginx认为是默认网站,全部发给Nginx服务器80端口的数据都会默认给该Server。
所以只有一个server的时候叫作默认网站,有多个server的时候则叫作虚拟主机。
http { # 默认网站 server { listen 80; server_name localhost; charset utf-8; # 修改字符集utf-8 #access_log logs/host.access.log main; # 使用默认日志路径 location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { # 错误代码页的位置 root html; } } }
访问控制配置规则是容许个别,拒绝全部。
location /a { allow 192.168.1.0/24; # 容许访问的网段 deny all; #return 404; return http://www.jd.com; }
对nginx.conf文件作以下配置:
# 网站根目录的相对路径,定位的路径在:/usr/local/nginx/html/a location /a { # 只容许本机访问拒绝全部 allow 127.0.0.1; deny all; }
重启nginx服务测试验证:
$ /usr/local/nginx/sbin/nginx -g ../conf/nginx.conf # 启动前校验配置文件是否有问题 $ /usr/local/nginx/sbin/nginx $ lsof -i :80 # 检验服务是否启动 $ elinks http://192.168.31.42/a --dump # 访问被拒绝 403 Forbidden $ elinks http://127.0.0.1/a --dump aaaa
注意这里返回的是403,告诉了访问者,说明这里有目录,只是不容许访问。为了迷惑访问者再也不尝试访问可作以下配置:
# 网站根目录的相对路径,定位的路径在:/usr/local/nginx/html/a location /a { # 只容许本机访问拒绝全部 allow 127.0.0.1; deny all; return 404; # 还能够返回5xx系列,或者直接返回url:return www.jd.com }
配置修改后,不想从新启动服务,能够给服务发送信号告诉它配置文件重读:
$ killall -s HUP nginx $ lsof -i :80 $ elinks http://192.168.31.42/a --dump 404 Not Found
登陆验证的规则是:任何人均可以访问,可是须要凭用户密码才能访问。常见格式以下所示:
location /b { auth_basic "登录验证"; auth_basic_user_file /etc/nginx/htpasswd; }
语法:auth_basic string | off;
默认值是:auth_basic off;
语法:auth_basic_user_file file;
$ mkdir /etc/nginx/ $ touch /etc/nginx/htpasswd $ echo 'sko:123456' > /etc/nginx/htpasswd $ killall -s HUP nginx ####打开浏览器访问192.168.31.42#####
此时打开浏览器访问192.168.31.42/b/能够发现弹出了登陆框,输入用户名和密码后并不能登陆成功,由于密码并无加密。
使用htpasswd实现密码加密:-c是指定文件路径没有文件会自动建立;-m是生成加密。文件后的sky是指定的用户名。
# 安装htpasswd $ yum -y install httpd-tools # 查看包来源 $ rpm -qf `which htpasswd` httpd-tools-2.4.6-88.el7.centos.x86_64 # 建立加密密码的用户 $ htpasswd -m /etc/nginx/htpasswd sky New password: # 输入密码 Re-type new password: # 重复密码 Adding password for user sky $ cat /etc/nginx/htpasswd sky:$apr1$kRE.rFXV$lbR2/Ga09e/i91qGrg/931
此时打开浏览器访问192.168.31.42/b/,输入sky/123后,登陆成功,看到页面信息bbbb。
Nginx访问日志主要有两个参数控制:
$remote_addr #记录访问网站的客户端地址 $remote_user #远程客户端用户名 $time_local #记录访问时间与时区 $request #用户的http请求起始行信息 $status #http状态码,记录请求返回的状态码,例如:200、30一、404等 $body_bytes_sent #服务器发送给客户端的响应body字节数 $http_referer #记录这次请求是从哪一个链接访问过来的,能够根据该参数进 行防盗链设置。 $http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等 $http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端 地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设 置
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"'; # 第一步:自定义日志格式 log_format hqs_01 '[$time_local] $remote_addr "$request" $status' #access_log logs/access.log main; #####其余代码省略#### server { listen 80; server_name localhost; #charset koi8-r; charset utf-8; #access_log logs/host.access.log main; # 第二步:自定义日志 access_log logs/host.access.log hqs_01; } }
重启服务后,从浏览器上访问服务,查看日志信息:
$ tailf logs/host.access.log [11/Dec/2018:15:51:49 +0800] 192.168.31.28 "GET /b/ HTTP/1.1" 304sendfileon [11/Dec/2018:15:52:28 +0800] 192.168.31.28 "GET / HTTP/1.1" 200sendfileon
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"'; # 自定义日志格式 log_format hqs_01 '[$time_local] $remote_addr "$request" $status'; # 自定义json日志格式 log_format main_json '{"@timestamp":"$time_local",' '"client_ip": "$remote_addr",' '"request": "$request",' '"status": "$status",' '"bytes": "$body_bytes_sent",' '"x_forwarded": "$http_x_forwarded_for",' '"referer": "$http_referer"' '}'; server { listen 80; server_name localhost; #charset koi8-r; charset utf-8; #access_log logs/host.access.log main; # 自定义日志 access_log logs/host.access.log main_json; location / { root html; index index.html index.htm; }
从新启动nginx后,查看日志,能够看到新的日志格式:
$ killall nginx $ sbin/nginx $ tailf logs/host.access.log {"@timestamp":"11/Dec/2018:16:25:30 +0800","client_ip": "192.168.31.28","request": "GET / HTTP/1.1","status": "304","bytes": "0","x_forwarded": "-","referer": "-"} {"@timestamp":"11/Dec/2018:16:25:34 +0800","client_ip": "192.168.31.28","request": "GET /a HTTP/1.1","status": "404","bytes": "555","x_forwarded": "-","referer": "-"}
盗链是指服务提供商本身不提供服务的内容,经过技术手段绕过其它有利益的最终用户界面(如广告),直接在本身的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供不多的资源,而真正的服务提供商却得不到任何的收益。
百度就是最大的盗链公司,不少图片和数据在百度就看到了。服务提供商须要支付带宽,但访问和用户并无增长,反而会愈来愈少。
防盗链:在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿连接到当前的网页或文件。经过referer,网站能够检测目标网页访问的来源网页,若是是资源文件,则能够跟踪到显示它的网页地址。有了referer跟踪来源,就能够经过技术手段来进行处理,一旦检测到来源不是本站即进行阻止或者返回指定的页面。
location /c { valid_referers none blocked *.ayitula.com; if ($invalid_referer) { return 403 } }
valid_referers定义的是有效的referer:
valid_referers none blocked *.ayitula.com; - none:没有referer字段的(用户直接过来访问的) - blocked:硬件防火墙标识的 - *.ayitula.com:本身的内网服务器
if ($invalid_referer){return 403}说明全部不知足valid_referer的referer都是无效的referer,直接返回403页面。
若是要实现宽广匹配:~是匹配,*是不区分大小写 .(png|gif|bmp)$:域名内任意以png/gif/bmp结尾的。
location ~* \.(png|gif|bmp)$ { valid_referers none blocked *.ayitula.com; if ($invalid_referer) { return 403 } }
发布一个内部HTML网站,要求对图片目录作防盗链设置,用户只能来自192.168.31.0网段,访问网站须要使用帐号密码。