nginx实现代理服务器功能

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

(5)
上一篇 2017-06-27 20:51
下一篇 2017-06-28 08:49

相关推荐

  • 删除boot,同时删除/etc/fstab,如何恢复

    删除boot,同时删除/etc/fstab,如何恢复 /etc/fstab文件丢失的时候,得光盘启动进入linux rescue下。 挂载好cdrom后,按ctrl+alt+del,然后按f2,进入bios,调整为cdrom启动,然后reboot,进入linux rescue急救模式/etc/fstab配置文件决定了linux系统在启动后如何加载各个分区,如…

    2017-09-23
  • 笔记一.如何使用VMWare Workstations 12 创建虚拟机

    笔记一.如何使用VMWare Workstations 12 创建虚拟机   一、准备工作 1.下载并安装VMWare Workstations 12 http://www.epinv.com/post/6304.html 下载链接 二、创建虚拟机步骤 1.打开VMWare Workstations 12 选择创建虚拟机 2.在创建虚拟机…

    Linux干货 2017-02-14
  • 实时获取MySQL的TPS、QPS(输出到屏幕)

    这个脚本挺好用的,例如我们在主库执行了一个大事务,结果导致从库的show slave status\G 一直卡在 system lock状态,我们用下面这个脚本就能知道到底是hang住了,还是只是执行缓慢(反应在脚本执行结果里面就是qps\tps是不停变动的)。 当然,从库一直处于system lock 状态,一般是因为我们从库也设置双1导致,只要…

    Linux干货 2017-05-07
  • 网络管理,程序管理

    lsmod |grep bond0 ifconfig bond0 down 关闭bonding服务 rmmod bonding  删除 bonding 服务 lsmod |grep eth1000 查看  lsmod指令,会列出所有已载入系统的模块 rmmod  删除内核中的一模块  查找网卡驱动e1000 rmmod…

    Linux干货 2016-09-11
  • ansible配置详解

    概述     ansible是一款无需在被管理主机上安装客户端,基于SSH对多台目标主机进行同时操作的轻量级的管理软件,借助各个内部的功能模块,实现了批量系统配置、批量程序部署、批量运行命令等功能。本篇就介绍一些关于ansible的基础配置相关的内容,具体包括:     1、an…

    Linux干货 2016-11-05
  • N26_第二周

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 a 文件查看类  cat tac  head  tail more  less b 文件操作类  touch cp  mv  rm more命令:   &nbsp…

    2017-05-26