Nginx知识

发布于:2025-02-10 ⋅ 阅读:(39) ⋅ 点赞:(0)

nginx 精简的配置文件

worker_processes  1;
# 可以理解为一个内核一个worker
# 开多了可能性能不好

events {
    worker_connections  1024;
}
# 一个 worker 可以创建的连接数
# 1024 代表默认一般不用改

http {
    include       mime.types;
    # 代表引入的配置文件
    # mime.types 在 nginx.conf 同级目录下
    # 给浏览器看的,让浏览器知道返回的是啥类型,便于好解析

    default_type  application/octet-stream;
    # 如果上面的 mime.types 匹配不到,就采用这个默认的

    sendfile        on;
    # 代表返回的数据是否走中间商,还是直接走网卡返回
    # 中间商就是 nginx 的内存
    
    keepalive_timeout  65;
    # 连接超时数

    # 一个服务器配置
    server {
        listen       80; # 监听的端口
        server_name  localhost; # 配置域名
        # 上面两个的组合要唯一,不然出问题
        
        # 上面两个匹配到了,匹配下面
        location / {
            root   html; # html文件夹里面的意思
            index  index.html index.htm;
        }
        
        # 出现错误了, 状态码,处理根路径 走下面的进行匹配
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

虚拟主机

因为服务器配置过剩产生的

一个 nginx 可以配置多个域名

# 直接复制一个 server 然后改一下就行了
# 按从上往下匹配如果匹配到了就结束
# 通配符匹配 * # 看第二个 server
# 在 server_name 后面可以写多个
# 可以使用正则

http {
    server {
        listen       80; # 监听的端口
        server_name  localhost; # 配置域名

        location / {
            root   html; # html文件夹里面的意思
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80; # 监听的端口
        server_name  *.hello.*; # 配置域名

        location / {
            root   html; # html文件夹里面的意思
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

域名解析相关架构

二级域名

短网址:就是服务器用数据库存一个key和value,value为真实地址,key随便,返回给用户,节省空间。

httpDns:用于app和C/S架构,好像可以避免域名劫持的风险。

反向代理与正向代理

按角色来说的

就是服务器和用户之间要通过代理, 如果把服务器和代理绑在一起就是反向(理解在一个机房),用户和服务器在一起就是正向(家和路由器)

nginx作为反向代理有带宽瓶颈,用户请求过nginx然后与后端交互,后端处理返回给nginx,会产生瓶颈(nginx),可以使用DR架构,就是服务器直接返回给用户(通过机房网关)。

负载均衡

一定要考虑流量倾斜问题

# 下面的那个网址
# 配置比重可以实现负载均衡
httpsdeom = {
    server 网址 weight=?[down # 代表当前机子挂了,这个参数基本不咋用,没啥用];
    server 网址 weight=?[backup # 代表这个机子作为备用机子];
}

server {
        listen       80; # 监听的端口
        server_name  localhost; # 配置域名

        location / {
            proxy_pass 网址(域名);
            # 下面没用了
            # root   html; # html文件夹里面的意思
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

动静分离

就是静态文件放在 nginx 中,其他的放到服务器中处理

# 第一种(单独写,可以不用写正则了,简单)
# js,css,img 都要上传到那个 html 目录下面
location /js {
    root   html;
    index  index.html index.htm;
}

# 正则写法
location ~*/(js|img|css) {
    root   html;
    index  index.html index.htm;
}
rewrite
location / {
    # 就是掩藏网址,浏览器显示你想要的
    rewrite ^/?.html$(你想要的(可以写正则))  /???(原来的) [flag # 标志]
    # last # 匹配完成后继续向下匹配
    # break 匹配完成不会向下继续匹配
    # redirect 302 不会起到隐藏功能
    # permanent 301 不会起到隐藏功能

    root   html;
    index  index.html index.htm;
}

配置 referer

location / {
    valid_referers none 网址 # 空或者网址中包含(ok)
    if($valid_referers){
        return 403;
    }
    
    root   html;
    index  index.html index.htm;
}


网站公告

今日签到

点亮在社区的每一天
去签到