原文:https://www.cnblogs.com/CraryPrimitiveMan/p/6568032.html
全局變量
$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 : 客戶端請求的動作,通常為GET
或POST
$remote_addr : 客戶端的IP
地址
$remote_port : 客戶端的端口
$remote_user : 已經經過Auth Basic Module
驗證的用戶名
$request_filename : 當前請求的文件路徑,由root
或alias
指令與URI
請求生成
$scheme : HTTP
方法(如http
,https
)
$server_protocol : 請求使用的協議,通常是HTTP/1.0
或HTTP/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:http://www.qq.com:8080/a/b/c.php
$document_uri:/a/b/c.php
$document_root:/var/www/html
$request_filename:/var/www/html/a/b/c.php
主機名(server_name)匹配
從上到下的優先級為從高到低
- 明確的
server_name
名稱,如www.qq.com
- 前綴通配符,如
*.qq.com
或. qq.com
- 后綴通配符,如
www.qq.*
- 正則表達式,如
~[a-z]+\.qq\.com
Location查找規則
從上到下的優先級為從高到低
- 等號類型,精確匹配,如
location = / {}
^~
類型,前綴匹配,不支持正則,如location ^~ /user {}
~
和~*
類型,正則匹配,~
區分大小寫,~*
不區分大小寫,如location ~ ^/user {}
- 常規字符串匹配類型,如
location / {}
或location /user {}
Try_files規則
try_files $uri $uri/ /index.php
假設請求為http://www.qq.com/test
,則$uri
為test
- 查找
/$root/test
文件 - 查找
/$root/test/
目錄 - 發起
/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
去做處理。