nginx_基础

3

概念:

  • 正向代理是代理客户端
  • 反向代理是代理服务端
    ![[Pasted image 20240515142517.png]]
    ![[Pasted image 20240515142543.png]]

部署:

  • 有外网yum源:直接yum部署
  • 离线内网rpm包:nginx.org/packages/centos中找rpm包 wget下载 rmp -ivh name
systemctl 启动只能systemctl关闭
nginx启动只能nginx -s stop关闭

常用命令:

nginx                启动(强制停止)
nginx - V          查看各种路径 
nginx -t            检查配置文件
nginx -s stop    停止 (一项一项停止)
nginx -s quit     安全退出
nginx -s reload 重新加载配置文件
nginx进程分为一个master(包工头)进程和多个work(打工人)进程 一般reload只会改变work的pid 除非restart才会改变master的pid
! ps时候如果master后只有nginx就是nginx启动的 /usr/sbin/nginx就是systemd启动的

三个特点:

  • 反向代理
  • 负载均衡
  • 动静分离

防火墙规则:

firewall-cmd --list-all 查看防火墙规则
firewall-cmd --add-port=81/tcp 
firewall-cmd --add-port=81/tcp --permanent 永久 持续性
firewall-cmd -- remove-port=81/tcp --permanent 
firewall-cmd --reload  重载

![[Pasted image 20240515151110.png]]

nginx常见目录

/etc/nginx/ #全局配置文件
|-- nginx.conf
|-- mime.types
|-- conf.d/  # 额外的配置文件
|   |-- example.conf
|-- sites-available/
|   |-- default
|-- sites-enabled/
|   |-- default -> /etc/nginx/sites-available/default
/var/log/nginx/ #日志文件目录
|-- access.log
|-- error.log
/usr/share/nginx/html/ #Web 根目录
|-- index.html
/var/lib/nginx/ #临时文件目录
|-- proxy/
|-- fastcgi/
|-- uwsgi/
|-- scgi/
/run/nginx/ #运行时目录
|-- nginx.pid
/usr/lib/nginx/modules/ # 模块目录
|-- ngx_http_module.so
|-- ngx_stream_module.so

nginx项目:

![[WechatIMG46.jpg]]

编辑nginx服务器配置文件:

反向代理:

1.安装httpd来辅助实现。
2.修改配置文件 更改端口不是80 /etc/httpd/conf
3.修改nginx配置文件 /etc/nginx/nginx.conf
主页文件位置:/usr/share/nginx/html
4.server {
listen 81;
location / {
proxy_pass http://127.0.0.1:88;
}
}
5.访问81端口会转发到88的httpd端口。

负载均衡:upstream 轮询 权重轮询

1.修改配置文件
upstream zhaoxi {
server 127.0.0.1:80 weight=1;
server 127.0.0.1:81 max_fails=3 fail_timeout=10s weight=5;
}
2.反向代理配置文件改为
server {
listen 82;
location / {
proxy_pass http://zhaoxi;
}
}

动静分离

1.修改反向代理配置文件
server {
listen 82;
location / {
proxy_pass http://zhaoxi;
}
location ~* \.(html)$ {
root /usr/share/nginx/html;
expires 7d;
}
}

2.当输入/index.html时会自动跳转到静态页面;

多端口虚拟主机

1.配置文件:

server{
        listen 81;
        location / {
          root /www/81/;
          index index.html;
        }
}

server{
        listen 82;
        location / {
        root /www/82/;
        index index.html;
        }
}

2.在总配置文件里有个include 所以这个配置文件直接在conf.d/port.conf 也是一样的。

多ip虚拟主机

多域名虚拟主机

日志

  • access_log 记录用户访问日志
  • error_log 记录错误日志

实现隐藏Nginx软件名及版本号

curl -I localhost

vim /etc/nginx/nginx.conf
http模块中添加:server_tokens off;

nginx -s reload

修改worker进程的最大文件打开数

修改Nginx配置文件

worker_rlimit_nofile 65535; events { worker_connections 65535; }
`worker_rlimit_nofile` 设置工作进程的最大文件描述符数。`worker_connections` 设置每个worker进程的最大连接数。

