在 nginx 中,proxy 用来实现反向代理,upstream 用来实现负载均衡。

例如有两台服务器,nginx 服务器作为代理服务器,执行 .html 文件,apache 服务器上执行 .php 文件,客户端发来的请求首先发送给 nginx 服务器,如果发送请求的是 .php 文件,则把请求通过 proxy pass 转发给 apache 服务器,apache 服务器处理后把结果返回给 nginx 服务器,nginx 服务再把结果返回给客户端。该例中 nginx 服务器实现了反向代理,或者说实现了 nginx + apache 的动静分离

配置过程:

① 首先不让 nginx 服务器执行 .php 文件,修改 /usr/local/nginx/conf/nginx.conf 文件,把以下 location 段注释:

        location ~ \.php$ {
#root html;
#fastcgi_pass 127.0.0.1:;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#include fastcgi_params;
}

保存退出。平滑重启 nginx。

此时访问 http://192.168.254.100/test.php:

nginx 已经不能解析 .php 文件了。

② 编辑 apache 的配置文件 httpd.conf

[root@localhost nginx]# find / -name httpd
/usr/local/apache2/bin/httpd
/root/httpd-2.2./httpd
/root/httpd-2.2./.libs/httpd
[root@localhost nginx]# vim /usr/local/apache2/conf/httpd.conf

修改监听端口(nginx 已经监听 80 端口,所以把 apache 的监听端口改为 8080):Listen 8080

保存退出。

③ 启动 apache:

[root@localhost nginx]# /usr/local/apache2/bin/apachectl start

访问 http://192.168.254.100:8080:

④ 配置 apache 的虚拟主机及端口:

[root@localhost nginx]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 

修改 httpd-vhosts.conf:

NameVirtualHost *:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:>
DocumentRoot "/usr/local/nginx/html"
ServerName test.com
</VirtualHost>

把 DocumentRoot 定义到 /usr/local/nginx/html 目录下。保存退出。

编辑 httpd.conf:

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>

改为

<Directory />
Options FollowSymLinks
AllowOverride all
Order deny,allow
Allow from all
</Directory>

把 line:151

AllowOverride None

改为

AllowOverride All

保存退出。重启 apache。

⑤ 测试:访问 http://192.168.254.100:8080

访问 http://192.168.254.100:8080/ecshop/

⑥ 配置 nginx 的反向代理

[root@localhost nginx]# vim conf/nginx.conf

nginx.conf,修改 location ~ \.php$,只需要添加一句:proxy_pass 192.168.254.100:8080;

        location ~ \.php$ {
proxy_pass http://192.168.254.100:;
#root html;
#fastcgi_pass 127.0.0.1:;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#include fastcgi_params;
}

保存退出。平滑重启 nginx。

测试动静分离:此时访问 http://192.168.254.100/test.php

也就是说,此时的 url 不带 8080 端口,访问 .php 文件,同样能够解析,而且是通过 apache 进行解析。

测试动静分离 2:

修改 /usr/local/nginx/html/test.php

[root@localhost nginx]# vim html/test.php 

test.php:

<?php
//phpinfo();
echo rand(,);
?>
<img src='image/nginx.png'>

保存退出。

再次访问 http://192.168.254.100/test.php

此时分析 apache 日志:

[root@localhost nginx]# tail -f /usr/local/apache2/logs/access_log 

access.log:

apache 没有响应图片。

再分析 nginx 日志:

[root@localhost nginx]# tail -f /usr/local/nginx/logs/access.log 

