技术流ken

运维拯救世界

DAY16: 课堂记录–Nginx反向代理和负载均衡–技术流ken

NGINX和Apache对比

 

  1. 轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源
  2. nginx 处理静态文件好,静态处理性能比 apache 高三倍以上,apache 在处理动态请求有优势
  3. nginx 作为负载均衡服务器,支持 7 层负载均衡
  4. 抗并发,nginx 以 epoll and kqueue 作为开发模型

 

nginx部署

 

第一步:配置网络yum源

 

[local]
name=local
enabled=1
gpgcheck=0
baseurl=file:///mnt
[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/

 

第二步:下载nginx

[root@ken yum.repos.d]# yum install nginx -y

 

第三步:启动nginx

[root@ken yum.repos.d]# systemctl restart nginx

 

验证防火墙作用:

 

第一步:防火墙关闭状态下下访问浏览器

可以正常访问到

 

第二步:开启防火墙

[root@ken yum.repos.d]# systemctl restart nginx^C
[root@ken yum.repos.d]# systemctl status firewalld
● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@ken yum.repos.d]# systemctl restart firewalld
[root@ken yum.repos.d]# systemctl status firewalld
● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2019-05-27 10:36:17 CST; 1min 5s ago
Docs: man:firewalld(1)
Main PID: 2219 (firewalld)
CGroup: /system.slice/firewalld.service
└─2219 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid

May 27 10:36:16 ken systemd[1]: Starting firewalld – dynamic firewall daemon…
May 27 10:36:17 ken systemd[1]: Started firewalld – dynamic firewall daemon.

 

第三步:访问浏览器

发现已经无法正常访问

 

第四步:设置防火墙规则,放行80端口

[root@ken yum.repos.d]# iptables -I INPUT -p tcp -d 192.168.64.4 -j ACCEPT

 

第五步:浏览器再次查看

发现可以正常访问

 

 

nginx反向代理–动静分离

 

192.168.64.4 主服务器

192.168.64.5 静态节点

192.168.64.7 动态节点

 

第一步:在主服务器部署nginx并配置动静分离规则

location ~ html$ {
proxy_pass http://192.168.64.5;
}
location ~ php$ {
proxy_pass http://192.168.64.7;
}

 

第二步:检测语法重启nginx

[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@ken html]# systemctl restart nginx

 

第二步:配置静态服务器

 

 

第三步:配置动态服务器

[root@ken html]# yum install php httpd -y

编辑/var/www/html/index.php

<?php
phpinfo();
?>

 

第四步:浏览器访问主服务器

 

 

nginx反向代理–不完全代理

 

第一步:编辑nginx文件,编辑规则

location /admin{
proxy_pass http://192.168.64.5;
}

 

第二步:检测并重启

[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx

 

第三步:浏览器访问

访问不到

 

第四步:创建文件

[root@ken html]# mkdir admin
[root@ken html]# echo “56566” > admin/index.html

 

第五步:浏览器再次访问

访问成功

 

总结:不完全代理的话location后面定义的访问uri会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5;
}

比如上面这个规则,意思就是http://192.168.64.5/admin

 

nginx反向代理–不完全代理

 

第一步:编辑nginx文件,编辑规则

location /admin{
proxy_pass http://192.168.64.5/;
}

 

第二步:检测并重启

[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx

 

第三步:浏览器访问

可以访问

 

总结:完全代理的话location后面定义的访问uri不会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5/;
}

比如上面这个规则,意思就是直接http://192.168.64.5

 

nginx负载均衡演示

 

第一步:编写规则

upstream ken {
server 192.168.64.5 weight=6 max_fails=2 fail_timeout=2;
server 192.168.64.7 weight=1 max_fails=2 fail_timeout=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://ken;
}

第二步:检查语法并重启

[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx

 

第三步:浏览器访问主节点

 

 

LNMP架构上线网站

 

第一步:下载相关的软件包

[root@ken html]# yum install nginx php php-mysql mariadb-server php-fpm -y

 

第二步:编辑php匹配规则

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}

 

第三步:检测并重启

