LNAMP反向代理等功能实现

来源:互联网 发布:java 积分兑换商城 编辑:程序博客网 时间:2024/06/04 01:08

简介

Nginx功能很强大,可以作为web静态页面服务器,七层反向代理、四层反向代理、负载均衡、缓存等功能。Nginx是Apache服务器不错的替代品。
与传统服务器不同,NGINX不依赖于线程来处理请求。相反,它使用更加可扩展的事件驱动(异步)架构。这种架构在负载下使用小的但更重要的是可预测的内存量。即使您不希望处理数千个并发请求,您仍然可以从NGINX的高性能和小内存中获益。

拓扑结构

一、安装环境:

centos7:nginx (ip:172.16.250.111) //实现Nginx代理
centos7:AP1 (ip :192.168.16.101) //后端web服务器1
centos7:AP2 (ip :192.168.16.102) //后端web服务器2
centos7:NFS (ip :192.168.16.103) //后端NFS文件共享机制 提供:WordPress
centos7:mariabd (ip :192.168.16.104) //后端数据库

二、设置后端各项服务

1.对AP1与AP2设置,提供http服务。

[root@centos7 ~]#yum -y httpd php php-mysql nfs-utils //安装相应服务,nfs-utils用于挂载nfs用到,否则出错。[root@centos7 ~]#systemctl start  httpd.service   //启动httpd服务[root@centos7 ~]#systemctl enable  httpd.service //开机启动httpd服务

2.在mysql服务设置WordPress数据库

[root@centos7 ~]#yum -y install mariadb-server  //安装mysql服务centos7为mariadb[root@centos7 ~]#systemctl start  mariadb.service  //启用mysql服务[root@centos7 ~]#systemctl enable  mariadb.service  //开机启动mys服务[root@centos7 ~]#mysql_secure_installation  //进行mysql安全设置[root@centos7 ~]#mysql -uroot -p123456 //以mysql的root身份登入。MariaDB [(none)]> create database blogdb; //创建WordPress数据库MariaDB [(none)]> grant all on blogdb.* to wpuser@'192.168.18.%' identified by '123456';  //创建WordPress用户和密码[root@centos7 ~]#mysql -uwpuser  -p123456 -h192.168.18.104 //对穿建用户测试,能进入就说明成功了。

3.对nfs服务器设置

[root@centos7 ~]#yum -y install nfs-server  nfs-utils  //安装nfs服务[root@centos7 ~]#systemctl start  nfs-server.service   //启动nfs服务[root@centos7 ~]#systemctl enable   nfs-server.service //开机启用nfs服务[root@centos7 app]#ll   //获取WordPress两个版本并设置连接blog指向wordpress4.8总用量 8lrwxrwxrwx. 1 apache apache   12 824 14:11 blog -> wordpress4.8drwxr-xr-x. 5 apache apache 4096 824 19:59 wordpress4.8drwxr-xr-x. 5 root   root   4096 84 15:54 wordpress4.8.1设置链接:ls -srv /app/wordpress4.8 /app/blog这里也可以放置一个版本到blog里,为什么要设个链接,就是防止升级或某种原因使当前版本不能访问可切换到上一版本。同理对某些服务更新来讲也是一样useradd -u 48  -r -s /sbin/nologin apache  //这里创建Apache账户,这是为了让RS1和RS2连接到这台服务是能以继续行使Apache的权限。 chown -R apache.apache /app/blog/   //设置归属权限[root@centos7 blog]#cp wp-config-sample.php  wp-config.php[root@centos7 blog]#vim wp-config.php //直接对配置文件修改,可省略到时候页面的填写/** WordPress数据库的名称 */define('DB_NAME', 'blogdb');/** MySQL数据库用户名 */define('DB_USER', 'wpuser');/** MySQL数据库密码 */define('DB_PASSWORD', '123456');/** MySQL主机 */define('DB_HOST', '192.168.18.104');[root@centos7 ~]#vim /etc/exports  //编辑nfs配置文件[root@centos7 ~]#cat /etc/exports/app/blog 192.168.18.0/24(rw,all_squash,anonuid=48,anongid=48)对192.168.18.这个网段可挂载,有读写全新,其他用户压榨权限,除 apache[root@centos7 app]#exportfs -r  // 检查[root@centos7 app]#exportfs -v  // 列出/app/wordpress4.8    192.168.18.0/24(rw,wdelay,root_squash,all_squash,no_subtree_check,anonuid=48,anongid=48,sec=sys,rw,secure,root_squash,all_squash)

