Nginx 安装部署

1、虚拟机安装
虚拟机:VMware workstation 16
操作系统:CentOS 7.4
下载地址:https://vault.centos.org/centos/7.4.1708/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso
虚拟机安装CentOS7.4
1 新建虚拟机

2 选择典型
3 选择CentOS镜像
我们在这次学习时使用mini班操作系统镜像,安装速度快,也去除了我们用不到的软件。

4 存储位置

5 虚拟机磁盘配置

6 自定义其他配置

在自定义硬件中,我们可以再次配置虚拟机的内存、cpu等硬件属性,在当前Nginx学习阶段硬件配置不需要过高,默认单核cpu、1G内存即可。
学习时的电脑配置
内存:建议8G以上
磁盘:建议使用SSD
CPU:4核以上主流即可
系统安装
1 虚拟机配置完成之后进入系统安装界面

出现此界面后敲“回车”进入安装程序
2 选择安装语言

3 分区选择
虽然默认会自动帮我们格式化磁盘,但也需要点击确认一下

点击左上角完成即可

4 开始安装
开始

安装过程中我们可以设置密码

安装完成
当出现“重启”按钮时,说明系统已经安装完成

重启后的样子

至此,我们在VMware中对CentOS的基本安装已经完成。
2、Linux配置
配置上网
修改配置网卡配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens33

修改 ONBOOT=yes
重启网络服务
systemctl restart network
测试
ping qq.com

至此,我们的虚拟机就可以访问互联网了。
配置静态ip
之前的网络配置是使用dhcp方式分配ip地址,这种方式会在系统每次联网的时候分配一个ip给我们用,也就是说有可
能系统下次启动的时候ip会变,这样非常不方便我们管理。
配置静态ip首先需要打开网卡配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens33
修改启动协议 BOOTPROTO=static
手动配置ip地址
IPADDR=192.168.44.101NETMASK=255.255.255.0GATEWAY=192.168.44.1DNS1=8.8.8.8
根据大家自己的环境,ip地址可能略有不同。
接下来重启网络服务
systemctl restart network
重启之后 xshell可能无响应,这是因为ip变了,修改xshell配置中的ip重新连接即可。
完整配置截图如下

TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=staticDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yes
IPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33UUID=10ac735e-0b8f-4b19-9747-ff28b58a1547DEVICE=ens33ONBOOT=yesIPADDR=192.168.44.101NETMASK=255.255.255.0GATEWAY=192.168.44.1DNS1=8.8.8.8
不能上网的错误排查
•Vmware中网关是否正确
•直接ping ip是否能通(物理连接排查)
•使用和老师一样版本的软件
•卸载重装最快
一些公网DNS服务器
阿里
223.5.5.5223.6.6.6
腾讯
119.29.29.29182.254.118.118
百度
180.76.76.76
114DNS
114.114.114.114114.114.115.115
谷歌
8.8.8.88.8.4.4
3、Nginx的安装
版本区别
常用版本分为四大阵营
Nginx开源版
Nginx plus 商业版
openresty
Tengine
编译安装
./configure --prefix=/usr/local/nginxmakemake install
如果出现警告或报错
提示
checking for OS+ Linux 3.10.0-693.el7.x86_64 x86_64checking for C compiler ... not found./configure: error: C compiler cc is not found
安装gcc
yum install -y gcc
提示:
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
安装perl库
yum install -y pcre pcre-devel
提示:
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
安装zlib库
yum install -y zlib zlib-devel
提示:
Configuration summary+ using system PCRE library+ OpenSSL library is not used+ using builtin md5 code+ sha1 library is not found+ using system zlib library
表象原因:
出现以上问题的表象原因是,在安装nginx的时候没有指定openssl的解压路径。正确的做法如下:
./configure —prefix=/usr/local/nginx —with-openssl=/usr/local/openssl-1.0.1j —with-http_ssl_module
接下来执行
makemake install
启动Nginx
进入安装好的目录 /usr/local/nginx/sbin
./nginx 启动./nginx -s stop 快速停止./nginx -s quit优雅关闭,在退出前完成已经接受的连接请求./nginx -s reload 重新加载配置
关于防火墙
关闭防火墙
systemctl stop firewalld.service
禁止防火墙开机启动
systemctl disable firewalld.service
放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
安装成系统服务
创建服务脚本
vi /usr/lib/systemd/system/nginx.service
服务脚本内容
[Unit]Description=nginx - web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stopExecQuit=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true[Install]WantedBy=multi-user.target
重新加载系统服务
systemctl daemon-reload
启动服务
systemctl start nginx.service
开机启动
systemctl enable nginx.service
容器(Docker)安装
…
Nginx基础使用