[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx

 

第四步:启动php-fpm

[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/

 

[root@ken yum.repos.d]# systemctl restart php-fpm
[root@ken yum.repos.d]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*

 

第五步:重启数据库

[root@ken yum.repos.d]# systemctl restart mariadb

 

第六步:创建数据库添加用户

MariaDB [(none)]> create database ken;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on ken.* to ken@’localhost’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)

 

 

第七步:上传wordpress安装包至nginx网站根目录下/usr/share/nginx/html 并解压

[root@ken html]# yum install unzip -y

[root@ken html]# unzip wordpress-3.3.1-zh_CN.zip

 

第八步:配置数据库文件

[root@ken html]# cp wp-config-sample.php wp-config.php
[root@ken html]# vim wp-config.php

define(‘DB_NAME’, ‘ken’);

/** MySQL 数据库用户名 */
define(‘DB_USER’, ‘ken’);

/** MySQL 数据库密码 */
define(‘DB_PASSWORD’, ‘123’);

 

第九步:浏览器访问

 

 

9 thoughts on “DAY16: 课堂记录–Nginx反向代理和负载均衡–技术流ken

  1. I have to express my appreciation to this writer for rescuing me from this particular situation. Because of surfing throughout the internet and finding ideas which were not powerful, I believed my entire life was done. Living devoid of the approaches to the problems you have sorted out all through your main report is a serious case, and ones which may have in a wrong way damaged my entire career if I had not discovered your website. Your own personal expertise and kindness in taking care of the whole lot was important. I’m not sure what I would have done if I had not come across such a point like this. It’s possible to at this moment relish my future. Thanks a lot so much for your professional and sensible help. I won’t be reluctant to suggest your blog post to any person who needs to have tips on this situation.

  2. I wanted to develop a quick remark so as to thank you for these precious secrets you are showing here. My incredibly long internet search has at the end been compensated with beneficial information to go over with my neighbours. I would express that we website visitors are extremely lucky to be in a fantastic site with so many lovely individuals with valuable strategies. I feel quite privileged to have encountered your entire site and look forward to so many more thrilling moments reading here. Thanks once more for all the details.

  3. I in addition to my guys were actually reading the best tips found on your website and so immediately developed a horrible suspicion I never thanked the web blog owner for them. My young boys were definitely absolutely very interested to read all of them and have in effect in reality been taking pleasure in them. Appreciation for indeed being well thoughtful as well as for picking out varieties of really good topics millions of individuals are really wanting to know about. My honest regret for not expressing gratitude to you earlier.

  4. I enjoy you because of every one of your hard work on this blog. Kate delights in getting into investigation and it’s easy to understand why. My partner and i hear all relating to the powerful form you deliver very useful tips on this web site and therefore invigorate contribution from others on the concern while our child is now discovering a whole lot. Have fun with the remaining portion of the new year. You have been conducting a good job.

  5. My husband and i were very thankful that Edward could complete his web research via the precious recommendations he gained when using the blog. It’s not at all simplistic to simply happen to be handing out procedures which some people have been selling. We remember we’ve got the writer to give thanks to for this. The explanations you made, the simple website navigation, the relationships you make it possible to engender – it’s everything overwhelming, and it’s really aiding our son and our family imagine that that subject is cool, and that is very serious. Thanks for all!

  6. I wish to show my love for your kindness giving support to those individuals that actually need assistance with the niche. Your personal commitment to getting the message throughout ended up being extremely good and have truly empowered women like me to achieve their pursuits. Your new important help means this much to me and additionally to my peers. With thanks; from everyone of us.

  7. Thank you a lot for giving everyone a very special possiblity to read in detail from this blog. It is often so awesome and as well , jam-packed with amusement for me and my office colleagues to visit your website minimum thrice in a week to learn the fresh things you have. Not to mention, I’m always fulfilled considering the very good hints served by you. Some 3 tips in this article are easily the most impressive we have all ever had.

  8. I together with my guys have already been digesting the excellent information located on your site and so unexpectedly I had a terrible feeling I had not expressed respect to you for those strategies. These boys are already as a result happy to see all of them and now have quite simply been enjoying them. Thank you for being simply considerate and for picking such outstanding themes most people are really desperate to learn about. My very own sincere regret for not expressing gratitude to you earlier.

发表评论

电子邮件地址不会被公开。