nginx 响应的图片。

Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)的更多相关文章

  1. 【nginx网站性能优化篇(2)】反向代理实现Apache与Nginx的动静分离(LNMPA)

    为什么要使用反向代理 具体请参考这篇博文:[Linux常识篇(1)]所谓的正向代理与反向代理 在虚拟机上配置反向代理的步骤 首先假设你已经假设好了LNMP架构了,这时我们还要安装Apache和php, ...

  2. nginx配置文件(反向代理+集群+动静分离)

    1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...

  3. 15 nginx反向代理实现nginx+apache动静分离

    一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...

  4. Nginx网络架构实战学习笔记(三):nginx gzip压缩提升网站速度、expires缓存提升网站负载、反向代理实现nginx+apache动静分离、nginx实现负载均衡

    文章目录 nginx gzip压缩提升网站速度 expires缓存提升网站负载 反向代理实现nginx+apache动静分离 nginx实现负载均衡 nginx gzip压缩提升网站速度 网页内容的压 ...

  5. nginx反向代理nginx,RealServer日志打印真实ip

    title: nginx反向代理nginx,RealServer日志打印真实ip date: 2016-05-11 19:15:37 tags: --- nginx反向代理nginx,RealServ ...

  6. nginx 反向代理 与 Apache backend的配置联合配置

    nginx 反向代理 与 Apache backend的配置联合配置: 说明: nginx 将http映射到Apache上的特定子目录. 配置方法步骤: 1.  设置域名, 子域名映射到指定服务器ip ...

  7. nginx在使用非80端口做反向代理【转】

    设置nginx反向代理,nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误 upstream jboss{ server max_fails= fail_timeout=20s; s ...

  8. 生产环境中nginx既做web服务又做反向代理

    一.写对于初入博客园的感想 众所周知,nginx是一个高性能的HTTP和反向代理服务器,在以前工作中要么实现http要么做反向代理或者负载均衡.尚未在同一台nginx或者集群上同时既实现HTTP又实现 ...

  9. 【nginx网站性能优化篇(3)】反向代理实现负载均衡

    注意,本篇文章为负载均衡的理论篇,后续找个机会推出实战篇.理论篇主要讲述如何配置负载均衡,配置负载均衡不难.难的是真正的实战,比如如何做到多服务器之间的数据共享(session,file等),多cac ...

随机推荐

  1. 幂运算(codevs 2541)

    题目描述 Description 从m开始,我们只需要6次运算就可以计算出m31: m2=m×m,m4=m2×m2,m8=m4×m4,m16=m8×m8,m32=m16×m16,m31=m32÷m. ...

  2. 谈谈异步加载JavaScript

    前言 关于JavaScript脚本加载的问题,相信大家碰到很多.主要在几个点—— 1> 同步脚本和异步脚本带来的文件加载.文件依赖及执行顺序问题 2> 同步脚本和异步脚本带来的性能优化问题 ...

  3. Android仿微信界面

    效果图 原理介绍 1.先绘制一个颜色(例如:粉红) 2.设置Mode=DST_IN 3.绘制我们这个可爱的小机器人 回答我,显示什么,是不是显示交集,交集是什么?交集是我们的小机器人的非透明区域,也就 ...

  4. cocos2dx实现象棋之运动

    1.头文件 void moveStone(int moveid, int killid, int x, int y); void moveComplete(CCNode*, void*); bool ...

  5. context switches per second 上下文切换

    上下文切换对系统来说意味着消耗大量的CPU时间.上下文切换只发生在内核态中.内核态是CPU的一种有特权的模式,在这种模式下只有内核运行并且可以访问所有内存和其它系统资源.

  6. SQL 查询CET使用领悟

    用到sql的遍历循环查询,如果不考虑用CET,估计又到了自己造轮子的时代了,现在觉得sql的CET确实是个好东西,针对SQL的递归查询,很是不错的方法: with etcRecommandINfo2( ...

  7. Windows 10 上强制Visual Studio以管理员身份运行

    Windows 10 的一个既安全又蛋疼之处是UAC的行为被改变了.以往在Windows 7中,只要关闭了UAC,自己的帐号又是本机管理员组的,任何程序都会以管理员身份启动.然而,在Windows 8 ...

  8. Wcf for wp8 创建wcf服务 连接wp8模拟器并显示来自wcf服务的接口信息 (一)

    下载: vs2012 pro for wp8 iis express http://download.microsoft.com/download/B/2/8/B2801FEE-9A60-4AFA-8 ...

  9. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

  10. MS SQL SERVER 2000 常用 Tran-SQL 语句

    一.创建数据库:create database mydb-创建数据库mydbon primary-在primary文件组中( name = mydb_data1,filename = 'd:\sql ...