目录结构
进入Nginx的主目录我们可以看到这些文件夹
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
其中这几个文件夹在刚安装后是没有的,主要用来存放运行过程中的临时文件
client_body_temp fastcgi_temp proxy_temp scgi_temp
conf
用来存放配置文件相关
html
用来存放静态文件的默认目录 html、css等
sbin
nginx的主程序
基本运行原理

Nginx配置与应用场景
最小配置
worker_processes
worker_processes 1; 默认为1,表示开启一个业务进程
worker_connections
worker_connections 1024; 单个业务进程可接受连接数
include mime.types;
include mime.types; 引入http mime类型
default_type application/octet-stream;
default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
sendfifile on;
sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
未开启sendfifile

开启后

keepalive_timeout 65;(反向代理)
keepalive_timeout 65;

虚拟主机配置
server {listen 80; 监听端口号server_name localhost; 主机名location / { 匹配路径root html; 文件根目录 (主目录的相对路径)index index.html index.htm; 默认页名称}error_page 500 502 503 504 /50x.html; 报错编码对应页面location = /50x.html {root html;}}
虚拟主机
原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务
servername匹配规则
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
完整匹配
我们可以在同一servername中匹配多个域名
server_name vod.mmban.com www1.mmban.com;
通配符匹配
server_name *.mmban.com
通配符结束匹配(精确匹配的优先级,大于通配符匹配,和配置文件先后顺序无关)
server_name vod.*;
正则匹配
server_name ~^[0-9]+\.mmban\.com$;
反向代理

短网址

反向代理原理图:(外网请求服务器)

正向代理:(用户向访问外网)

