博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx配置小结
阅读量:6874 次
发布时间:2019-06-26

本文共 3386 字,大约阅读时间需要 11 分钟。

前两天区听了一堂Nginx的课,然后翻了一下自己之前的Nginx的笔记,做了一个简单的小结。

全局变量

$args : 这个变量等于请求行中的参数,同$query_string

$content_length : 请求头中的Content-length字段
$content_type : 请求头中的Content-Type字段
$document_root : 当前请求在root指令中指定的值
$host : 请求主机头字段,否则为服务器名称
$http_user_agent : 客户端agent信息
$http_cookie : 客户端cookie信息
$limit_rate : 这个变量可以限制连接速率
$request_method : 客户端请求的动作,通常为GETPOST
$remote_addr : 客户端的IP地址
$remote_port : 客户端的端口
$remote_user : 已经经过Auth Basic Module验证的用户名
$request_filename : 当前请求的文件路径,由rootalias指令与URI请求生成
$scheme : HTTP方法(如httphttps
$server_protocol : 请求使用的协议,通常是HTTP/1.0HTTP/1.1
$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值
$server_name : 服务器名称
$server_port : 请求到达服务器的端口号
$request_uri : 包含请求参数的原始URI,不包含主机名,如/foo/bar.php?arg=baz
$uri : 不带请求参数的当前URI$uri不包含主机名,如/foo/bar.html
$document_uri : 与$uri相同

假设请求为http://www.qq.com:8080/a/b/c.php,则

$host:www.qq.com
$server_port:8080
$request_uri:
$document_uri:/a/b/c.php
$document_root:/var/www/html
$request_filename:/var/www/html/a/b/c.php

主机名(server_name)匹配

从上到下的优先级为从高到低

  1. 明确的server_name名称,如www.qq.com
  2. 前缀通配符,如*.qq.com. qq.com
  3. 后缀通配符,如www.qq.*
  4. 正则表达式,如~[a-z]+\.qq\.com

Location查找规则

从上到下的优先级为从高到低

  1. 等号类型,精确匹配,如location = / {}
  2. ^~类型,前缀匹配,不支持正则,如location ^~ /user {}
  3. ~~*类型,正则匹配,~区分大小写,~*不区分大小写,如location ~ ^/user {}
  4. 常规字符串匹配类型,如location / {}location /user {}

Try_files规则

try_files $uri $uri/ /index.php

假设请求为http://www.qq.com/test,则$uritest

  1. 查找/$root/test文件
  2. 查找/$root/test/目录
  3. 发起/index.php的内部“子请求”

Rewrite规则

rewrite ^/images/(.*).(png|jpg|gif)$ /images?name=$1.$4 last;

上面的rewrite规则会将文件名改写到参数中

last : 相当于Apache的[L]标记,表示完成rewrite

break : 停止执行当前虚拟主机的后续rewrite指令集
redirect : 返回302临时重定向,地址栏会显示跳转后的地址
permanent : 返回301永久重定向,地址栏会显示跳转后的地址

负载均衡

例子如下

upstream backend1 {    server backend1.qq.com weight=5;    server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;    server unix:/tmp/backend3 backup;}upstream backend2 {    ip_hash;    server backend1.qq.com;    server backend2.qq.com;    server backend3.qq.com down;    server backend4.qq.com;}server {    location / {        proxy_pass http://backend1;    }    location /api {        proxy_pass http://backend2;    }}

查看一个实例

下面是一个 laravel框架Nginx配置的例子,听过这堂课终于了解了下面的原理。

server {    listen 80 default_server;    listen [::]:80 default_server ipv6only=on;    # 设定网站根目录    root /var/www/laravel/public;    # 网站默认首页    index index.php index.html index.htm;    # 服务器名称,server_domain_or_IP 请替换为自己设置的名称或者 IP 地址    server_name server_domain_or_IP;    # 修改为 Laravel 转发规则,否则PHP无法获取$_GET信息,提示404错误    location / {        try_files $uri $uri/ /index.php?$query_string;    }    # PHP 支持    location ~ \.php$ {        try_files $uri /index.php =404;        fastcgi_split_path_info ^(.+\.php)(/.+)$;        fastcgi_pass unix:/var/run/php5-fpm.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        include fastcgi_params;    }}

我们主要关注两个location,假设地址是http://www.qq.com/user/info,会匹配到如下location

location / {        try_files $uri $uri/ /index.php?$query_string;    }

由于$uri$uri/是不存在的,所以会走/index.php?$query_string,这时候会发起一个内部“子请求”,“子请求”会重新匹配location,然后匹配到如下location

location ~ \.php$ {        try_files $uri /index.php =404;        fastcgi_split_path_info ^(.+\.php)(/.+)$;        fastcgi_pass unix:/var/run/php5-fpm.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        include fastcgi_params;    }

这样请求就会发送到fastcgi去做处理。

转载地址:http://fgsfl.baihongyu.com/

你可能感兴趣的文章
把可视化从业者的生存现状可视化出来:他们是谁?
查看>>
Flutter Web - 目标全平台开发的Flutter再下一城!
查看>>
RAID-10 阵列的创建(软)
查看>>
小白学爬虫-批量部署Splash负载集群
查看>>
dubbo源码分析-服务端发布流程-笔记
查看>>
小菜鸡进阶之路-First week
查看>>
Create a RHEL6 PXE Installation Server
查看>>
【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!
查看>>
桌面支持--Office2013没有Office Picture Manage怎么安装
查看>>
chmod修改文件权限失败
查看>>
数据结构与算法-->互为素数
查看>>
Linux系统学习方法——写给小白
查看>>
Nginx服务器报500 Internal Server Error错误
查看>>
链表的游标实现
查看>>
别伤了虚拟桌面管理员的"心"
查看>>
yum安装lamp
查看>>
[Unity 3D] Unity 3D 性能优化 (一)
查看>>
Disabling OOM killer on Ubuntu 14.04
查看>>
VBS备份脚本
查看>>
CentOS 6.5 自动安装镜像
查看>>