1年前 (2018-06-19) 网友投稿  Linux安全运维
文章评分 0 次,平均分 0.0

前言:

Nginx网站架构实战——01、Nginx介绍及编译安装:传送门

Nginx网站架构实战——02、Nginx信号量:传送门

Nginx网站架构实战——03、nginx虚拟主机配置:传送门

Nginx网站架构实战——04、nginx日志管理:传送门

Nginx网站架构实战——05、nginx定时任务完成日志切割:传送门

Nginx网站架构实战——06、Location详解之精准匹配:传送门

Nginx网站架构实战——07、Location之正则匹配:传送门

Nginx网站架构实战——08、nginx Rewrite语法详解:传送门

Nginx网站架构实战——09、编译PHP并与nginx整合:传送门

Nginx网站架构实战——10、安装ecshop:传送门

Nginx网站架构实战——11、商城url重写实战:传送门

Nginx网站架构实战——12、nginx gzip压缩提升网站速度:传送门

Nginx网站架构实战——13、expires缓存提升网站负载:传送门

这两天做了一下apache+nginx实现动静分离的实验,实验大概是这样的,搭建LAMP之后,再装上Nginx。用户访问页面的请求到达服务器之后,静态页面又nginx出来,动态页面则交给apache处理。这是因为apache处理静态页面的效率不高,远不及nginx。通过nginx的反向代理加速,直接将请求丢给apache去处理,达到动静分离的效果。下面是实验的过程:

实验平台:RHEL6.3_x64 最小化安装
IP:192.168.30.114

[root@tiejiangSRC1 src]# yum install cmake gcc gcc-c++ make ncurses-devel bison wget
[root@tiejiangSRC1 src]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.31.tar.gz/from/http://cdn.mysql.com

一、安装mysql

