前兩天區聽了一堂Nginx的課,而後翻了一下本身以前的Nginx的筆記,作了一個簡單的小結。php
$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
相同html
假設請求爲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.phplaravel
從上到下的優先級爲從高到低正則表達式
server_name
名稱,如www.qq.com
*.qq.com
或. qq.com
www.qq.*
~[a-z]+\.qq\.com
從上到下的優先級爲從高到低api
location = / {}
^~
類型,前綴匹配,不支持正則,如location ^~ /user {}
~
和~*
類型,正則匹配,~
區分大小寫,~*
不區分大小寫,如location ~ ^/user {}
location / {}
或location /user {}
try_files $uri $uri/ /index.php
假設請求爲http://www.qq.com/test
,則$uri
爲test
服務器
/$root/test
文件/$root/test/
目錄/index.php
的內部「子請求」rewrite ^/images/(.*).(png|jpg|gif)$ /images?name=$1.$4 last;cookie
上面的rewrite
規則會將文件名改寫到參數中負載均衡
last : 至關於Apache
的[L]標記,表示完成rewrite
break : 中止執行當前虛擬主機的後續rewrite
指令集
redirect : 返回302臨時重定向,地址欄會顯示跳轉後的地址
permanent : 返回301永久重定向,地址欄會顯示跳轉後的地址框架
例子以下dom
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
去作處理。