nginx实现代理服务器功能1:
#环境:
172.16.253.223 #CentOS7.3,安装nginx作为代理服务器
172.16.253.224 #CentOS7.3,安装httpd作为服务器
172.16.253.188 #CentOS6.8,咱庄httpd作为图片服务器
#223主机:
yum install nginx
vim /etc/nginx/conf.d/test.conf #注意必须以conf结尾
server {
listen 80;
server_name jing.li.io;
location / {
proxy_pass http://172.16.253.224/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #告诉后端主机这个请求的真正地址是$remote_addr(客户端真实的地址)
}
location ~* \.(jpg|png|jpeg)$ { #用正则之后
proxy_pass http://172.16.253.188; #这里不可以跟多余的uri
}
}
systemctl start nginx
vim /etc/hosts
172.16.253.223 jing.li.io #添加域名解析,物理机上的hosts也需要添加
#224主机
yum install httpd
vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #验证这个后端服务器收到的请求报文的源IP是否被更改
vim /var/www/html/index.html
<h1>OK</h1>
systemctl start httpd
tail /var/log/httpd/access_log
- - - [23/Jun/2017:04:13:30 +0800] "GET /favicon.ico HTTP/1.0" 404 209 "http://jing.li.io/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3095.5 Safari/537.36"
#改前
172.16.253.188 - - [23/Jun/2017:04:19:04 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3095.5 Safari/537.36"
#改后
#188主机
yum install httpd
find /usr/share/ -name "*.jpg" -exec cp {} /var/www/html/ \;
service httpd start
#注意,都需要关闭防火墙和SELinux
systemctl stop firewalld #C7
service iptables stop #C6
setenforce 0 #关闭SELinux
#在浏览器上访问jing.li.io以及jing.li.io/*.jpg #*为复制过去的某个图片文件名称
启用缓存功能:
vim /etc/nginx/nginx.conf
proxy_cache_path /data/nginx/cache levels=1:1:1 #注意,要放入http代码段;下面代码与这一行是相连续的,为了视图方便,特意分开
#cache目录需要建立出来 #定义3级子目录,每一级只有16个
keys_zone=pcache:10m
#定义键区域为pcache,可以定义多个,但不能重名,在内存中能使用10m
max_size=2g;
#/data/nginx/cache这个最大使用2g磁盘大小
proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g; #这个为整段的代码段
vim /etc/nginx/conf.d/test.conf
#在location中添加为单独location资源使用缓存,在server中加,表示所有资源都使用这个缓存
proxy_cache pacache; #调用这个缓存
proxy_cache_key $request_uri; #当多个域名指向同一站点,这种更加实用,这里必须添加
proxy_cache_valid 200 302 301 1h; #200,302,301响应码可以在1小时内进行
proxy_cache_valid 404 1m; #404最好比较短
构建lnmp环境
#前端nginx主机处理静态,后端fpm server处理动态。前端nginx也要有phpMyadmin
#A机器配置为fpm server
yum install php-fpm php-mysql php-mbstring php-mcrypt mariadb-server -y
lftp 172.16.0.1/pub
cd Sources/7.x86_64/php
mget phpMyAdmin-4.xxxxx
#将A机器网络配置为内网主机
ifconfig eth0 192.168.10.11/24 up
#启动fpm server
cd /etc/php-fpm.d/
vim www.conf
listen = 0.0.0.0:9000
#listen.allowed_clients
user = nginx #需要创建,会自动创建
group = nginx
pm.max_chidren = 150 #最大进程数
pm_status_path status #状态页启用,pong,ping也启用
php_value[session.save_path] = /var/lib/php/session #可能需要创建
chown nginx:nginx /var/lib/php/session #与php-fpm进程运行者身份保持一致
systemctl start php-fpm.service
#配置B机器作为nginx为反代服务器
vim /etc/nginx/conf.d/test.conf #原来的test文件可以删掉
server {
listen 80;
server_name li.jing.io;
index index.php index.html; #指明主页
location / {
root /data/nginx/html; #目录需要创建
}
location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;
include fastcgi_params; #需要包含进来一起生效
fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; #顺序
}
#vim /etc/nginx/fastcgi.pass #一般不改这个文件
#fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; #表示,反代时需要把用户请求的URL发送到服务器,路径还必须指明为服务器文件系统路径上的位置。
vim /data/nginx/index.html
<h1>Nginx Server</h1>
nginx -t
nginx -s reload
#A机器上创建主页
mkdir /data/apps -pv
vim /data/apps/index.php
<?php
phpinfo();
?>
#请求li.jing.io/index.php
#请求li.jing.io
#hosts文件记得更改
##A机器配置phpMyadmin
vim /etc/my.cnf.d/server.cnf
skip_name_resolve=ON
innodb_file_per_table=ON
systemctl start mariadb.service
ss -tnl #3306端口
mysql_secur_installation #需要确保root用户有密码
musql -uroot -plijing #lijing自己指定
#部署应用
tar xf phpMyadmin-xxxxx.tar.gz -C /data/apps/
cd /data/apps
ln -sv phpxxxxxxx pma
cd pma/
cp config.sample.inc.php config.inc.php
vim config.inc.php
$cfg....._secret'] = 'adada8b7c6d'; #添加随机数
#注意,访问的时候不能以pma下,他不会往后端反代
#再访问li.jing.io/index.php,会发现图片无法反代
scp php.....tar.gz 192.168.10.11:/root/
#切换到B机器
tar xf phpM......tar.gz -C /data/nginx/html
cd /data/nginx/html
ln -sv phpM..... pma
#再次访问li.jing.io/index.php
再配置一台nginx
C机器
ifconfig eth0 192.16.10.12/24 up
mkdir /data/nginx/html -pv
#切换到B
scp php.....tar.gz 192.168.10.11:/root/
#切换回C机器
tar xf phpM......tar.gz -C /data/nginx/html
cd /data/nginx/html
ln -sv phpM..... pma
vim /etc/nginx/nginx.conf
server {
root /data/nginx/html; #添加
}
systemctl start nginx #先关了httpd
#切换回B机器(Nginx)
vim /etc/nginx/conf.d/test.conf
location / {
proxy_pass http://192.168.10.12:80; #添加
}
nginx -s reload
#访问li.jing.io
#查看192.168.10.12,C机器的日志
tail /var/log/nigx/access.log
#做压测:
yum install httpd-tools
ab -c 100 -n 5000 http://li.jing.io/pma/index.php
#hosts文件需要添加解析
fastcgi添加缓存
#B机器
vim /etc/nginx/nginx.conf
http {
fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m max_size=2g;
}
vim /etc/nginx/conf.d/test.conf
location ~* \.php$ {
fastcgi_cache fcache; #调用上面定义的fcache
fastcgi_cache_key $request_uri; #定义键位$requesst_uri
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m; #添加这些
}
#先请求得到缓存之后再去压测
ab -c 100 -n 2000 http://li.jing.io/index.php #缓存
!ab
添加测试页
#B
vim /etc/nginx/conf.d/test.conf
location ~*/(status|ping)$ {
include fastcgi_params;
fastcgi_pass 192.168.10.11:9000;
fastcgi_param SCRIPT_FILENAME $fastcig_script_name;
}
#访问li.jing.io/status
#li.jing.io/ping
#li.jinng.io/status?full
#把fpm server换成ap
原创文章,作者:半斤八两,如若转载,请注明出处:http://www.178linux.com/78711