[root@tiejiangSRC1 src]# useradd -s /sbin/nologin -M mysql
[root@tiejiangSRC1 src]# cd mysql-5.5.29
[root@tiejiangSRC1 mysql-5.5.29]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DINSTALL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DMYSQL_USER=mysql
[root@tiejiangSRC1 mysql-5.5.29]# make && make install
[root@tiejiangSRC1 mysql-5.5.29]# cd /usr/local/mysql/
[root@tiejiangSRC1 mysql]# chown -R mysql:mysql  .
[root@tiejiangSRC1 mysql]# mkdir /data
[root@tiejiangSRC1 mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
[root@tiejiangSRC1 mysql]# chown -R root .
[root@tiejiangSRC1 mysql]# rm -rf /etc/my.cnf
[root@tiejiangSRC1 mysql]# cp support-files/my-medium.cnf /etc/my.cnf
    [mysqld]
    bind-address    = 127.0.0.1
    port            = 3306
    socket          = /tmp/mysql.sock
    basedir         = /usr/local/mysql
    datadir         = /data/mysql
    user            = mysql
    character_set_server = utf8
    ......
[root@tiejiangSRC1 mysql]# vim /etc/init.d/mysqld
    basedir=/usr/local/mysql
    datadir=/data/mysql

二、安装apache

[root@tiejiangSRC1 src]# wget http://mirror.bjtu.edu.cn/apache//apr/apr-1.4.6.tar.bz2
[root@tiejiangSRC1 src]# wget http://mirror.bjtu.edu.cn/apache//apr/apr-util-1.5.2.tar.bz2
[root@tiejiangSRC1 src]# wget http://mirror.bjtu.edu.cn/apache//apr/apr-iconv-1.2.1.tar.bz2
[root@tiejiangSRC1 src]# tar xf apr-1.4.6.tar.gz
[root@tiejiangSRC1 src]# cd apr-1.4.6
[root@tiejiangSRC1 apr-1.4.6]# ./configure
    这里有一个报错:
    config.status: executing libtool commands
    rm: cannot remove `libtoolT': No such file or directory
    config.status: executing default commands
解决办法:
[root@tiejiangSRC1 apr-1.4.6]# vim configure
    # $RM "$cfgfile"
    找到上面那句命令,并注释
[root@tiejiangSRC1 apr-1.4.6]# make && make install
[root@tiejiangSRC1 apr-1.4.6]# vim /etc/ld.so.conf.d/apr.conf
    /usr/local/apr/lib
[root@tiejiangSRC1 apr-1.4.6]# ldconfig
[root@tiejiangSRC1 apr-1.4.6]# cd ..
[root@tiejiangSRC1 src]# tar xf apr-util-1.5.2.tar.bz2
[root@tiejiangSRC1 src]# cd apr-util-1.5.2
[root@tiejiangSRC1 apr-util-1.5.2]# ./configure --with-apr=/usr/local/apr
[root@tiejiangSRC1 apr-util-1.5.2]# make && make install
[root@tiejiangSRC1 apr-util-1.5.2]# cd ..
[root@tiejiangSRC1 src]# tar xf apr-iconv-1.2.1.tar.bz2
[root@tiejiangSRC1 src]# cd apr-iconv-1.2.1
[root@tiejiangSRC1 apr-iconv-1.2.1]# ./configure --with-apr=/usr/local/apr
[root@tiejiangSRC1 apr-iconv-1.2.1]# 
[root@tiejiangSRC1 src]# tar xf httpd-2.2.22.tar.gz
[root@tiejiangSRC1 src]# cd httpd-2.2.22
[root@tiejiangSRC1 httpd-2.2.22]# ./configure --prefix=/usr/local/apache2 --enable-ssl=share --enable-so
[root@tiejiangSRC1 httpd-2.2.22]# make && make install

三、安装php

[root@tiejiangSRC1 src]# yum install libxml2-devel bzip2-devel libcurl-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel
readline-devel net-snmp-devel freetype-devel zlib-devel gd libjpeg-devel
[root@tiejiangSRC1 src]# tar xf php-5.4.11.tar.bz2
[root@tiejiangSRC1 src]# cd php-5.4.11
[root@tiejiangSRC1 php-5.4.11]# ./configure --prefix=/usr/local/php  --with-apxs2=/usr/local/apache2/bin/apxs --disable-ipv6 --with-pcre-regex --with-zlib --with-bz2 --with-curl --enable-dba=shared --with-pcre-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype --enable-mbstring --enable-gd-native-ttf --with-mhash --with-mysql --with-mysqli --with-pdo-mysql --with-readline --with-snmp --enable-sockets --enable-zip
[root@tiejiangSRC1 php-5.4.11]# make && make install
[root@tiejiangSRC1 php-5.4.11]# cp php.ini-production /usr/local/php/lib/php.ini
为apache添加支持php
[root@tiejiangSRC1 php-5.4.11]# vim /usr/local/apache2/conf/httpd.conf
 <IfModule dir_module>
 DirectoryIndex index.html index.php
 </IfModule>
 Include conf/extra/php.conf
[root@tiejiangSRC1 php-5.4.11]# vim /usr/local/apache2/conf/extra/php.conf
  <FilesMatch \.php$>
      SetHandler application/x-httpd-php // 引用模板使apache支持php
  </FilesMatch>
创建动态页面
[root@tiejiangSRC1 apache2]# mkdir /data/www
[root@tiejiangSRC1 apache2]# vim /data/www/index.php</pre>

<?php phpinfo(); ?>
<pre>启动apache
[root@tiejiangSRC1 apache2]# bin/apachectl start
    测试访问是否正常支持php

四、编译安装nginx

[root@tiejiangSRC1 src]#  yum install pcre-devel openssl-devel perl-ExtUtils-Embed
[root@tiejiangSRC1 src]# tar xf nginx-1.2.6.tar.gz
[root@tiejiangSRC1 src]# cd nginx-1.2.6
[root@tiejiangSRC1 nginx-1.2.6]# useradd -s /sbin/nologin -M www
[root@tiejiangSRC1 nginx-1.2.6]#  ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module  --without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre
[root@tiejiangSRC1 nginx-1.2.6]# make && make install

五、配置nginx

[root@tiejiangSRC1 nginx]# vim conf/nginx.conf
user  www www;
worker_processes  8;
worker_rlimit_nofile 65535;
pid        logs/nginx.pid;
events {
   worker_connections  65535;
   use epoll;
}
error_log /usr/local/nginx/logs/error.log info;
http {
   include       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  logs/access.log  main;
   charset gb2312;
   server_names_hash_max_size 2048;
   server_names_hash_bucket_size 256;
   client_header_buffer_size 256k;
   client_max_body_size 100m;
   large_client_header_buffers 4 256k;
   sendfile        on;
   tcp_nopush     on;
   server_tokens   off;
   tcp_nodelay     on;
   proxy_send_timeout  300;
   proxy_read_timeout  300;
   proxy_buffer_size  4k;
   proxy_buffers 16 32k;
   proxy_busy_buffers_size 64k;
   proxy_temp_file_write_size 64k;
   proxy_connect_timeout 30s;
   keepalive_timeout  10;
# 开启压缩功能
   gzip  on;
   gzip_http_version 1.0;
   gzip_min_length  1100;
   gzip_comp_level  3;
   gzip_buffers  4 32k;
   gzip_types    text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml;
   ignore_invalid_headers on;
   client_header_timeout  3m;
   client_body_timeout 3m;
   send_timeout     3m;
   connection_pool_size  256;
   request_pool_size  32k;
   output_buffers   4 64k;
   postpone_output  1460;
   open_file_cache max=1000 inactive=300s;
   open_file_cache_valid    600s;
   open_file_cache_min_uses 2;
   open_file_cache_errors   off;
   include "/usr/local/nginx/conf/vhosts/*.conf";
    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  logs/access.log  main;
}

[root@tiejiangSRC1 nginx]# vim conf/vhosts/vhost1.conf
server{
       listen 80;
       server_name 192.168.30.114;
       root /data/www;
       index   index.html index.htm index.php;
       if (-d $request_filename)
               {
                       rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
               }
#所有php的页面均交由apache处理
       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://192.168.30.114:81;
               }
}

 

创建静态页面
[root@tiejiangSRC1 nginx]# echo "hello,this is the html test." > /data/www/index.html
测试81端口能不能成功访问到页面

Nginx网站架构实战——14、反向代理实现nginx+apache动静分离

测试访问静态页面

Nginx网站架构实战——14、反向代理实现nginx+apache动静分离

测试访问动态页面

Nginx网站架构实战——14、反向代理实现nginx+apache动静分离

通过curl -I 可以看到访问静态页面的时候是通过Nginx处理的
[root@tiejiangSRC1 nginx]# curl -I http://192.168.30.114
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 06 Jun 2013 09:26:10 GMT
Content-Type: text/html; charset=gb2312
Content-Length: 29
Last-Modified: Thu, 06 Jun 2013 09:25:48 GMT
Connection: keep-alive
Accept-Ranges: bytes
由于动态页面是通过nginx进行反向代理交给apache处理,所以返回显示的也是nginx
[root@tiejiangSRC1 nginx]# curl -I http://192.168.30.114/index.php
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 06 Jun 2013 09:26:58 GMT
Content-Type: text/html; charset=gb2312
Connection: keep-alive
X-Powered-By: PHP/5.4.11
验证php是通过apache 处理的:
关闭apache 再测试访问php页面,看到访问不到php,但是能访问到静态页面

Nginx网站架构实战——14、反向代理实现nginx+apache动静分离

为apache安装rpaf模块,该模块用于apache做后端时获取访客真实的IP
[root@tiejiangSRC1 src]# wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
[root@tiejiangSRC1 src]# tar xf mod_rpaf-0.6.tar.gz
[root@tiejiangSRC1 src]# cd mod_rpaf-0.6
[root@tiejiangSRC1 mod_rpaf-0.6]# /usr/local/apache2/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
编辑/usr/local/apache2/conf/httpd.conf添加模块参数

查找LoadModule php5_module modules/libphp5.so,在下方添加:

[root@tiejiangSRC1 mod_rpaf-0.6]# vim /usr/local/apache2/conf/httpd.conf
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 192.168.30.114
RPAFsethostname On
RPAFheader X-Forwarded-For

重启apache,nginx 即可




//下面这个css和插件后台设置的主题有关系,如果需要换样式,则需要修改以下CSS名称

 

除特别注明外,本站所有文章均为铁匠运维网原创,转载请注明出处来自http://www.tiejiang.org/21492.html

发表评论

暂无评论

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

扫一扫二维码分享