配置nginx反向代理,实现动态静态页面分离

配置nginx反向代理,实现动态静态页面分离

实例:

nginx代理服务器(缓存服务器):172.25.254.19

php处理app:172.25.254.20 (可以添加多台php app server,配置相同)

静态页面处理:172.25.254.18 (可以添加多台静态页面sercver,配置相同)

配置php app

  1. 下载php-5.6.35.tar.bz2安装包,进行源码编译

    [root@php ~]# bzip2 -d php-5.6.35.tar.bz2 
    [root@php ~]# tar xf php-5.6.35.tar 
    [root@php ~]# ls
    anaconda-ks.cfg  php-5.6.35  php-5.6.35.tar
  2. 对php源码进行配置,检查当前的环境是否满足要安装软件的依赖关系

    ./configure --prefix=/usr/local/php --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-mhash  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-xml
    [root@php php-5.6.35]# make 
    [root@php php-5.6.35]# make test
    [root@php php-5.6.35]# make install
    [root@php php-5.6.35]# yum install libxml2-devel

    ./configure时,可能需要一系列安装依赖

  3. 初始化php app,主要用到php-fpm,fastcgi模块

    [root@php php-5.6.35]# cp php.ini-production /etc/php.ini
    [root@php php-5.6.35]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
    [root@php php-5.6.35]# chmod +x /etc/rc.d/init.d/php-fpm 
    [root@php php-5.6.35]# chkconfig --add php-fpm
    [root@php php-5.6.35]# chkconfig php-fpm on
    [root@php php-5.6.35]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  4. 优化php服务

    [root@php php-5.6.35]# vim /usr/local/php/etc/php-fpm.conf

    pm.max_children = 150pm.start_servers = 8pm.min_spare_servers = 5pm.max_spare_servers = 10pid = /usr/local/php/var/run/php-fpm.pid

  5. 安装http服务

    [root@php ~]# yum -y install httpd 
    [root@php ~]# systemctl start php-fpm
    [root@php ~]# systemctl start httpd
  6. 测试

    [root@php html]# cd /var/www/html/
    root@php html]# cat > index.php << eof 
    <?php
    phpinfo();
    ?>
    eof
  7. 使用浏览器访问172.25.254.20/index.php,如果有php信息页面,则成功

配置静态页面app

  1. 安装配置nginx app作为web服务器,源码安装。安装包可以在nginx官网下载

    [root@nginx_rs ~]# ls nginx-1.12.0.tar.gz 
    nginx-1.12.0.tar.gz
    [root@nginx_rs ~]# tar zxvf nginx-1.12.0.tar.gz 
    [root@nginx_rs ~]# cd nginx-1.12.0

    可以在编译前进行优化:

    [root@nginx_rs nginx-1.12.0]# vim src/core/nginx.h 
    //由于nginx版本一般不建议泄露,所以更改nginx头文件中的宏定义
    #define NGINX_VERSION      "fsx-1.0"    //这里可以自己修改
    #define NGINX_VER          "nginx/" NGINX_VERSION

    去掉nginx的debug模式

    [root@nginx_rs nginx-1.12.0]# vim auto/cc/gcc 
    # debug
    #CFLAGS="$CFLAGS -g"    //找到这行,注释掉
  2. 进行编译

    [root@nginx_rs nginx-1.12.0]# groupadd -r -g 108 nginx
    [root@nginx_rs nginx-1.12.0]# useradd -r -g 108 -u 108 nginx
    [root@nginx_rs nginx-1.12.0]# ./configure --prefix=/usr/local/php --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d 
    [root@nginx_rs nginx-1.12.0]# make && make install
    

    检查配置依赖可能会遇到依赖问题

  3. 修改nginx配置文件

    [root@nginx_rs nginx]# pwd
    /etc/nginx
    [root@nginx_rs nginx]# vim nginx.conf
    //添加location模块
            location / {
                root   /web/;       //这里设置默认发布目录为文件系统下的/web/
                index  index.html index.htm;
            } 
  4. 创建默认发布目录喝默认发布文件

    [root@nginx_rs nginx]# mkdir /web
    [root@nginx_rs web]# cd /web/
    [root@nginx_rs web]# echo "<h1> welcome to nginx </h1>" > index.html  
  5. 测试

    [root@nginx_rs web]# curl localhost
    <h1> welcome to nginx </h1>

配置nginx代理服务器

  1. 源码编译nginx,如nginx静态页面服务器一样

  2. 修改配置文件,实现动态页面和静态页面分离

    [root@nginx_new nginx]# vim nginx.conf

    具体内容:"只是http模块内容,其他优化没有加入"

    
    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;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
        upstream static {
            server 172.25.254.18:80 max_fails=2 fail_timeout=5s;
            server 127.0.0.1:80 backup;
        #这里可以添加多个server,实现负载均衡
        }
        upstream php {
            server 172.25.254.20:80 max_fails=2 fail_timeout=5s;
        #这里可以添加多个server,实现负载均衡
        }
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
        charset utf-8;
            #access_log  logs/host.access.log  main;
    #root for service 
            location / {
                root   /web/;
            auth_basic "restricted";
            auth_basic_user_file /etc/nginx/.user;
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
            index  index.html index.htm;
            }
    #service for static page
            location /media/ {
                    proxy_pass http://static/;
            }
    
    #service for administor
        location /admin {
            stub_status on;
            access_log off;
            allow 172.25.254.11;
                deny all;
        }
    #serive for php app
        location ~ \.php$ {
            proxy_pass http://php;
            proxy_connect_timeout 30;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_send_timeout 15;
            proxy_read_timeout 15;
        }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
        }
    
    }
  3. 添加缓存,修改配置文件

    //在http模块下添加
    proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
    //在server模块基于步骤2下修改
    #service for static page
            location /media/ {
                    proxy_pass http://static/;
            proxy_cache first;
                    proxy_cache_valid 200 10m;
            }
        add_header X-Via $server_addr;
            add_header X_cache_hit $upstream_cache_status;
  4. 测试

    • 静态页面测试

      使用浏览器访问172.25.254.19/media,出现welcome to nginx ,表示成功

      在nginx代理服务器会有缓存

      [root@nginx_new nginx]# ls /nginx/cache/first/0/35/
      5e7dbd456f4c61ec794fe9a6ad5a3350

      使用curl命令访问,会301提示,表示被重定向

      [root@nginx_new nginx]# curl 172.25.254.19/media
      <html>
      <head><title>301 Moved Permanently</title></head>
      <body bgcolor="white">
      <center><h1>301 Moved Permanently</h1></center>
      <hr><center>nginx/nginx-proxy</center>
      </body>
      </html>
      [root@nginx_new nginx]# curl -I 172.25.254.19/media
      HTTP/1.1 301 Moved Permanently
      Server: nginx/nginx-proxy
      Date: Mon, 18 Jun 2018 17:07:58 GMT
      Content-Type: text/html
      Content-Length: 190
      Location: http://172.25.254.19/media/
      Connection: keep-alive
      X-Via: 172.25.254.19

    • php app测试

      使用浏览器访问172.25.254.19/index.php,会出现php执行页面,包括php版本信息,表示成功

猜你喜欢

转载自blog.csdn.net/fsx2550553488/article/details/80727929
今日推荐