Nginx常用版本

Nginx开源版

Nginx Plus 商业版

Openresty

Tengine

Nginx的编译安装

到官网下载最新版/稳定版tar包。使用xftp等工具上传到linux中!

  1. tar -zxf nginx-1.21.6.tar.gz #解压到当前文件夹
  2. yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel gd gd-devel libtool zlib zlib-devel make #c语言的编译器 pcre包
  3. groupadd nginx
  4. useradd -g nginx nginx
  5. ./configure --prefix=/usr/local/nginx \
  6. --user=nginx \
  7. --group=nginx \--with-http_stub_status_module \
  8. --with-http_gzip_static_module \
  9. --with-http_flv_module \
  10. --with-http_ssl_module \
  11. --with-http_mp4_module \
  12. --with-stream \
  13. --with-http_realip_module \
  14. --with-http_v2_module \
  15. --with-http_sub_module \
  16. --with-http_image_filter_module #编译安装nginx,并且设置安装路径
  17. make && make install
  18. cd /usr/local/nginx/ #进入配置文件

image.png

启动nginx

  1. cd /usr/local/nginx/sbin #进入sbin目录
  2. ./nginx #启动nginx
  3. systemctl stop firewalld #关闭防火墙
  4. systemctl disable firewalld #禁止防火墙自启
  5. firewall-cmd --zone=public --add-port=80/tcp --permanent #放行

image.png

nginx命令

  1. ./nginx #启动
  2. ./nginx -s stop #快速停止
  3. ./nginx -s quit #优雅的停止,在退出前完成已经接受的连接请求
  4. ./nginx -s reload #重载

安装成系统服务

  1. vim /usr/lib/systemd/system/nginx.service #设置配置文件
  2. [Unit]
  3. Description=nginx - web server
  4. After=network.target remote-fs.target nss-lookup.target
  5. [Service]
  6. Type=forking
  7. PIDFile=/usr/local/nginx/logs/nginx.pid
  8. ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  9. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  10. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  11. ExecStop=/usr/local/nginx/sbin/nginx -s stop
  12. ExecQuit=/usr/local/nginx/sbin/nginx -s quit
  13. PrivateTmp=true
  14. [Install]
  15. WantedBy=multi-user.target

Nginx基本使用

Nginx目录结构

image.png
以_temp结尾的文件夹都是启动服务器后的临时文件夹,暂时不用管

conf:配置文件文件夹。最主要的就是nginx.conf
html:里面放网页,并且index.html是主文件夹
logs:放日志文件-access.log正常访问日志-error错误日志

Nginx.conf最小配置

  1. worker_processes 1; #开启一个进程。能开几个需要根据CPU来判断
  2. # 只启动一个进程,nginx是多进程单线程模型,但是使用了epoll sendfile 非阻塞io。
  3. ### 主要是网络连接相关的配置
  4. events {
  5. # worker能连接的客户端数量 1024个
  6. worker_connections 1024;
  7. # use epoll; 事件驱动模型 select|poll|epoll|resig
  8. }
  9. ### http的配置
  10. http {
  11. include mime.types; #将别的配置文件引到当前配置文件中,并且文件中写的是请求头,文件类型。
  12. #mime.types 中写的是文件类型,判断文件是要展示还是下载。
  13. default_type application/octet-stream; #如果不包含在mime.types中,就用application/octet-stream的方式传输给客户端
  14. # application/octet-stream 下载
  15. # text/html 文本显示
  16. sendfile on; # 开启sendfile系统调用
  17. #数据0拷贝,免去一次调度过程,一次复制过程。直接让网络接口去读取这个文件传送给用户
  18. #如果不开启,就是先读取再到内存中再发送给网络接口。
  19. keepalive_timeout 65; #保持连接 超时时间
  20. #如果开多个端口,多个页面就是虚拟主机。
  21. server {
  22. listen 80; #监听端口号
  23. server_name localhost; #配置域名,主机名
  24. #子目录,子路径
  25. location / { #重点
  26. #
  27. root html;
  28. index index.html index.htm;
  29. }
  30. error_page 500 502 503 504 /50x.html; #错误代码 转向到 /50x.html
  31. location = /50x.html {
  32. root html;
  33. }
  34. }
  35. }

Host解析域名

  1. C:\Windows\System32\drivers\etc\hosts #修改hosts 建议用editplus打开
  2. #我们在末尾添加:192.168.218.151 liao.com

image.png

此时通过liao.com 会直接跳转到192.168.218.151

公网域名配置与泛域名解析实战

可以根据各个公有云的教程来做

Nginx虚拟主机域名配置