通过代理服务器是谁提供的,来辨别是正向代理还是反向代理
lvs软件能降低nginx的流量
proxy_pass http://baidu.com;
proxy_pass和root 目录是二选一的
location / {proxy_pass http://atguigu.com/;}
基于反向代理的负载均衡
upstream httpd {server 192.168.44.102:80;server 192.168.43.103:80;}
tips:自己的单机实现
server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://httpd;#root html;#index index.html index.htm;}#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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}#虚拟主机vhostserver {listen 87;#域名、主机名server_name localhost;location / {root www/dyq;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /www/dyq/50x.html {root html;}}#虚拟主机vhostserver {listen 88;#域名、主机名server_name localhost;location / {root www/lhq;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /www/dyq/50x.html {root html;}}upstream httpd {server 192.168.200.188:88 weight=8;server 192.168.200.188:87 weight=1;}
负载均衡策略
轮询
默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。
weight(权重)
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream httpd {server 127.0.0.1:8050 weight=10 down;server 127.0.0.1:8060 weight=1;server 127.0.0.1:8060 weight=1 backup;}
down:表示当前的server暂时不参与负载
weight:默认为1.weight越大,负载的权重就越大。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。
ip_hash
根据客户端的ip地址转发同一台服务器,可以保持回话。
least_conn
最少连接访问
url_hash
根据用户访问的url定向转发请求
fair
根据后端服务器响应时间转发请求
动静分离
配置反向代理
location / {proxy_pass http://127.0.0.1:8080;root html; index index.htmlindex.htm;}
增加每一个location
location /css {root /usr/local/nginx/static;index index.html index.htm;}location /images {root /usr/local/nginx/static;index index.html index.htm;}location /js {root /usr/local/nginx/static;index index.html index.htm;}
使用一个location
使用正则
location 前缀
/ 通用匹配,任何请求都会匹配到。
= 精准匹配,不是以指定模式开头
~ 正则匹配,区分大小写
~* 正则匹配,不区分大小写
^~ 非正则匹配,匹配以指定模式开头的location
location匹配顺序
- 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
- 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
- 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
- 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)
location ~*/(css|img|js) {root /usr/local/nginx/static;index index.html index.htm;}
root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。
1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的; 2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
3)使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上”/“符号!! 4)alias虚拟目录配置中,location匹配的path目录如果后面不带”/“,那么访问的url地址中这个path目录后面加不加”/“不影响访问,访问时它会自动加上”/“; 但是如果location匹配的path目录后面加上”/“,那么访问的url地址中这个path目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/“,访问就会失败! 5)root目录配置中,location匹配的path目录后面带不带”/“,都不会影响访问。
UrlRewrite
rewrite语法格式及参数语法:
rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容,重定向到replacement,结尾是flag标记。rewrite <regex> <replacement> [flag];关键字 正则 替代内容 flag标记关键字:其中关键字error_log不能改变
正则:perl兼容正则表达式语句进行规则匹配替代内容:将正则匹配的内容替换成replacementflag标记:rewrite支持的flag标记rewrite参数的标签段位置:server,location,ifflag标记说明:last #本条规则匹配完成后,继续向下匹配新的location URI规则break #本条规则匹配完成即终止,不再匹配后面的任何规则redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
实例
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
同时使用负载均衡
应用服务器防火墙配置
开启防火墙
systemctl start firewalld
重启防火墙
systemctl restart firewalld
重载规则
firewall-cmd --reload
查看已配置规则
firewall-cmd --list-all
指定端口和ip访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.44.101" port protocol="tcp" port="8080" accept"
移除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.44.101" port port="8080" protocol="tcp" accept"
网关配置
upstream httpds {server 192.168.44.102 weight=8 down;server 192.168.44.103:8080 weight=2;server 192.168.44.104:8080 weight=1 backup;}location / {rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 redirect; proxy_pass http://httpds ;}
防盗链配置
valid_referers none | blocked | server_names | strings ....;
- none, 检测 Referer 头域不存在的情况。
- blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以“http://” 或 “https://” 开头。
- server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一个。
在需要防盗链的location中配置
valid_referers 192.168.44.101;if ($invalid_referer) {return 403;}
使用curl测试
curl -I http://192.168.44.101/img/logo.png
带引用
curl -e "http://baidu.com" -I http://192.168.44.101/img/logo.png
高可用配置
安装Keepalived
编译安装
下载地址
https://www.keepalived.org/download.html#
使用 ./configure 编译安装
如遇报错提示
configure: error:!!! OpenSSL is not properly installed on your system. !!!!!! Can not include OpenSSL headers files. !!!
安装依赖
yum install openssl-devel
yum安装
yum install keepalived
配置
使用yum安装后配置文件在
/etc/keepalived/keepalived.conf
最小配置
第一台机器
! Configuration File for keepalivedglobal_defs {router_id lb111}vrrp_instance atguigu {state MASTER#网卡名字interface ens33#...virtual_router_id 51#优先级priority 100#间隔检测时间advert_int 1#配对认证服务器 分组authentication {auth_type PASSauth_pass 1111}#虚拟ipvirtual_ipaddress {192.168.44.200}}
第二台机器
! Configuration File for keepalivedglobal_defs {router_id lb110}vrrp_instance atguigu {state BACKUPinterface ens33virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.44.200}}
启动服务
systemctl start keepalived
通过keepalived实现ip漂移
keepalived主要通过检测监听的keepalived进程是否存在,可以通过脚本的方式,检测nginx的访问请求是否是200,如果nginx返回失败了,杀掉keepalive进程,从而实现keeplived监听nginx服务是否存活的效果,再进行ip漂移,实现高可用

Https证书配置
不安全的http协议

双方采用同样的加密算法—-对称加密,不安全的
用非对称加密算法:
公钥私钥

但是,中间拦截密文,伪造私钥,请求篡改后再用公钥加密发给服务器,仍然存在安全问题
openssl

openssl包含:SSL协议库、应用程序以及密码算法库
证书自签名
OpenSSL
系统内置
图形化工具 XCA
下载地址
https://www.hohnstaedt.de/xca/index.php/download
CA签名
阿里云RSA非对称加密算法
主机安装
自定义:oneinstack.com
扩展discuz.net

Discuz! 超过300万站长使用,全球成熟度最高、覆盖率最大的建站系统之一,拥有超过5000款应用。 站长可以方便的通过 Discuz! 搭建社区论坛、知识付费网站、视频直播点播站、企业网站、同城社区、小程序、APP、图片素材站,游戏交流站,电商购物站、小说阅读、博客、拼车系统、房产信息、求职招聘、婚恋交友等等绝大多数类型的网站。 Discuz!自2001年6月面世以来,已有20年的历史,Discuz!性能优异、功能全面、安全稳定,在社区论坛(BBS)软件领域全球市场占有率第一。站长可以不需要任何编程基础,通过简单的安装和设置,在互联网上搭建起具备完善功能、很强负载能力和可高度定制的网站。Discuz! 的基础架构采用世界上最流行的web编程组合PHP+MySQL实现,是一个经过完善设计,适用于各种服务器环境的高效建站解决方案。
