订阅本站
收藏本站
微博分享
QQ空间分享

nginx实现代理服务器功能

alen 分类:Linux入门 时间:2018/12/21 22:17:00 浏览: 评论: 加入收藏

nginx实现代理服务器功能1:

1234567891011121314151617181920212223242526272829303132333435363738394041424344 #环境:172.16.253.223          #CentOS7.3,安装nginx作为代理服务器172.16.253.224          #CentOS7.3,安装httpd作为服务器172.16.253.188          #CentOS6.8,咱庄httpd作为图片服务器#223主机:yum install nginxvim /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 nginxvim /etc/hosts    172.16.253.223 jing.li.io           #添加域名解析,物理机上的hosts也需要添加#224主机yum install httpdvim /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 httpdtail /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 httpdfind /usr/share/ -name "*.jpg" -exec cp {} /var/www/html/ /;service httpd start#注意,都需要关闭防火墙和SELinuxsystemctl stop firewalld        #C7service iptables stop           #C6setenforce 0                    #关闭SELinux#在浏览器上访问jing.li.io以及jing.li.io/*.jpg            #*为复制过去的某个图片文件名称

启用缓存功能:

1234567891011121314 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环境

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 #前端nginx主机处理静态,后端fpm server处理动态。前端nginx也要有phpMyadmin#A机器配置为fpm serveryum install php-fpm php-mysql php-mbstring php-mcrypt mariadb-server -ylftp 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 servercd /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 -tnginx -s reload#A机器上创建主页mkdir /data/apps -pvvim /data/apps/index.php    <?php        phpinfo();    ?>#请求li.jing.io/index.php#请求li.jing.io#hosts文件记得更改##A机器配置phpMyadminvim /etc/my.cnf.d/server.cnf    skip_name_resolve=ON            innodb_file_per_table=ONsystemctl start mariadb.servicess -tnl     #3306端口mysql_secur_installation        #需要确保root用户有密码musql -uroot -plijing           #lijing自己指定#部署应用tar xf phpMyadmin-xxxxx.tar.gz -C /data/apps/cd /data/appsln -sv phpxxxxxxx pmacd pma/cp config.sample.inc.php config.inc.phpvim 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/htmlcd /data/nginx/htmlln -sv phpM..... pma#再次访问li.jing.io/index.php

再配置一台nginx

123456789101112131415161718192021222324252627 C机器ifconfig eth0 192.16.10.12/24 upmkdir /data/nginx/html -pv #切换到Bscp php.....tar.gz 192.168.10.11:/root/#切换回C机器tar xf phpM......tar.gz -C /data/nginx/htmlcd /data/nginx/htmlln -sv phpM..... pmavim /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-toolsab -c 100 -n 5000 http://li.jing.io/pma/index.php#hosts文件需要添加解析

fastcgi添加缓存

12345678910111213141516 #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

添加测试页

1234567891011 #Bvim /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

TAG:

文章评论

留言与评论(共有 0 条评论)
   
验证码: