本文共 12533 字,大约阅读时间需要 41 分钟。
1、http协议包含很多功能。
上网的×××w是http功能之一。×××w服务默认端口80,OSI 第七层 应用层协议。https 默认端口443,加密的http服务。2、实现×××w服务的常用web软件。
产品:nginx,apache(解决静态web软件)3、经典流行的web组合。
lamp(linux apache mysql php) ===》经典lnmp(linux nginx mysql php)====》国内流行4、nginx(engine x)介绍
nginx,×××w服务软件,俄罗斯人开发,开源的×××w服务软件,一共780k,c语言开发的。nginx本身是一款静态(html,js,css,jpg等)的×××w软件,不能解析动态的php,jsp,do。5、nginx服务从大的方面的功能:
a. ×××w web服务,邮件服务,邮件代理b.负载均衡(反向代理proxy)c.web cache(web缓存),相当于squid(CDN主要使用squid)。6、nginx特点:
最大的特点:静态小文件(低于1M),支持高并发,同时占用的资源很少。3w并发,10个进程,消耗150M。1)配置简单,灵活,轻量。2)支持高并发(静态小文件),静态几万的并发。3)占用资源少。2w并发,开10个线程服务,内存消耗几百M。4)功能种类比较多(web,cache,proxy),每一个功能都不是特别强。5)支持epoll模型。使得nginx可以支持高并发。apache使用select模型。6)nginx可以配合动态服务(fastcgi接口)。7)利用nginx可以对ip限速,可以限制连接数。它所具备的其他×××w服务特性如下:
支持基于名字、端口以及IP的多虚拟主机站点。支持rewrite模块,支持URL重写及正则表达式匹配。支持基于客户端IP地址和HTTP基本认证的访问控制。支持http响应速率限制。支持同一IP地址的并发连接或请求数限制。7、nginx的应用场景:
1)提供静态服务(图片,视频服务),另一个lighttpd。并发:几万并发。html,js,css,.flv,jpg,gif等。类似lighttpd。2)提供动态服务,nginx+fastcgi的方式运行php,jsp。动态并发:500-1500.apache+php,lighttpd+fcgi php。3)提供反向代理(proxy)服务,或称为负载均衡。日PV2000W以下,并发1万以下,都可以直接用nginx做反向代理。软件:haproxy,硬件:F5,A104)提供缓存服务。类似squid,varnish,ats。8、nginx支持虚拟主机:
一个server标签段就是一个虚拟主机。a、基于域名的虚拟主机,通过域名来区分虚拟主机==>应用:外部网站**b、基于端口的虚拟主机,通过端口来区分虚拟主机==>应用:公司内部网站,网站的后台c、基于IP的虚拟主机,几乎不用。不支持ifconfig别名,配置文件可以。9、安装Nginx:(http://nginx.org)
a、首先安装PCREPcre全称(Perl Compatible Regular Expressions),中文perl兼容正则表达式,官方网站:HTTP rewrite module requires the PCRE library。安装: rpm -qa pcre pcre-develyum install pcre pcre-devel -yrpm -qa pcre pcre-develb、然后安装OpenSSLSSL modules require the OpenSSL library。安装: yum install openssl-devel -yc、安装nginxmkdir -p /home/oldboy/tools cd /home/oldboy/tools wget -q http://nginx.org/download/nginx-1.6.3.tar.gz # -q 不显示输出 useradd nginx -s /sbin/nologin -M tar xf nginx-1.6.3.tar.gz cd nginx-1.6.3 ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module ` # --prefix=/application/nginx-1.6.3/ 设置安装路径` ` --with-http_stub_status_module 加密的ssl模块 --with-http_ssl_module 状态模块` make make install ln -s /application/nginx-1.6.3 /application/nginx # 创建软链接 ls -l /application/nginx/
启动nginx服务: /application/nginx/sbin/nginx
查看配置信息(怎么编译的):/application/nginx/sbin/nginx -V查看日志文件:cat /var/log/messages查看nginx下的日志文件:cat /application/nginx/logs/error.log[root@lnmp02 nginx]# ls -l|grep -v temp总用量 36drwxr-xr-x 2 root root 4096 12月 18 09:56 conf# 配置文件的目录
drwxr-xr-x 2 root root 4096 12月 18 09:56 html # 默认网站目录(站点)
drwxr-xr-x 2 root root 4096 12月 18 10:07 logs # 错误,访问日志
drwxr-xr-x 2 root root 4096 12月 18 09:56 sbin # 启动命令
10、基于域名的虚拟主机配置步骤:
×××w.etiantian.org ====>html/×××w,浏览域名显示×××w.etiantian.orgbbs.etiantian.org====>html/bbs,浏览域名显示bbs.etiantian.orga、配置nginx.conf[root@lnmp02 conf]# cat nginx.confworker_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 ×××w.etiantian.org; location / { root html/×××w; index index.html index.htm; } } server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } }}
将配置文件里所有的注释行都过滤出去:
[root@lnmp02 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
b、创建站点目录 [root@lnmp02 conf]# mkdir ../html/{×××w,bbs} -p [root@lnmp02 conf]# echo "×××w.etiantian.org" > ../html/×××w/index.html [root@lnmp02 conf]# echo "bbs.etiantian.org" > ../html/bbs/index.html
c、检查语法,重新加载nginx
[root@lnmp02 conf]# /application/nginx/sbin/nginx -t [root@lnmp02 conf]# /application/nginx/sbin/nginx -s reload
d、配置hosts,测试:
linux client:[root@lnmp02 conf]# tail -1 /etc/hosts 192.168.153.135 ×××w.etiantian.org bbs.etiantian.org # 添加这一行 [root@lnmp02 conf]# curl ×××w.etiantian.org # 测试 ×××w.etiantian.org [root@lnmp02 conf]# curl bbs.etiantian.org bbs.etiantian.org
windows: C:\Windows\System32\drivers\etc\hosts 添加这一行:192.168.153.135 ×××w.etiantian.org bbs.etiantian.org # Ctrl + s 保存 浏览器里访问×××w.etiantian.org bbs.etiantian.org域名。
11、利用include功能优化Nginx的配置文件。
a、配置nginx.conf文件``[root@lnmp02 conf]# cat nginx.conf 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/×××w.conf; include extra/bbs.conf; include extra/blog.conf; }
b、创建extra目录然后在目录下对应的文件添加配置。
[root@lnmp02 conf]# mkdir extra [root@lnmp02 conf]# sed -n "10,17p" nginx.conf.ori.1 >extra/×××w.conf [root@lnmp02 conf]# sed -n "18,25p" nginx.conf.ori.1 >extra/bbs.conf [root@lnmp02 conf]# sed -n "26,33p" nginx.conf.ori.1 >extra/blog.conf
c、重新加载nginx服务
` [root@lnmp02 conf]# ../sbin/nginx -s reload
` 12、Nginx别名作用及配置实战。
[root@lnmp02 conf]# cat extra/×××w.conf `` server { listen 80;`` server_name ×××w.etiantian.org etiantian.org; location / { root html/×××w; index index.html index.htm; } }
b、在linux下/etc/hosts下做DNS解析。
[root@lnmp02 conf]# tail -1 /etc/hosts 192.168.153.135 ×××w.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
c、重加加载nginx服务。
13、Nginx状态信息配置实战。
a、查看状态配置信息``[root@lnmp02 conf]# ../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
b、生成状态配置,并增加状态配置参数。
cat >>/application/nginx/conf/extra/status.conf<
c、在配置文件里写入状态的文件名,并配置DNS解析,最后重新加载。
[root@lnmp02 conf]# cat nginx.confworker_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/×××w.conf; include extra/bbs.conf; include extra/blog.conf; include extra/status.conf; }
14、一、访问日志作用及格式。
a、找出日志配置[root@lnmp02 conf]# sed -n "21,23p" nginx.conf.default #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
b、将上面的日志配置放到nginx.conf主配置里。````
[root@lnmp02 conf]# cat nginx.conf worker_processes 1; 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"'; #nginx vhosts config include extra/×××w.conf; include extra/bbs.conf; include extra/blog.conf; include extra/status.conf; }
二、记录日志 a、默认配置:access_log logs/access.log combined 访问日志 日志文件 格式 将配置分别放到extra/×××w.conf extra/bbs.conf extra/blog.conf文件下。
[root@lnmp02 conf]# cat extra/×××w.conf server { listen 80; server_name ×××w.etiantian.org etiantian.org; location / { root html/×××w; index index.html index.htm; } access_log logs/access_×××w.log main; } #在网页上访问可以查看日志的实时变化:tail -f ../logs/access_×××w.log
b、访问日志优化及工作日志轮询。
[root@lnmp02 scripts]# cat cut_nginx_log.sh ########################################################################## File Name: cut_nginx_log.sh# Author: yushanshuai# mail: 872523367@.com# Created Time: 2017年12月21日 星期四 07时38分10秒##########################################################################!/bin/bashDateformat=`date +%Y%m%d`Basedir="/application/nginx"``Nginxlogdir="$Basedir/logs"Logname="access×××w"[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1[ -f ${Logname}.log ]||exit 1/bin/mv ${Logname}.log ${Dateformat}${Logname}.log$Basedir/sbin/nginx -s reload
然后放到定时任务里:规定每天00点生成一个日志。(查看:ll /application/nginx/logs/)
[root@lnmp02 scripts]# crontab -l`` echo yushanshuai >>/server/log/oldboy.txt#cron job for ett by oldboy 2017-6-3000 09,14 6,0 /bin/sh /server/script/oldboy.sh >/dev/null 2>&100 /2 /bin/sh /server/scripts/tar.sh >/dev/null 2>&1################nginx log00 00 /bin/sh /server/scripts/cutnginxlog.sh & >/dev/null
15、nginx rewrite 301跳转实战案例
a、将配置放到配置文件里。`` [root@lnmp02 conf]# cat extra/×××w.conf server { listen 80; servername etiantian.org; rewrite ^/(.) http://×××w.etiantian.org/$1 permanent; } server { listen 80; servername ×××w.etiantian.org; location / { root html/×××w; index index.html index.htm; } accesslog logs/access×××w.log main; }
b、重新加载后,查看:curl etiantian.org -I*
1、安装nginx(上面有详细过程)
2、负载均衡需要哪些模块:1)、upstream模块(定义有哪些节点,调度算法是什么,调度算法默认是轮询。定义节点池)ngx_http_upstream_module2)、http_proxy模块(负责请求的转发,用户访问先找到它。调用节点池)proxy_pass 发给定义好的指定的upstream名字。3、配置[root@lb01 conf]# cat nginx.confworker_processes 1;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"';upstream bbs_server_pools{# ip_hash; ##########默认是轮询,加上是保持会话,只在一台web服务器上显示。 server 192.168.153.135:80; # weight=3; ########weight是权重,=3,是3:1轮询。 server 192.168.153.133:80;}#include extra/lb_www.conf; include extra/lb_bbs.conf;#include extra/lb_blog.conf;}
[root@lb01 conf]# cat extra/lb_bbs.conf server { listen 80; server_name bbs.etiantian.org; location / { proxy_pass http://bbs_server_pools; proxy_set_header Host $host; ########访问bbs就会指定域名bbs。 proxy_set_header X-Forwarded-For $remote_addr; #######$remote_addr 这是客户端的地址,将客户端的地址传给 X-Forwarded-For ,X-Forwarded-For 通过反向代理传给web服务器的功能。 }}
检查:(用mysql-server服务器作为客户端检查)
1、做一下DNS解析。[root@mysql-server ~]# cat /etc/hosts127.0.0.1 localhost.localdomain localhost.localdomain localhost4 localhost4.localdomain4 mysql-server::1 localhost.localdomain localhost.localdomain localhost6 localhost6.localdomain6 localhost mysql-server192.168.153.141 www.etiantian.org bbs.etiantian.org blog.etiantian.org
2、a、轮询(默认)
[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;donebbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135
b、会话保持(ip_hash)
[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;donebbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135
c、权重轮询(weight=3 3:1)
[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;donebbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 133bbs.etiantian.org 135bbs.etiantian.org 135bbs.etiantian.org 135
d、X-Forwarded-For(返回客户端的ip地址到后端服务器)
[root@lnmp02 bbs]# tail -1 /application/nginx/logs/access_bbs.log 192.168.153.141 - - [02/Jan/2018:14:37:25 +0800] "GET /index.html HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "192.168.153.130"
转载于:https://blog.51cto.com/13688462/2321527