NGINX反向代理-LANMP配置

实验环境:

NGINX                CentOS 7.2x86_64            IP:172.16.253.94

LAMP                 CentOS 6.7x86_64            IP:192.168.1.20

测试端              CentOS 7.2x86_64            IP:172.16.251.138

构建LAMP:

[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install httpd php php-mysql mysql-server

启动服务:

[[email protected] ~]# service httpd restart

[[email protected] ~]# service httpd mysql

[[email protected] ~]# echo "Backup Server 1" >> /var/www/html/index.html

安装NGINX:

[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0

[[email protected] ~]# yum -y install
nginx-1.10.1-1.el7.ngx.x86_64.rpm

[[email protected] ~]# rpm -ql nginx

启动服务:

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx

[[email protected] ~]# ss -tnl
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port             
LISTEN     0      128                     *:80                                  *:*

反向代理至后端LAMP:

[[email protected] ~]# vim
/etc/nginx/conf.d/default.conf

server {

listen       80;

server_name  localhost;

location / {

root   /usr/share/nginx/html;

index  index.html index.htm;

proxy_pass http://192.168.1.20;             //转发至后端服务器

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   /usr/share/nginx/html;

}

[[email protected] ~]#
nginx -s reload

客户端测试:

[[email protected] ~]# curl http://172.16.253.94
Backup Server 1

设定发往后端主机请求报文首部的值:

[[email protected] ~]#
vim /etc/nginx/conf.d/default.conf

server {

省略部分…

proxy_set_header X-IP $remote_addr;

省略部分…

}

[[email protected] ~]#
nginx -s reload

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf

省略部分…

LogFormat "%{X-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

省略部分…

[[email protected] ~]# service httpd restart

测试后查看日志:

[[email protected]
~]# tail /var/log/httpd/access_log

192.168.1.10 - -
[02/Aug/2016:13:57:18 +0800] "GET / HTTP/1.0" 304 - "-"
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/45.0.2454.101 Safari/537.36"

192.168.1.10 - -
[02/Aug/2016:13:57:19 +0800] "GET / HTTP/1.0" 304 - "-"
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/45.0.2454.101 Safari/537.36"

172.16.251.138 - - [02/Aug/2016:17:01:15 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
172.16.251.138 - - [02/Aug/2016:17:01:15 +0800] "GET /favicon.ico HTTP/1.0" 404 287 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"

代理PHP动态页面:

[[email protected] ~]# vim /etc/nginx/conf.d/default.conf

省略部分…

location ~* \.php$ {

proxy_pass http://192.168.1.20;

}

省略部分…

[[email protected] ~]# nginx -s reload

[[email protected] ~]# vim /var/www/html/test.php

<?php
        phpinfo();
?>

客户端测试:

[[email protected] ~]# curl http://172.16.253.94/test.php

AB压力测试:

[[email protected] ~]# ab -n 5000 -c 100 http://172.16.253.94/test.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.253.94 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests

Server Software:        nginx/1.10.1
Server Hostname:        172.16.253.94
Server Port:            80

Document Path:          /test.php
Document Length:        49151 bytes

Concurrency Level:      100
Time taken for tests:   19.042 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      246565000 bytes
HTML transferred:       245755000 bytes
Requests per second:    262.57 [#/sec] (mean)
Time per request:       380.847 [ms] (mean)
Time per request:       3.808 [ms] (mean, across all concurrent requests)
Transfer rate:          12644.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   9.2      1     179
Processing:    47  375 155.1    341    3182
Waiting:        6  292 112.3    282    1709
Total:         54  378 155.8    342    3188

Percentage of the requests served within a certain time (ms)
  50%    342
  66%    372
  75%    401
  80%    421
  90%    502
  95%    612
  98%    883
  99%    990
 100%   3188 (longest request)

定义缓存:

[[email protected] ~]# vim /etc/nginx/nginx.conf

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

access_log  /var/log/nginx/access.log  main;

sendfile        on;
    #tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=pcache:10m max_size=1g;

include /etc/nginx/conf.d/*.conf;
}

[[email protected] ~]# vim /etc/nginx/conf.d/default.conf

server {

省略部分…

proxy_cache pcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;

省略部分…

}

测试缓存效果:

[[email protected] ~]# ab -n 5000 -c 100 http://172.16.253.94/test.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.253.94 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests

Server Software:        nginx/1.10.1
Server Hostname:        172.16.253.94
Server Port:            80

Document Path:          /test.php
Document Length:        49151 bytes

Concurrency Level:      100
Time taken for tests:   3.304 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      246565000 bytes
HTML transferred:       245755000 bytes
Requests per second:    1513.35 [#/sec] (mean)
Time per request:       66.078 [ms] (mean)
Time per request:       0.661 [ms] (mean, across all concurrent requests)
Transfer rate:          72878.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   14  64.9     10    1014
Processing:     7   51   8.3     51      82
Waiting:        1   13   6.1     12      46
Total:         23   66  66.2     62    1083

Percentage of the requests served within a certain time (ms)
  50%     62
  66%     64
  75%     66
  80%     68
  90%     71
  95%     75
  98%     82
  99%     87
 100%   1083 (longest request)

定义响应报文值:

[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
server {

省略部分…

add_header X-Via $server_addr;
    add_header X-Accel $server_addr;

省略部分…

}

[[email protected] ~]# nginx -s reload

客户端测试:

[[email protected] ~]# curl http://172.16.253.94/test.php

时间: 2024-10-28 21:57:17

NGINX反向代理-LANMP配置的相关文章

Nginx反向代理的配置

Chapter: Nginx基本操作释疑 1. Nginx的端口修改问题 2. Nginx 301重定向的配置 3. Windows下配置Nginx使之支持PHP 4. Linux下配置Nginx使之支持PHP 5. 以源码编译的方式安装PHP与php-fpm 6. Nginx多站点配置的一次实践 7. Nginx反向代理的配置 Nginx 作为 web 服务器一个重要的功能就是反向代理.其实我们在前面的一篇文章<Nginx多站点配置的一次实践>里,用的就是 Nginx 的反向代理,这里简单再

Nginx反向代理websocket配置实例

最近有一个需求,就是需要使用 nginx 反向代理 websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录 复制代码 代码如下: 注: 看官方文档说 Nginx 在 1.3 以后的版本才支持 websocket 反向代理,所以要想使用支持 websocket 的功能,必须升级到 1.3 以后的版本,因此我这边是下载的 Tengine 的最新版本测试的 1.下载 tengine 最近的源码 复制代码 代码如下: wget http://tengine.taobao.org/dow

Nginx反向代理后配置404页面

关键词:proxy_intercept_errors.fastcgi_intercept_errors   在网上搜索404配置,有很多配置文章,但都是关于fastcgi_intercept_errors的,不过对我们的项目不起作用.我们的项目使用nginx做反向代理,通过域名区分不同的网站,配置fastcgi_intercept_errors不生效,需要配置proxy_intercept_errors.以下罗列出两种配置方式,供大家参考.   1.fastcgi_intercept_error

【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

m今天来整理下netcore在linux(ubuntu)上的运行环境搭建 对应版本 ubuntu 16.04 .net core 2.1 nginx version: nginx/1.10.3 (Ubuntu) supervisor 配置开机重启服务自启动 Supervisorhttp://supervisord.org/是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统.它可以很方便的监听.启动.停止.重启一个或多

nginx反向代理时配置访问密码

在有些情况下,我们需要对服务器上的某些资源进行限流或者禁止陌生人访问,我们可以通过nginx配置来对url添加访问密码. 效果如下 nginx 开启访问验证在 nginx 下,提供了 ngx_http_auth_basic_module 模块实现让用户只有输入正确的用户名密码才允许访问web内容.默认情况下,nginx 已经安装了该模块.所以整体的一个过程就是先用第三方工具( htpasswd,或者使用 openssl)设置用户名.密码(其中密码已经加过密),然后保存到文件中,接着在 nginx

nginx反向代理原理和配置讲解

最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器. 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接

nginx的反向代理和配置

最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器. 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接

搭建nginx反向代理用做内网域名转发

基于域名的7层转发的实现(NAT+反向代理) 在实际办公网中,因为出口IP只有一个,要实现对外提供服务的话就必须得做端口映射,如果有多个服务要对外开放的话,这只能通过映射不同端口来区分,这在实际使用过程中非常的痛苦(记忆困难.一一对应关系也没有规律.访问的时候还得加端口),这个痛苦的问题用表格的形式来形象的描述如下: Public IP Public Port Number Internal IP Internal Port Number Note 1.1.1.1 80 192.168.1.10

Nginx反向代理(基于目录动静分离、不同浏览器类型不同代理、基于扩展名的不同代理)

proxy_pass http_proxy_module proxy_pass指令输入ngx_http_proxy_module模块,此模块可以将请求转发到另外一台服务器 官网:http://nginx.org/en/docs/http/ngx_http_proxy_module.html =============================================== location /some/path/ { proxy_pass http://127.0.0.1; } ==