4.对AP1和AP2挂载nfs.

[root@centos7 ~]#vim /etc/fstab //写进配置文件。可以以后开机自动挂载。[root@centos7 ~]#cat /etc/fstab.....192.168.18.103:/app/blog        /var/www/html/blog       nfs     defaults  0 0 //在最后添加这条记录[root@centos7 ~]#mkdir /var/www/html/blog -pv //没有此目录创建此目录。[root@centos7 ~]#mount -a //对/etc/fstab 新增内容挂载[root@centos7 ~]#df -h   //产看是否挂载成功文件系统                  容量  已用  可用 已用% 挂载点......192.168.18.103:/app/blog   50G   90M   50G    1% /var/www/html/blog  此时已经成功RS2:同样的上述操作。[root@centos7 ~]#ll /var/www/html/blog/ -d   //查看权限显示为Apache ,因为我们对NFS服务器上的blog进行了权限的设定。drwxr-xr-x 5 apache apache 4096 8月  24 20:54 /var/www/html/blog/[root@centos7 ~]#ll /var/www/html/blog/.....-rw-r--r--  1 apache apache   418 9月  25 2013 index.php-rw-r--r--  1 apache apache 19935 1月   3 2017 license.txt-rw-r--r--  1 apache apache  6956 7月  23 19:28 readme.html-rw-r--r--  1 apache apache  5447 9月  28 2016 wp-activate.phpdrwxr-xr-x  9 apache apache  4096 7月  23 19:28 wp-admin-rw-r--r--  1 apache apache   364 12月 19 2015 wp-blog-header.php-rw-r--r--  1 apache apache  1627 8月  29 2016 wp-comments-post.php......

测试


设置好后,测试,出现如上画面说明后端服务器已经设置好了

三、Nginx代理的设置

1.设置代理:

[root@centos7 ~]#vim /etc/nginx/nginx.confhttp {    upstream websrvs {        server 192.168.18.101:80 weight=1 max_fails=2 fail_timeout=3;        server 192.168.18.102:80 weight=1 max_fails=2 fail_timeout=3;(ip_hash;)这里可开启源地址hash算法;    }  //在http 中添加或视情况增删其余配置//调用ngx_http_upstream_module模块 1.upstream name { ... }定义后端服务器组;引入一个新的上下文;只能用于http{}上下文中;默认的调度方法是wrr;2.server address [parameters];定义服务器地址和相关的参数;地址格式:IP[:PORT]HOSTNAME[:PORT]unix:/PATH/TO/SOME_SOCK_FILE参数:weight=number //权重,默认为1;max_fails=number //失败尝试的最大次数;fail_timeout=time //设置服务器为不可用状态的超时时长;backup//把服务器标记为“备用”状态; down//手动标记其为不可用;还可以定义:least_conn; //最少连接调度算法; 当server拥有不同的权重时为wlc;当所有后端主机的连接数相同时,则使用wrr进行调度;ip_hash; //源地址hash算法;能够将来自同一个源IP地址的请求始终发往同一个upstream server;

3.设置主机代理配置

[root@centos7 conf.d]#cat vhost1.conf   //在/etc/nginx/conf.d/目录下创建虚拟主机,主配置文件同样会识别配置。server {    listen 80;  //监听端口    server_name www.ilinux.io;  //主机名    location / {    //uri匹配    root "/data/nginx/vhost1";   //路径    proxy_pass http://websrvs;  //代理后端服务器组名,与Nginx.conf主配置文件里http下,  upstream websrvs;一致    }}[root@centos7 conf.d]#nginx -t  //检查配置文件是否有语法错误nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@centos7 conf.d]#nginx -s reload  //配置文件平滑加载,不需要重启Nginx服务

测试:
在测试机上设置解析

[root@centos7 ~]# cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.18.131  www.ilinux.io172.16.250.111 www.ilinux.io


此时已成功实现nginx代理

2.实现https加密。