创建其他的文件,用来让其他虚拟主机显示

  1. mkdir /www
  2. cd /www
  3. mkdir wwwroot
  4. mkdir test
  5. vim wwwroot/index.html #随便输入内容
  6. vim test/index.html #随便输入内容
  1. #下面这段是虚拟主机 因为是虚拟的所以我们可以创建很多个。但是端口不要重合!
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. location / {
  6. root html;
  7. index index.html index.htm;
  8. }
  9. error_page 500 502 503 504 /50x.html;
  10. location = /50x.html {
  11. root html;
  12. }
  13. }
  14. #添加新的虚拟主机:
  15. server {
  16. listen 88;
  17. server_name localhost;
  18. location / {
  19. root /www/wwwroot;
  20. index index.html index.htm;
  21. }
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. }

此时你会发现,80和88端口都可以访问。

我们还可以通过修改子域名进行访问特殊的文件夹

  1. server {
  2. listen 80;
  3. server_name a.mliaoyyds.top; #a.liao进入/www/wwwroot
  4. location / {
  5. root /www/wwwroot;
  6. index index.html index.htm;
  7. }
  8. error_page 500 502 503 504 /50x.html;
  9. location = /50x.html {
  10. root html;
  11. }
  12. }
  13. #添加新的虚拟主机:
  14. server {
  15. listen 80;
  16. server_name *.liao.com; #*.liao进入/www/test
  17. location / {
  18. root /www/test;
  19. index index.html index.htm;
  20. }
  21. error_page 500 502 503 504 /50x.html;
  22. location = /50x.html {
  23. root html;
  24. }
  25. }

servername多种匹配模式

  1. #完整匹配
  2. server_name a.mliaoyyds.top; #只能通过a.mliaoyyds.top访问指定位置
  3. #通配符匹配
  4. server_name *.mliaoyyds.top; #不管是任何子域名开头都访问指定位置
  5. #通配符结束匹配
  6. server_name www.mliaoyyds.*; #不管是任何域名结尾都能访问指定位置
  7. #正则匹配
  8. server_name ~^[0-9]+\.mliaoyyds\.top$;
  9. #~ 设置以正则匹配
  10. #每个 . 都要添加\
  11. #并且要以$结尾

Nginx反向代理与负载均衡

image.png
正常流程:
用户通过 互联网访问 到机房网关路由 接收数据到Nginx nginx再把流量发送到应用服务器 服务器再传回到nginx nginx再传给用户。
注意:加入tomcat速度为100MB但nginx为10MB。最后用户的传输效率就为10MB无法改变。

正向代理与反向代理

正向代理类似一个跳板机,代理访问外部资源。
比如早些时候我们连接上网络了,但是就是无法访问baidu。我们就通过正向代理服务器(路由器),就可以访问baidu,然后baidu返回数据给到正向代理服务器然后再发送给我们。
(1)访问原来无法访问的资源,如google
(2)可以做缓存,加速访问资源
(3)对客户端访问授权,上网进行认证
(4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息
在正向代理中,我们和正向代理服务器是一家,进行的操作最多。

反向代理(Reverse Proxy)实际运行方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
(1)保证内网的安全,阻止web攻击,大型网站,通常将反向代理作为公网访问地址,Web服务器是内网
(2)负载均衡,通过反向代理服务器来优化网站的负载
在反向代理中,反向代理服务器和web服务器是一家,他们之间的操作最多。

Nginx负载均衡

我们将一批web服务器,作为一个集群来看待。用户访问nginx服务器,nginx将内容通过算法分配到集群中的服务器上。因为有时候修改机器时可能会停止机器或者机器故障无法访问,此时就负载均衡就会帮用户跳转到一个可以使用的机器上。因为有retry功能,所以当机器故障才会自动跳转。

  1. 解决突然出现的宕机,故障等。
  2. 合理分配流量。

Nginx反向代理实操

再创建一台机器作为web服务器。一台作为反向代理服务器。

外网代理

在反向代理服务器操作

  1. vim /usr/local/nginx/conf/nginx.conf #进入配置文件
  2. location / {
  3. proxy_pass http://www.4399.com;
  4. #proxy_pass 则是反向代理网页,并且后面跟着网页链接即可!
  5. #root /www/wwwroot; 如果配置的是root 则是访问网页后在文件夹中显示文件
  6. #index index.html index.htm;
  7. }
  8. systemctl restart nginx

注意:proxy_pass如果连接外网一定要填写正确的地址,看清是http还是https,并且开头www也不能写错。否则反向代理就会变成网页跳转。

image.png

内网代理

还是在反向代理服务器操作

  1. vim /usr/local/nginx/conf/nginx.conf
  2. location / {
  3. proxy_pass http://192.168.218.152; #因为默认也是http协议,所以前面httpd即可。
  4. #proxy_pass 则是反向代理网页,并且后面跟着网页链接即可!
  5. #root /www/wwwroot; 如果配置的是root 则是访问网页后在文件夹中显示文件
  6. #index index.html index.htm;
  7. }
  8. systemctl restart nginx

image.png

负载均衡实操

再创建一台机器作为web服务器。
还是在反向代理服务器操作

  1. vim /usr/local/nginx/conf/nginx.conf
  2. http {
  3. include mime.types;
  4. default_type application/octet-stream;
  5. sendfile on;
  6. keepalive_timeout 65;
  7. upstream http_001{ #这里和后面一定要一样,并且upstream和server是一个等级的
  8. server 192.168.218.152:80;
  9. server 192.168.218.153:80;
  10. }
  11. server {
  12. listen 80;
  13. server_name localhost;
  14. location / {
  15. proxy_pass http://http_001; #因为变成了多个,所以这里类似创建了数组 里面可以随便写 但是前面一定要写对
  16. #root /www/wwwroot;
  17. #index index.html index.htm;
  18. }
  19. systemctl restart nginx

此时基础的反向代理/负载均衡就成功了。默认算法是雨露均沾型。

负载均衡策略 权重

比如有些机器的配置略微差一点,我们给他的权重低一些,用户访问也就少一些。合理分配。

  1. vim /usr/local/nginx/conf/nginx.conf
  2. #假设第一台配置高,第二台配置低。我们可以进行如下配置
  3. upstream httpd{
  4. server 192.168.218.152:80 weight=8;
  5. server 192.168.218.153:80 weight=2;
  6. }
  7. systemctl restart nginx

会发现访问很多次都是152,153次数会少一些。还有更多参数
down:不参数负载
backup:当没有负载机器时使用

  1. vim /usr/local/nginx/conf/nginx.conf
  2. upstream httpd{
  3. server 192.168.218.152:80 weight=8 down; #不参数负载
  4. server 192.168.218.153:80 weight=2 backup; #当没有负载机器时使用
  5. }
  6. systemctl restart nginx

不过最常用的就是权重。

不常用的负载均衡策略

当负载均衡时,如果是默认的算法,并不会将我们访问网页的cookie,session等信息共享,刷新到了其他网页,这些信息就会消失需要重新填写。
不常用主要原因:不能动态的上下线。并且只能通过修改配置文件,既然修改了就需要重启,非常麻烦。

  1. ip_hash
  2. #根据客户端的ip地址转发同一台服务器,可以保持会话。
  3. least_conn
  4. #最少连接访问
  5. fair
  6. #根据后端服务器响应时间转发请求 需要第三方组件
  7. url_hash
  8. #根据用户访问的url定向转发请求 定向流量转发
  9. #常用在固定资源,不在统一服务器
  10. #比如一批文件分散在3台服务器,你需要访问该文件的服务器,就根据url定位到有该文件的服务器。

常见的解决方法

将session放入到一个额外的服务器内,其中使用redis存储sessio。当用户访问了服务器,服务器会从redis服务器上获取session,这样就共享了session。不适用大规模场景。
大规模场景会下发token。会额外设置一台服务器来做权限校验,用户进入服务器后会对在权限校验服务器中进项校验,然后下发一个token,将token记录到文件中。

Nginx动静分离

就是区分开动态文件和静态文件
动态资源:用户多次访问,资源和源代码可能会发生改变。比如JS CSS
静态页面:用户多次访问,资源和源代码永远不会发生改变。比如视频 图片

  • 动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路
  • 动静分离简单的概括是:动态文件与静态文件的分离。
  • 伪静态:网站如果想被搜索引擎搜素到,动态页面静态技术freemarker等模版引擎技术

创建一台新机器里面运行Tomcat,其中有静态和动态资源。
静态资源是js img css文件夹。所以将这些文件件删除,并移动到nginx服务器中。html文件夹下。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://192.168.218.154:80; #设置代理到tomcat服务器
  6. }
  7. location /css { #在tomcat页面中,是以css/xxx 格式访问。所以我们这里写localhost就写/css
  8. #因为nginx中配置了/js /img /css他们优先级是最高的,会最先被访问。因为存在了所以这些服务器就不用再额外添加这三个文件了
  9. root html; #因为是放到html下,所以这里要写html。
  10. index index.html index.htm
  11. }
  12. location /js {
  13. root html;
  14. index index.html index.htm
  15. }
  16. location /img {
  17. root html;
  18. index index.html index.htm
  19. }
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. root html;
  23. }
  24. }

前面用了3个localtion来设置3个文件夹,当然可以用正则匹配

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://192.168.218.154:80; #设置代理到tomcat服务器
  6. }
  7. location ~*/(js|css|img) {
  8. root html;
  9. index index.html index.htm
  10. }
  11. error_page 500 502 503 504 /50x.html;
  12. location = /50x.html {
  13. root html;
  14. }
  15. }

URLRewrite

URL Rewrite即URL重写,就是把传入Web的请求重定向到其他URL的过程。URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。
比如将www.baidu.com/index.asp?id=123 写成 www.baidu.com/123.html

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; #
  6. proxy_pass http://192.168.218.154:80; #设置代理到tomcat服务器
  7. }
  8. location ~*/(js|css|img) {
  9. root html;
  10. index index.html index.htm
  11. }
  12. error_page 500 502 503 504 /50x.html;
  13. location = /50x.html {
  14. root html;
  15. }
  16. }

末尾的flag标记说明
last #本条规则匹配完成后,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

防盗链

防止资源被盗用,主要方法就是区分哪些请求是非正式的用户请求。
比如某网站有logo等是使用动静分离调用,但是当我们通过反向外网代理等方法,可以直接代理他们的图片,或者直接访问图片,设置防盗链后,就不会被反向代理等操作代理使用了。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; #
  6. proxy_pass http://192.168.218.154:80; #设置代理到tomcat服务器
  7. }
  8. location ~*/(js|css|img) {
  9. valid_refers 192.168.218.158; #检测是否来自该网页(一般写原网站)
  10. if ($invaild_refer){ #如果不来自该网页,则返回403
  11. return 403;
  12. }
  13. root html;
  14. index index.html index.htm
  15. }
  16. error_page 500 502 503 504 /50x.html;
  17. location = /50x.html {
  18. root html;
  19. }
  20. }

valid_refers none 192.168.218.158; #当没有refer时,也能访问。(直接访问时就是没有refer)
valid_refers blocked 192.168.218.158; #当检测是否以http https开头

curl测试防盗链

  1. 正常访问
  2. curl -I http://192.168.218.154/img/logo.png #-I 只返回头信息
  3. 带引用访问
  4. curl -e "http://192.168.218.154" -I http://192.168.218.154/img/logo.png

盗链资源返回页面或提示图片

当设置了返回参数,比如403,那么我们可以自定义显示返回页面。大多时候在企业中使用

  1. 错误码跳转:
  2. error_page 401 /401.html;
  3. location = /401.html {
  4. root html;
  5. }
  6. 返回报错图片:
  7. location ~*/(js|css|img) {
  8. valid_refers 192.168.218.158;
  9. if ($invaild_refer){ #设置返回的参数为图片
  10. rewrite ^/ /img/x.png break;
  11. }
  12. root html;
  13. index index.html index.htm
  14. }

高可用快速搭建

  1. 创建三台机器,分别都yum安装keepliaved
  2. #三台机器做以下操作
  3. yum install -y keepalived #yum安装keepalived
  4. #第一台机器
  5. vim /etc/keepalived/keepalived.conf
  6. ! Configuration File for keepalived
  7. global_defs {
  8. router_id ksnginx #路由ID
  9. }
  10. vrrp_instance liao { #设置vrrp 名称自定义
  11. state MASTER #设置为主节点
  12. interface ens33 #设置网卡
  13. virtual_router_id 51 #设置虚拟路由id
  14. priority 100 #设置主备竞选的优先级
  15. advert_int 1
  16. authentication { #高可用服务器之间通信,需要一个小服务器来通信,下面设置通信密码
  17. auth_type PASS
  18. auth_pass 1111
  19. }
  20. virtual_ipaddress {
  21. 192.168.218.200 #设置访问的虚拟IP
  22. }
  23. }
  24. #第二台机器
  25. vim /etc/keepalived/keepalived.conf
  26. ! Configuration File for keepalived
  27. global_defs {
  28. router_id ksnginx1 #路由ID要不相同
  29. }
  30. vrrp_instance liao {
  31. state BACKUP
  32. interface ens33
  33. virtual_router_id 51
  34. priority 50
  35. advert_int 1
  36. authentication {
  37. auth_type PASS
  38. auth_pass 1111
  39. }
  40. virtual_ipaddress {
  41. 192.168.218.200
  42. }
  43. }
  44. #第三台机器
  45. vim /etc/keepalived/keepalived.conf
  46. ! Configuration File for keepalived
  47. global_defs {
  48. router_id ksnginx2
  49. }
  50. vrrp_instance liao {
  51. state BACKUP
  52. interface ens33
  53. virtual_router_id 51
  54. priority 49
  55. advert_int 1
  56. authentication {
  57. auth_type PASS
  58. auth_pass 1111
  59. }
  60. virtual_ipaddress {
  61. 192.168.218.200
  62. }
  63. }

不安全的http协议