架构师课程笔记day04——Nginx

本文介绍了Nginx从单体到集群的过渡,详细讲解了Nginx的正反向代理概念及示例,包括其在架构中的位置、安装步骤、进程模型和常用命令。此外,讨论了Nginx的核心配置文件、负载均衡策略如url_hash和least_conn,以及HTTPS配置和缓存管理,强调了动静分离的概念和实现方式。
摘要由CSDN通过智能技术生成

 大纲

1.从单体到集群过渡

 

 

 

 

 

 

2.Nginx


 2.1什么是nginx

 2.2常见服务器

 2.3nginx在架构中所处位置

2.4使用率,性能,市场占有率等信息

 

 

 2.5正反向代理啥意思

正向代理

 反向代理

示例

 

2.6安装步骤

Nginx安装步骤 常用命令等

2.7请求链路

 

2.8进程模型

通用模型 一个管事的 多个打工的

2.9通用命令

-s stop  强制关机

-s quit 保存当前配置然后关机

-s reload 刷新配置

-h 帮助命令 里面还有些别的常用命令

[root@VM-4-3-centos sbin]# ./nginx -s stop
[root@VM-4-3-centos sbin]# ./nginx -s quit
nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
[root@VM-4-3-centos sbin]# ./nginx
[root@VM-4-3-centos sbin]# ./nginx -s quit
[root@VM-4-3-centos sbin]# ./nginx -h
nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

2.10核心配置文件各个配置的含义

以下是一个nginx.conf示例

#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       888;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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;
        }
}

 常用配置

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    #限制最小压缩,小于1字节文件不会压缩
    gzip_min_length 1;
    #压缩比 文件越大 压缩越多
    gzip_comp_level 3;
    #定义压缩文件的类型
    gzip_types text/plain application/javascript  image/png text/css text/xml image/gif image/jpeg;

location 的匹配规则
空格 :默认匹配,普通匹配
location / {
root /home;
}
= :精确匹配
location = /imooc/img/face1.png {
root /home;
}
~* :匹配正则表达式,不区分大小写
#符合图片的显示
location ~ \.(GIF|jpg|png|jpeg) {
root /home;
}
~ :匹配正则表达式,区分大小写
#GIF必须大写才能匹配到
location ~ \.(GIF|jpg|png|jpeg) {
root /home;
}
^~ :以某个字符路径开头
location ^~ /imooc/img {
root /home;
}

 跨域与防盗链

#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;

#对源站点验证 防盗链
valid_referers *.imooc.com;
#非法引入会进入下方判断
if ($invalid_referer) {
return 404;
}

2.11负载均衡 url_hash least_conn

根据每次请求的 url 地址, hash 后访问到固定的服务器节点。
upstream tomcats {
# url hash
hash $request_uri;
# 最少连接数
# least_conn
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
server {
listen 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}

2.12 一致性hash原理

这玩意比较通用 见之前redis那篇文章 的4.1部分

分布式缓存技术-redis分布式篇 (redis主从复制,哨兵机制,集群搭建)_我才是真的封不觉的博客-CSDN博客

2.13Nginx的缓存

我们都知道静态文件浏览器一般都会给缓存住

 那么 在nginx中设置缓存的失效时间可以用到以下

expires 10s:  10s后缓存失效

expires @22h30m:22h30m 缓存失效

expires -1h:当前时间前一个小时缓存失效 说明不缓存

expires epoch:这个也是不缓存

expires off:nginx关闭缓存设置 使用浏览器中默认缓存设置

expires max:缓存永不过期

 具体使用方式如下

1. 浏览器缓存:
加速用户访问,提升单个用户(浏览器访问者)体验,缓存在本地
2. Nginx缓存
缓存在nginx端,提升所有访问到nginx这一端的用户
提升访问上游(upstream)服务器的速度
用户访问仍然会产生请求流量
控制浏览器缓存:
location /files {
alias /home/imooc;
# expires 10s;
# expires @22h30m;
# expires -1h;
# expires epoch;
# expires off;
expires max;
}

2.14Nginx的反向代理缓存

当我们的静态资源文件除了存储在这个中间nginx之外还存在 下游的服务器的时候,那么可以使用

 如下缓存设置

# proxy_cache_path 设置缓存目录
# keys_zone 设置共享内存以及占用空间大小
# max_size 设置缓存大小
# inactive 超过此时间则被清理
# use_temp_path 临时目录,使用后会影响nginx性能
proxy_cache_path /usr/local/nginx/upstream_cache keys_zone=mycache:5m max_size=1g inactive=1m use_temp_path=
location / {
proxy_pass http://tomcats;
# 启用缓存,和keys_zone一致
proxy_cache mycache;
# 针对200和304状态码缓存时间为8小时
proxy_cache_valid 200 304 8h;
}

2.15使用Nginx配置HTTPS域名证书

1. 安装 SSL 模块
要在 nginx 中配置 https ,就必须安装 ssl 模块,也就是 : http_ssl_module
进入到 nginx 的解压目录: /home/software/nginx-1.16.1
新增 ssl 模块 ( 原来的那些模块需要保留 )
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module
编译和安装
make
make install
2. 配置 HTTPS
ssl 证书 *.crt 和 私钥 *.key 拷贝到 /usr/local/nginx/conf 目录中。
新增 server 监听 443 端口:
server {
listen 443;
server_name www.imoocdsp.com;
# 开启ssl
ssl on;
# 配置ssl证书
ssl_certificate 1_www.imoocdsp.com_bundle.crt;
# 配置证书秘钥
ssl_certificate_key 2_www.imoocdsp.com.key;
# ssl会话cache
ssl_session_cache shared:SSL:1m;
# ssl会话超时时间
ssl_session_timeout 5m;
# 配置加密套件,写法遵循 openssl 标准
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://tomcats/;

2.16动静分离两种实现方式

CDN

 Nginx

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的封不觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值