修改系统级文件描述符限制

编辑系统安全限制配置文件:

vim /etc/security/limits.conf

G
* soft nofile 65535 
* hard nofile 65535
编辑系统启动文件:

vim /etc/systemd/system.conf
vim /etc/systemd/user.conf

DefaultLimitNOFILE=65535

重新加载系统守护进程

systemctl daemon-reload
重启nginx

验证修改

sudo cat /proc/$(pidof nginx | awk '{print $1}')/limits | grep 'Max open files'



Max open files            65535                65535                files

对于动态网页进行gzip压缩优化

在http模块中添加

# 启用gzip压缩
    gzip on;

    # 设置最低启用压缩的文件大小
    gzip_min_length 0;

    # 设置压缩缓冲区大小
    gzip_buffers 4 16k;

    # 设置压缩级别(1-9,1为最快,9为最小)
    gzip_comp_level 5;

    # 设置需要压缩的文件类型
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # 设置对Proxy请求和客户端请求启用gzip压缩
    gzip_proxied any;

    # 启用Vary: Accept-Encoding头
    gzip_vary on;

测试

curl -I -H 'Accept-Encoding: gzip' localhost
![[Pasted image 20240615163809.png]]

expires过期缓存优化

# 在需要设置缓存过期时间的地方(比如在 `server {}` 或 `location {}` 块内),添加如下指令:
location / {
    expires 1y;  # 设置缓存过期时间为1年
}

这里的 `1y` 表示1年,你也可以使用其他单位如 `30d` 表示30天、 `7h` 表示7小时等。选择合适的过期时间取决于你的网站内容更新频率和缓存策略。
nginx -s reload

测试

![[Pasted image 20240615165357.png]]

防盗链

  • 直接在conf.d下写了 方便管理
# /etc/nginx/conf.d/anti-hotlinking.conf

server {
    listen 80;
    server_name zhaoxi.com;  # 将example.com替换为你的域名

    location / {
        root /usr/share/nginx/html;  # 你的站点根目录
        index index.html index.htm;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        valid_referers none blocked example.com *.example.com;

        if ($invalid_referer) {
            return 403;
        }

        # Optional: add a fallback image for blocked requests
        # rewrite ^/.*\.(jpg|jpeg|png|gif)$ /images/fallback.png break;
    }
}

解释配置:

在这个配置中,valid_referers 指令定义了有效的 Referer 列表:

  • none: 允许没有 Referer 头的请求。
  • blocked: 允许被屏蔽的 Referer 头的请求。
  • example.com *.example.com: 允许来自 example.com 及其所有子域名的请求。

$invalid_referer变量

Nginx 会根据 valid_referers 指令配置的规则检查每个请求的 Referer 头,并设置 $invalid_referer 变量:

  • 如果 Referer 头不在允许的列表中,$invalid_referer 的值为 1
  • 如果 Referer 头在允许的列表中,$invalid_referer 的值为 0

判断逻辑

if ($invalid_referer) 条件中:

  • 如果 $invalid_referer1,表示请求的 Referer 头无效,执行 if 块内的代码,即返回 403 禁止访问。
  • 如果 $invalid_referer0,表示请求的 Referer 头有效,继续处理请求。

流程总结

  1. 用户请求资源(如图片、CSS、JS)。
  2. Nginx 检查请求头中的 Referer 头。
  3. 根据 valid_referers 指令配置的规则,Nginx 设置 $invalid_referer 变量。
  4. 如果 $invalid_referer1(无效),Nginx 返回 403 禁止访问。
  5. 如果 $invalid_referer0(有效),Nginx 继续处理请求,返回资源。

通过这种方式,Nginx 可以有效地控制哪些请求被允许访问资源,防止其他网站盗链你的资源。

禁止非法域名解析

conf.d

server {
    listen 80 default_server;
    server_name _;  # 捕捉所有其他域名

    return 444;  # 返回一个关闭的连接,不发送任何头部信息
}
  • 比如在自己本机添加hosts 强制使用域名访问 就会返回444

禁止通过IP地址访问网站

  • 跟上面一样 server_name 不设置ip就行

[[nginx高级篇]]