4-Nginx配置小結如request_filename


原文: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 : 客戶端請求的動作,通常為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: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)匹配

從上到下的優先級為從高到低

  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去做處理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM