Nginx

nginx [engine x] 是一个 HTTP 和反向代理服务器、邮件代理服务器和通用 TCP/UDP 代理服务器,最初由 Igor Sysoev 编写。 长期以来,它一直在许多负载较大的俄罗斯网站上运行,包括 Yandex、Mail.Ru、VK 和 Rambler。 据 Netcraft 称,2024 年 2 月,nginx 为 20.74% 最繁忙的网站提供服务或代理。

Nginx

  • 控制命令
    • nginx -t #测试配置是否正确
    • nginx -s reload #重新加载配置到内存,重启nginx
    • nginx -s reopen #重新打开日志
    • nginx -s stop #立即停止
    • nginx -s quit #优雅停止(等待该响应的响应完成后才停止)
  • 全局配置
# 配置worker的数目
# worker的数量 = cpu的数量*cpu的核数
# 没必要设置太大,因为这些worker会去抢夺cpu
worker_processes 1;
# 配置1个worker进程最多能接受多少个连接
events {
    worker_connections 1024;
}
http { #配置虚拟主机
    server {
        listen 80; # 监听端口号
        server_name localhost; # 虚拟主机的域名
        location / { # 虚拟主机的站点目录,/表示就是nginx服务器下的html目录
            root html;
            index index.php index.html index.htm; # 默认文件,优先级从前到后
        }
    }
    server {
        ......
    }
}

#示例1:基于域名的虚拟主机
server {
    listen 80; # 监听80端口
    server_name itbool.com; # 监听域名,如果有多个域名,用空格隔开
    location / {
        root html; # 网站根目录定位
        index index.php index.html index.htm; # 默认索引页
    }
}
#示例2:基于端口号的虚拟主机配置
server {
    listen 8080; # 监听8080端口
    server_name blog.itbool.com; # 监听域名,如果有多个域名,用空格隔开
    location / {
        root html; # 网站根目录定位
        index index.php index.html index.htm; # 默认索引页
    }
}
  • 日志管理
#access_log logs/host.access.log main;

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

$remote_addr 与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user 用来记录客户端用户名称;
$time_local 用来记录访问时间与时区;
$request 用来记录请求的url与http协议;
$status 用来记录请求状态,成功是200;
$body_bytes_sent 记录发送给客户端文件主体内容大小;
$http_referer 用来记录从那个页面链接访问过来的;
$http_user_agent 记录客户端浏览器的相关信息;

log_format mystyle '$remote_addr - $remote_user [$time_local] '
'$status "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

server {
    listen 80; # 监听8080端口
    server_name zixue.it; # 监听域名,如果有多个域名,用空格隔开
    access_log logs/zixue.it.access.log mystyle;
    location / {
        root html; # 网站根目录定位
        index index.php index.html index.htm; # 默认索引页
    }
}
  • nginx与php的整合
location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
    include fastcgi_params;
}
#1: 遇到请求 html/ 根目录下的 .php 结尾的脚本时,将请求转发到监听 127.0.0.1:9000 端口的php进程.
#2: 并告诉php进程,当前要解释的脚本路径是 $DOCUMENT_ROOT$fastcgi_script_name
注意: 所以你一定要指定正确要解释的php脚本的正确路径,否则php进程会找不到哦
  • rewrite规则
//网站根目录 ./html/下,新建脚本 user.php
<?php
$info = [
    '1' => '你是1号用户',
    '2' => '你是2号用户',
    '3' => '你是3号用户',
];
$uid = $_GET['id'];
echo $info[$uid];
#编辑 ./conf/nginx.conf 配置文件
location / {
    root html;
    index index.html index.htm;
    rewrite /user-(\w+)\.html /user.php?id=$1;
}
# 访问 user-2.html
# 这种通过url重写的方法,也被SEO界称之伪静态

#pathinfo支持
# 典型配置
location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
    include fastcgi_params;
}
# 修改第1,6行,支持pathinfo
location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
    fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
    include fastcgi_params;
}

#try_files
try 试试
try_files $uri/ $uri /index.php?$query_string;
以 xx.com/goods/1 为例
是指,
先试试 有没有 /goods/1/ 这个目录
再试试 有没有 /goods/1 这个文件
最后试试 /index.php?goods/1 这个 URL
  • 反向代理
# 打开nginx的配置文件 ./conf/nginx.conf 在你的Server虚拟主机段中添加如下配置:
location ~ \.(jpg|gif|png)$ {
    proxy_pass IP:port;
}

# 代理服务器通过设置头信息字段,把用户 IP 传到后台服务器去
location ~ \.(jpg|jpeg|png|gif)$ {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass IP:port;
}
  • 负载均衡
# 编辑 ./conf/nginx.conf 文件(注意 upstream {} 配置信息必须在 Server {} 配置的外部,不是 Server {} 的里面)
# 集群服务器组
upstream itbool { # 服务器集群的组名
    server 192.168.3.110:80 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.3.111:80 weight=1 max_fails=2 fail_timeout=30s;
    #server 服务器的ip:端口号 权重 最大失败次数 最大连接时间
}
# 指定代理请求的集群组名
Server {
    location ~ \.(jpg|png|jpeg|gif)$ {
    proxy_pass http://itbool;
    }
}
# 1: weight的值越大,表明这台服务器办事效率高,老板喜欢,有事了,找他的概率大.
# 2: max_fails要说明的是,你找这台服务器办事,叫他2次如果还不理你,你不要对他报以希望了.
# 3: fail_timeout要说的是,给这台服务器一件小事让它办,30s还没办完,算了,不靠谱,不要等了,找其他人吧.

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注