资源池

nginx官方网站:http://nginx.org/

nginx官方文档:http://nginx.org/en/docs/

本章资源: 点击这里 资源提取码:u2jv


1、nginx简介

  • Nginx (engine x) 是俄罗斯人编写的十分轻量级的、高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务;
  • nginx的特点是占有内存少,并发能力强,nginx 以 epoll and kqueue 作为开发模型,并发能力确实在同类型的网页服务器中表现较好;
  • nginx 作为负载均衡服务器,支持 7 层负载均衡。
  • nginx 应用异步IO:遇到IO就切换

2、nginx对比apache

nginx apache
轻量级服务器 重量级服务器
适合处理静态页面 适合处理动态页面
处理高并发能力比较好
高并发占用资源少
稳定性非常好、技术成熟

3、nginx下载

第一步:配置网络yum源

╭─root@localhost.localdomain ~
╰─➤ vim /etc/yum.repos.d/local.repo
...
[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
...
╭─root@localhost.localdomain ~
╰─➤ mount /dev/cdrom /mnt #挂载cd
mount: /dev/sr0 is write-protected, mounting read-only
╭─root@localhost.localdomain ~
╰─➤ yum repolist #查看

第二步:下载nginx

╭─root@localhost.localdomain ~
╰─➤ yum install nginx -y

第三步:启动nginx

╭─root@localhost.localdomain ~
╰─➤ systemctl start nginx

第四步:防火墙放行80端口

╭─root@localhost.localdomain ~
╰─➤ iptables -I INPUT -p tcp --dport 80 -j ACCEPT

第五步:浏览器访问

4、nginx主配置文件简介

╭─root@localhost.localdomain /etc/nginx
╰─➤ cat nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; #运行nginx服务的属主
worker_processes auto; #进程数与内核数相同
error_log /var/log/nginx/error.log; #指定错误日志
pid /run/nginx.pid; #进程id # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; #加载的模块 events {
worker_connections 1024; #一个进程可以接受多少用户请求
} http { # http下定义了网站的信息
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#log_format定义了日志的格式 access_log /var/log/nginx/access.log main;
#access_log定义访问网站的日志记录 sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; #文件传输的规则 include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; #指定配置文件
server{ #定义一个新的网站虚拟主机
listen 80; #监听端口
root /var/www/html; #指定网站根目录
index index.html; #指定网站首页文件
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / { #定义访问规则
} error_page 404 /404.html; #指定错误文件
location = /40x.html {
root /var/www/html #可以指定错误文件目录
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} # Settings for a TLS enabled server.
# 启用TLS的服务器的设置
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# } }

5、nginx反向代理

nginx反向代理讲解

location  (匹配规则)+[匹配内容] {
proxy_pass url;
}

1、匹配规则有五种(优先等级由高到低)

 =       # = 表示精确匹配,只有完全匹配上才能生效
^~ #一般匹配一个目录
~ #区分大小写的正则匹配
~* #不区分大小写正则匹配
/ #通用匹配

2、完全代理与不完全代理

假设:[匹配内容] =admin

url=http://192.168.64.4;
不完全代理:
如果不加斜杆上面location表示的含义是请求http://192.168.64.4/admin
客户端admin文件或者目录必须存在!
[匹配内容] 会自动填补到查找地址的后面. url=http://192.168.64.4/;
完全代理:
如果加斜杆上面location表示的含义是请求http://192.168.64.4/
客户端admin文件或者目录不需要存在!
[匹配内容] 不会自动填补到查找地址的后面.

动静分离实验

第一步:布置环境

192.168.80.3 --->主服务器

192.168.80.4 --->静态节点 (apache)

192.168.80.5 --->动态节点 (apache)

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

╭─root@localhost.localdomain /etc/nginx
╰─➤ vim /etc/nginx/nginx.conf ...
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location ~ html$ {
proxy_pass http://192.168.80.4; #分号结束
}
location ~ php$ {
proxy_pass http://192.168.80.5;
}
... ╭─root@localhost.localdomain /etc/nginx
╰─➤ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
╭─root@localhost.localdomain /etc/nginx
╰─➤ systemctl restart nginx

第三步:配置静态服务器

╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ yum install httpd -y
╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ echo "i am html" >> /var/www/html/index.html
╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ systemctl restart httpd.service

第四步:配置动态服务器

╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ yum install httpd php -y
╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ vim /etc/httpd/conf/httpd.conf
...
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
... ╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ systemctl restart httpd.service
╭─root@localhost.localdomain /etc/yum.repos.d
╰─➤ vim /var/www/html/index.php
...
<?php
phpinfo();
?>
...

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

6、负载均衡

weight :权重

max_fails:最大失败次数

fail_timeout:失败等待时间

eg:max_fails=2 fail_timeout=2

表示:请求2s没有回复记1次失败;2次失败将该服务器提出该upstream组;

第一步:修改配置文件

╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ vim /etc/nginx/nginx.conf ...
include /etc/nginx/conf.d/*.conf;
upstream du1 {
server 192.168.80.4 weight=5 max_fails=2 fail_timeout=2;
server 192.168.80.5 weight=2 max_fails=2 fail_timeout=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
proxy_pass http://du1;
}
...

第二步:检测语法并重启

╭─root@localhost.localdomain /usr/share/nginx/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@localhost.localdomain /usr/share/nginx/html
╰─➤ systemctl restart nginx.service

第三步:监控access日志

192.168.80.4

╭─root@localhost.localdomain /etc/httpd/logs
╰─➤ echo '' > /etc/httpd/logs/access_log
╭─root@localhost.localdomain /etc/httpd/logs
╰─➤ cat /etc/httpd/logs/access_log ╭─root@localhost.localdomain /etc/httpd/logs
╰─➤ tail -f /etc/httpd/logs/access_log

192.168.80.5

╭─root@localhost.localdomain /var/www/html
╰─➤ echo '' > /etc/httpd/logs/access_log
╭─root@localhost.localdomain /var/www/html
╰─➤ tail -f /etc/httpd/logs/access_log

第四步、浏览器访问

192.168.80.4

╭─root@localhost.localdomain /etc/httpd/logs
╰─➤ tail -f /etc/httpd/logs/access_log 192.168.80.3 - - [29/May/2019:18:40:58 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:03 +0800] "GET / HTTP/1.0" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:03 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:03 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:04 +0800] "GET / HTTP/1.0" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:04 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:04 +0800] "GET / HTTP/1.0" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:04 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:05 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:05 +0800] "GET / HTTP/1.0" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:05 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:06 +0800] "GET / HTTP/1.0" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:06 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"

192.168.80.5

╭─root@localhost.localdomain /var/www/html
╰─➤ tail -f /etc/httpd/logs/access_log 192.168.80.3 - - [29/May/2019:18:40:59 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:03 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:04 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:05 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
192.168.80.3 - - [29/May/2019:18:41:05 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"

7、拓展:负载均衡组合动静分离

8、LNMP架构上线动态网站(实验)

第一步:下载相关软件

╭─root@localhost.localdomain ~
╰─➤ yum install nginx php php-mysql mariadb-server php-fpm -y

第二步:编辑php匹配规则

╭─root@localhost.localdomain ~
╰─➤ vim /etc/nginx/nginx.conf ...
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.php index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
}
location ~ php$ {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
...
╭─root@localhost.localdomain ~
╰─➤ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
╭─root@localhost.localdomain ~
╰─➤ systemctl restart nginx.service

第三步:启动php-fpm

╭─root@localhost.localdomain ~
╰─➤ systemctl restart php-fpm

第四步:启动数据库

╭─root@localhost.localdomain ~
╰─➤ systemctl restart mariadb

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

╭─root@localhost.localdomain ~
╰─➤ mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database du;
Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on *.* to du@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit
Bye

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

  • 注意:wordpress安装包本章开头资源池中寻找
╭─root@localhost.localdomain ~
╰─➤ cd /usr/share/nginx/html
╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ yum install unzip lrzsz -y
╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ rz ╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ unzip wordpress-3.3.1-zh_CN.zip
╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ cp ./wordpress/* . -a
╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ cp wp-config-sample.php wp-config.php -a
╭─root@localhost.localdomain /usr/share/nginx/html
╰─➤ vim wp-config.php
...
/** WordPress 数据库的名称 */
define('DB_NAME', 'du'); /** MySQL 数据库用户名 */
define('DB_USER', 'du'); /** MySQL 数据库密码 */
define('DB_PASSWORD', '123'); /** MySQL 主机 */
define('DB_HOST', 'localhost');
...

第七步:浏览器访问


nginx详解、反向代理、负载均衡和LNMP架构的更多相关文章

  1. nginx详解反向代理,负载均衡,LNMP架构上线动态网站

    1.nginx介绍 nginx.org Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/ ...

  2. nginx 详解反向代理负载均衡

    什么是反向代理负载均衡 使用代理服务器可以将请求转发给内部的Web服务器,使用这种加速模式显然可以提升静态网页的访问速度.因此也可以考虑使用这种技术,让代理服务器将请求 均匀转发给多台内部Web服务器 ...

  3. Nginx特性验证-反向代理/负载均衡/页面缓存/URL重定向

    原文发表于cu:2016-08-25 参考文档: Nginx 反向代理.负载均衡.页面缓存.URL重写等:http://freeloda.blog.51cto.com/2033581/1288553 ...

  4. nginx详解反向代理、负载均衡、LNMP架构上线动态网站(week4_day1_part1)-技术流ken

    nginx介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理 ...

  5. nginx详解反向代理、负载均衡、LNMP架构上线动态网站

    简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.N ...

  6. nginx实现http反向代理+负载均衡

    原理 反向代理:反向代理(reverse proxy)方式是指以代理来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  7. nginx 虚拟主机+反向代理+负载均衡

    nginx是一款免费.开源的http服务器,它是由俄罗斯程序设计师开发的,官方测试,nginx能支撑5万的并发量,主要功能有虚拟主机.反向代理和负载均衡等. nginx配置 # 全局块 ... # e ...

  8. 项目实战2.2—nginx 反向代理负载均衡、动静分离和缓存的实现

    实验一:实现反向代理负载均衡且动静分离 1.环境准备: 机器名称 IP配置 服务角色 备注 nginx VIP:172.17.11.11 反向代理服务器 开启代理功能 设置监控,调度 rs01 RIP ...

  9. 项目实战2.1—nginx 反向代理负载均衡、动静分离和缓存的实现

    总项目流程图,详见 http://www.cnblogs.com/along21/p/8000812.html 实验一:实现反向代理负载均衡且动静分离 1.环境准备: 机器名称 IP配置 服务角色 备 ...

  10. 项目实战02:nginx 反向代理负载均衡、动静分离和缓存的实现

    目录 实验一:实现反向代理负载均衡且动静分离 1.环境准备: 2.下载编译安装tengine 3.设置代理服务器的配置文件 4.启动tengine服务 5.开启后端的web服务 6.测试 实验二:ng ...

随机推荐

  1. 整数划分(硬币问题)(dp)

    题目描述 考试时思路 本菜狗考试的时候,第一扁打了纯dfs,15分拿了9分 后面看了时限400ms,多组数据,以为会卡常数,然后就想着先dp打表然后再直接O(1)查询 后面发现自己想多了,数据有点水- ...

  2. 每日一个linux命令4

    mkdir命令 linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. mkdir test    创建一个空目录 ...

  3. SQL Server On Linux:基于实际项目案例,总结功能支持情况及相关问题解决方案,讲如何快速完成迁移

    上个月,有个朋友问我说Sql Sever向Mysql迁移有什么好的经验分享,他们公司客户明确提出不再提供Windows服务器,现在计划Mysql迁移.我说Mysql迁移成本太高了,不妨可以了解一下SQ ...

  4. Spring Boot 2.x基础教程:使用Flyway管理数据库版本

    之前已经介绍了很多在Spring Boot中使用MySQL的案例,包含了Spring Boot最原始的JdbcTemplate.Spring Data JPA以及我们国内最常用的MyBatis.同时, ...

  5. WebSocket协议中文版

    WebSocket协议中文版 摘要 WebSocket协议实现在受控环境中运行不受信任代码的一个客户端到一个从该代码已经选择加入通信的远程主机之间的全双工通信.用于这个安全模型是通常由web浏览器使用 ...

  6. DevOps,CI,CD,自动化简单介绍

    前言: 随着企业应用的不断迭代,不断扩大,应用的发布发布可能涉及多个团队,如pc端,手机端,小程序端等等.应用发布也就成为了一项高风险,高压力的超过过程,以及应用的开发迭代的沟通,测试成本也大大的变得 ...

  7. 【JavaWeb】Filter 过滤器

    Filter 过滤器 简介 Filter 过滤器是 JavaWeb 三大组件之一 Filter 过滤器是 JavaEE 的规范,也就是接口 Filter 过滤器的作用是 拦截请求,过滤响应 拦截请求的 ...

  8. SpringBoot 集成Shiro之使用Redis缓存授权认证信息

    因为用户认证与授权需要从数据库中查询并验证信息,但是对于权限很少改变的情况,这样不断从数据库中查询角色验证权限,对整个系统的开销很大,对数据库压力也随之增大.因此可以将用户认证和授权信息都缓存起来,第 ...

  9. 详解 TCP的三次握手四次挥手

    本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...

  10. Python基础语法4-运算符

    Python提供了一系列丰富的运算符,包括:  Ø算术运算符  Ø赋值运算符  Ø关系运算符  Ø逻辑运算符 Ø位运算符  Ø三元运算符 Ø身份运算符 Ø成员运算符