1.创建私有CA。[root@centos7 CA]# (umask 077;openssl genrsa -out private/cakey.pem 4096) //创建私钥[root@centos7 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365  //生成公钥自行填写相关信息[root@centos7 CA]#touch index.txt //生成索引文件 必须建立[root@centos7 CA]#echo 01 > serial  //生成证书序列文件 必须建立[root@centos7 nginx]#mkdir ssl //创建文件夹,将放置Nginx的申请的证书文件,及公钥与私钥。[root@centos7 ssl]#(umask 077;openssl genrsa -out nginx.key 2048)  //nginx的私钥[root@centos7 ssl]#openssl req -new -key nginx.key -out nginx.csr //Nginx的公钥[root@centos7 ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365 //向CA申请证书  注意:默认国家,省,公司名称三项必须和CA一致[root@centos7 nginx]#cat conf.d/vhost1.conf server {    listen 80;    server_name www.jie0112.com;     rewrite /blog/(.*)$   https://www.jie0112.com/blog/$1 ; //临时重定向。    location / {     root "/data/nginx/vhost1";    proxy_pass http://websrvs;    }}[root@centos7 nginx]# cat conf.d/vhost1.ssl.conf [root@centos7 nginx]#cat conf.d/vhost1.ssl.conf  //配置重定向的虚拟主机。server {    listen 443 ssl;    //使用 443 端口 ssl 协议    server_name www.jie0112.com;    location / {     root "/data/nginx/vhost1";    proxy_pass http://websrvs;    }    ssl on;    ssl_certificate /etc/nginx/ssl/nginx.crt;    ssl_certificate_key /etc/nginx/ssl/nginx.key;    ssl_session_cache shared:sslcache:20m;    ssl_session_timeout 600s;}//ssl_certificate file;当前虚拟主机使用PEM格式的证书文件;//ssl_certificate_key file;当前虚拟主机上与其证书匹配的私钥文件//ssl_session_cache off | none | [builtin[:size]] [shared:name:size];   builtin[:size]:使用OpenSSL内建的缓存,此缓存为每worker进程私有;//ssl_session_timeout time;客户端一侧的连接可以复用ssl session cache中缓存 的ssl参数的有效时长;如需使用其他功能,参考ngx_http_ssl_module模块:[root@centos7 conf.d]#nginx -t  //检查配置文件是否有语法错误nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@centos7 conf.d]#nginx -s reload  //配置文件平滑加载

测试:

3.增加Nginx的压缩模块。

在server块里面添加如下内容

gzip  on;    //开启压缩gzip_comp_level 6;    //等级为6gzip_min_length 64; //启用压缩功能的响应报文大小阈值;gzip_proxied any;  //任何都压缩gzip_types text/xml text/css  application/javascript; // 压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能[root@centos7 conf.d]#nginx -t [root@centos7 conf.d]#nginx -s reload

4,增加Nginx的缓存模块

[root@centos7 nginx]#cat conf.d/vhost1.ssl.conf  //vhost1.ssl.conf配置文件修改如下,因为已永久重定向就不必在vhost1.conf设置server {    listen 443 ssl;    server_name www.jie0112.com;    location / {     root "/data/nginx/vhost1";    proxy_pass http://websrvs;    }    ssl on;    ssl_certificate /etc/nginx/ssl/nginx.crt;    ssl_certificate_key /etc/nginx/ssl/nginx.key;    ssl_session_cache shared:sslcache:20m;    ssl_session_timeout 600s;    proxy_cache pxycache;    proxy_cache_key $request_uri;    proxy_cache_valid 200 302 301 1h;    proxy_cache_valid any 1m;}//proxy_cache zone | off;指明要调用的缓存,或关闭缓存机制;Context:  http, server, location//proxy_cache_key string;缓存中用于“键”的内容;默认值:proxy_cache_key $scheme$proxy_host$request_uri;//proxy_cache_valid [code ...] time;定义对特定响应码的响应内容的缓存时长;定义在http{...}中;[root@centos7 nginx]#cat  nginx.confhttp {proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;//proxy_cache_path ;定义可用于proxy功能的缓存;Context:    http            //proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];[root@centos7 conf.d]#nginx -t [root@centos7 conf.d]#nginx -s reload

刷新几下 观察是否已缓存,在Nginx主机上查看是否有缓存

[root@centos7 ~]#cd /var/cache/nginx/proxy_cache/[root@centos7 proxy_cache]#tree└── 6    └── a        └── d            └── f7aa5e7d8164aa09aacfe1de7eb14da63 directories, 1 file已成功缓存至本地磁盘

总结

到这里实验就完成,实现简单的Nginx的代理服务,可实现htpps,页面的压缩,和缓存,这些功能只是Nginx的小众功能,还可以设置更为灵活,复杂的功能。它有着大量模块支持!

实验可能有误,如果发现,请提出一块交流^_^

原创粉丝点击