一:七层分发理论
重点是http协议
1:客户端请求七层封包格式
GET/POST 请求的方法(POST就是往服务器上写数据,通常不需要cache,GET下载)
URL : http://ip/index.html 协议+IP+URI
URI : /index.html 请求的文档名称
Accept : 可以接受的数据方式比如压缩,编码等
User-Agent : 客户使用的浏览器
Host : 客户端请求的主机头可以是ip 也可以是域名
2:服务器七层回包的封装格式
Response Code : 200 404 500 (不同的数字代表不同的状态)
Date : 服务器响应请求时间
Content-Type : 服务器返回的数据类型
server: 服务器的类型
二:nginx基础
nginx和apache都是80端口。启动前必须关闭apache
1:安装启动
rqm -ivh nginx-0.6.36-1.el5.i386.rpm
service nginx start
2:配置文件重要参数
vim /etc/nginx/nginx.conf
events {
worker_connections 1024; 线程数
}
http {
server {
listen 80; ##服务器监听端口
server_name 192.168.18.249; ##服务器的IP
location / {
root /usr/share/nginx/html; ##网站根目录
index index.html index.htm index.php index.jsp; ##默认文档
}
error_page 404 /404.html; ##错误返回的页面
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
-----------------------------------------------------------------------------------------------
########## 添加虚拟服务器 ###############
server {
listen 80;
server_name https://www.wendangku.net/doc/c84186135.html,; ##服务器的域名
location / {
root /nginx/html; ##虚拟服务器的根目录
index index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}
}
三:nginx分发
1:分发的作用及三种方式
作用: 根据客户端的七层的封包格式,服务器可以基于 主机头(ip/域名),URI,User-Agent 三种方式 ,将客户端的不同请求分配给不同的服务器,从而实现分发功能,到达七层分发负载均衡.nginx在
七层分发的性能最优秀
2:基于主机头的分发
http {
server {
listen 80;
server_name 192.168.18.249; ##客户端访问的
主机头为ip,
location / {
root /usr/share/nginx/html; ##返回的是该目录的网站
index index.html index.htm index.php index.jsp;
}
}
server {
listen 80;
server_name https://www.wendangku.net/doc/c84186135.html,; ##客户端访问的主机头为域名
location / {
root /nginx/html; ##返回的是域名主机对用的网站
index index.html index.htm;
}
}
3:基于浏览器的分发
4:基于URI的分发
nginx能够处理静态页面,当客户端请求的URI为*.html静态页面时,本机自己处理。如果请求的是*.php,nginx会把请求交给php服务器处理,*.jsp的请求交给 tomcat服务器处理,达到七层分发
五:LNMP分发---基于fastcgi协议
LNMP
L linux
N nginx
M mysql
P php
1:php服务器(192.168.18.149)搭建------php作为单独服务器启动
rqm -ivh spawn-fcgi-1.6.3-1.el5.i386.rpm 建立fastcgi协议
rpm -ivh php-eaccelerator-0.9.5.2-2.el5.i386.rpm pdo加速
yum install php php-mysql php-pdo php-cli mysql mysql-server
2:修改配置文件
vim /etc/init.d/php_cgi
server_ip=0.0.0.0 允许所有的服务器访问
server_port=9000 端口9000
server_user=nginx 启动前保证主机有nginx用户
server_group=nginx
cp php_cgi /etc/init.d/ 将启动文件拷贝到开机启动
chmod +x /etc/init.d/php_cgi
/etc/init.d/php_cgi start 启动服务
server mysqld start
3:可以随意创建PHP网页的根目录
eg: mkdir /nginx/php
2: nginx服务器配置
http {
server {
listen 80;
server_name 192.168.18.249; ##客户端访问的主机头为ip,
location / {
root /usr/share/nginx/html; ##返回的是该目录的网站
index index.html index.htm index.php index.jsp;
}
location ~ \.php$ { ##当遇到PHP的请求时执行下面
fastcgi_pass 192.168.18.149:9000; ##PHP服务器IP
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /nginx/php/$fastcgi_script_name; ##php的网站根目录
include fastcgi_params;
}
# location ~ \.php$ { # php不单独启动,而是用lamp架构加载时用 这条命令分配给lamp服务器主机
# proxy_pass http://192.168.18.69;
# }
# location ~ \.jsp$ { # 请求jsp页面时分发给JSP服务器
# proxy_pass http://192.168.18.235:8080;
# }
}
3:测试
service nginx restart
http://192.168.18.249/index.php nginx服务器将php的请求交给PHP服务器处理
4:lamp架构加载PHP服务
yum install httpd php php-mysql mysql mysql-server -y 搭建lamp架构
vim /var/www/html/index.php 在根目录添加PHP网站
server httpd start