ndlli 发表于 2018-11-23 10:29:57

[转载]Nginx Apache实现网页动静分离 =修改

原文地址:Apache实现网页动静分离 =修改">Nginx Apache实现网页动静分离 =修改作者:用户3659498391

  复制域名:http://my.oschina.net/alanlqc/blog/148360>
  本文应用nginx措置静态文件战做负载反向代劳代理,apache负责措置php静态页面,经过过程简单的配置实现动静离散。
  因为apache措置静态页面的效力没有下,而普通网站大年夜多数的内容都是静态文件(如图片、html、css、js等),颠末nginx前端的反向代劳代理放慢战过滤,后端apache措置请供的压力便可大年夜大年夜加少,只需负责措置静态内容便能够了。正在机能与稳定性的衡量下,应用nginx+apache拆配便可以让它们正在各自擅长的发域大年夜展拳足。
  1、安装与配置Apache
  1. 安装Apache2
  # sudo yum install httpd
  2. 修改配置文件
  修改处事端标语,将80端口改成8080。
# sudo vim /etc/httpd/conf/httpd.conf

...
  #Listen 12.34.56.78:80
Listen 8080
...
  3. 启动处事并设置开机自启动
   # sudo /etc/init.d/httpd start
# sudo /sbin/chkconfig httpd on
  两、安装与配置Nginx
  1. 启用 EPEL repo源
  假定应用的CentOS 5.x版本,安装以下repo源:
# sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
假定应用的CentOS 6.x版本,安装以下repo源:
# sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  2. 安装nginx
  # sudo yum install nginx
  3. 修改配置文件
  主配置文件/etc/nginx/nginx.conf无需做太大年夜窜改,只需将worker_processes设置成与机器CPU核数相等即可(如CPU数为1,则worker_processes 1;)。
# sudo vim /etc/nginx/conf.d/virtual.conf

server {
listen 80;
server_name 192.168.85.83;
root /var/www/html;
index index.html index.htm index.php;

location ~ .(php)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}

location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
expires 15d;
}
location ~ .*.(js|css)?$ {
expires 1h;
}
}
4. 启动处事并设置开机自启动  # sudo /etc/init.d/nginx start
# sudo /sbin/chkconfig nginx on
  三、安装PHP(可选安装PHP-FPM)

  # sudo yum install php
# sudo /etc/init.d/httpd restart

FPM安装详见 http://my.oschina.net/alanlqc/blog/148126
  四、测试
  静态页面:
# echo "This is 192.168.85.83" > /data/www/index.html
http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif=修改" /> >
  静态页面:
# echo "" > /var/www/html/info.php
http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif=修改" />>
  经过过程curl -I 能够看到会见静态页面的时刻是经过过程nginx措置的
# curl -I http://192.168.85.83
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Mon, 29 Jul 2013 08:42:27 GMT
Content-Type: text/html
Content-Length: 22
Last-Modified: Sun, 28 Jul 2013 19:17:58 GMT
Connection: keep-alive
Expires: Tue, 13 Aug 2013 08:42:27 GMT
Cache-Control: max-age=1296000
Accept-Ranges: bytes


因为静态页面是经过过程nginx进行反向代劳代理交给apache措置,所以返回提醒的也是nginx
# curl -I http://192.168.85.83/info.php
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Mon, 29 Jul 2013 08:43:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.3
  

  验证php是经过过程apache 措置的:
闭闭apache 再测试会见php页面,看到会见没有到php,可是能会见到静态页面
# sudo /etc/init.d/httpd stop
http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif=修改" />>
  5、参考文章
  http://www.ha97.com/5119.html
http://pmghong.blog.51cto.com/3221425/1217151
http://www.rackspace.com/knowledge_center/article/centos-installing-nginx-via-yum






页: [1]
查看完整版本: [转载]Nginx Apache实现网页动静分离 =修改