任何一個工具都有它的靈魂所在,做爲一個PHP程序員,咱們可能僅僅使用了它的一小部分,這篇文章讓你更加了解Nginx,本章大多都是總結、翻譯、整理 ,但願你能夠知道nginx不只僅是PHP的附屬品,更宏觀的認識它的強大。php
內置變量存放在 ngx_http_core_module 模塊中,變量的命名方式和apache 服務器變量是一致的。總而言之,這些變量表明着客戶端請求頭的內容,例如$http_user_agent, $http_cookie, 等等。下面是nginx支持的全部內置變量html
變量名 | 註釋 |
---|---|
$arg_name | 請求中的的參數名,即「?」後面的arg_name=arg_value形式的arg_name |
$args | 請求中的參數值 |
$binary_remote_addr | 客戶端地址的二進制形式, 固定長度爲4個字節 |
$body_bytes_sent | 傳輸給客戶端的字節數,響應頭不計算在內;這個變量和Apache的mod_log_config模塊中的「%B」參數保持兼容 |
$bytes_sent | 傳輸給客戶端的字節數 (1.3.8, 1.2.5) |
$connection | TCP鏈接的序列號 (1.3.8, 1.2.5) |
$connection_requests | TCP鏈接當前的請求數量 (1.3.8, 1.2.5) |
$content_length | 「Content-Length」 請求頭字段 |
$content_type | 「Content-Type」 請求頭字段 |
$cookie_name | cookie名稱 |
$document_root | 當前請求的文檔根目錄或別名 |
$document_uri | 同 $uri |
$host | 優先級以下:HTTP請求行的主機名>」HOST」請求頭字段>符合請求的服務器名 |
$hostname | 主機名 |
$http_name | 匹配任意請求頭字段; 變量名中的後半部分「name」能夠替換成任意請求頭字段,如在配置文件中須要獲取http請求頭:「Accept-Language」,那麼將「-」替換爲下劃線,大寫字母替換爲小寫,形如:$http_accept_language便可。 |
$https | 若是開啓了SSL安全模式,值爲「on」,不然爲空字符串。 |
$is_args | 若是請求中有參數,值爲「?」,不然爲空字符串。 |
$limit_rate | 用於設置響應的速度限制,詳見 limit_rate。 |
$msec | 當前的Unix時間戳 (1.3.9, 1.2.6) |
$nginx_version | nginx版本 |
$pid | 工做進程的PID |
$pipe | 若是請求來自管道通訊,值爲「p」,不然爲「.」 (1.3.12, 1.2.7) |
$proxy_protocol_addr | 獲取代理訪問服務器的客戶端地址,若是是直接訪問,該值爲空字符串。(1.5.12) |
$proxy_protocol_port | 獲取代理訪問服務器的客戶端的端口,若是是直接訪問,該值爲空字符串(1.11.0)。 |
$query_string | 同 $args |
$realpath_root | 當前請求的文檔根目錄或別名的真實路徑,會將全部符號鏈接轉換爲真實路徑。 |
$remote_addr | 客戶端地址 |
$remote_port | 客戶端端口 |
$remote_user | 用於HTTP基礎認證服務的用戶名 |
$request | 表明客戶端的請求地址 |
$request_body | 客戶端的請求主體,此變量可在location中使用,將請求主體經過proxy_pass, fastcgi_pass, uwsgi_pass, 和 scgi_pass傳遞給下一級的代理服務器。 |
$request_body_file | 將客戶端請求主體保存在臨時文件中。文件處理結束後,此文件需刪除。若是須要之一開啓此功能,須要設置client_body_in_file_only。若是將次文件傳遞給後端的代理服務器,須要禁用request body,即設置proxy_pass_request_body off,fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off 。 |
$request_completion | 若是請求成功,值爲」OK」,若是請求未完成或者請求不是一個範圍請求的最後一部分,則爲空。 |
$request_filename | 當前鏈接請求的文件路徑,由root或alias指令與URI請求生成。 |
$request_id | 16隨機字節(十六進制)生成的惟一請求標識符(1.11.0) |
$request_length | 請求的長度 (包括請求的地址, http請求頭和請求主體) (1.3.12, 1.2.7) |
$request_method | HTTP請求方法,一般爲「GET」或「POST」 |
$request_time | 處理客戶端請求使用的時間 (1.3.9, 1.2.6); 從讀取客戶端的第一個字節開始計時。 |
$request_uri | 這個變量等於包含一些客戶端請求參數的原始URI,它沒法修改,請查看$uri更改或重寫URI,不包含主機名,例如:」/cnphp/test.php?arg=freemouse」。 |
$scheme | 請求使用的Web協議, 「http」 或 「https」 |
$sent_http_name | 能夠設置任意http響應頭字段; 變量名中的後半部分「name」能夠替換成任意響應頭字段,如須要設置響應頭Content-length,那麼將「-」替換爲下劃線,大寫字母替換爲小寫,形如:$sent_http_content_length 4096便可。 |
$server_addr | 服務器端地址,須要注意的是:爲了不訪問linux系統內核,應將ip地址提早設置在配置文件中。 |
$server_name | 服務器名,www.nginx.org |
$server_port | port of the server which accepted a request |
$server_protocol | 服務器的HTTP版本, 一般爲 「HTTP/1.0」 或 「HTTP/1.1」 , 「HTTP/2.0」 |
$status | HTTP響應代碼 (1.3.2, 1.2.2) |
$tcpinfo_rtt、$tcpinfo_rttvar、$tcpinfo_snd_cwnd、$tcpinfo_rcv_space | 客戶端TCP鏈接的具體信息 |
$time_iso8601 | 服務器時間的ISO 8610格式 (1.3.12, 1.2.7) |
$time_local | 服務器時間(LOG Format 格式) (1.3.12, 1.2.7) |
$uri | 請求中的當前URI(不帶請求參數,參數位於$args),能夠不一樣於瀏覽器傳遞的$request_uri的值,它能夠經過內部重定向,或者使用index指令進行修改,$uri不包含主機名,如」/foo/bar.html」。 |
nginx if 不容許嵌套,變量與括號之間都須要有空格,不然nginx認爲是一條命令linux
符號 | 註釋 |
---|---|
= | 比較運算符,並不是 "==" |
&& | 邏輯與 |
or | 邏輯或 |
nginx 將每一個功能都分紅不一樣的模塊加載,具備很強的擴展性,與php的ext同樣。
下面列出的都是經常使用到的模塊和內置的模塊nginx
模塊名 | 註釋 |
---|---|
ngx_http_core_module | nginx核心模塊,例如location, error_page和內置變量等等 |
ngx_http_access_module | 容許/限制客戶端的訪問 |
ngx_http_fastcgi_module | 這個很少說,若是你是PHP程序員必定知道 |
ngx_http_grpc_module | 將請求發送到grpc服務器 |
ngx_http_gzip_module | 壓縮技術 |
ngx_http_proxy_module | 反向代理的模塊 |
ngx_http_log_module | 日誌的模塊 |
ngx_http_rewrite_module | 重寫的模塊 |
ngx_http_ssl_module | 配置https的模塊 |
nginx支持如下命令行參數程序員
參數 | 註釋 |
---|---|
-? -h | 打開幫助信息 |
-c file | 使用指定配置文件啓動nginx. |
-g directives | 設置配置文件外的全局指令 |
-p prefix | 設置前綴路徑(默認是:/usr/local/Cellar/nginx) |
-q suppress | 在檢測配置文件期間屏蔽非錯誤信息 |
-s signal | 給一個 nginx 主進程發送信號:stop(中止), quit(退出), reopen(重啓), reload(從新加載配置文件) |
stop | 馬上中止nginx運行 |
quit | 優雅的關閉nginx |
reload | 重啓 |
reopen | 打開日誌文件 |
-t | 檢測配置文件是否有語法錯誤,而後退出 |
-T | 與-t相同 |
-v | 顯示版本信息並退出 |
-V | 顯示版本和配置選項信息,而後退出 |
server { listen 443; server_name nginx.org; ssl on; root /var/www/nginx.org; index index.html index.htm; ssl_certificate cert/nginx.pem; ssl_certificate_key cert/nginx.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { index index.html index.htm; } }
server { listen 80; server_name www.baidu.cim; access_log logs/nginx.access.log main; error_log logs/nginx.error.log; root html; index index.html index.htm index.php; location / { proxy_pass http://nginx.org; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; } }
upstream server { server 127.0.0.1:8080 weight=3; server 127.1.1.1:8081; } server { listen 80; server_name www.baidu.cim; access_log logs/nginx.access.log main; error_log logs/nginx.error.log; root html; index index.html index.htm index.php; location / { proxy_pass http://server; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; } }
感謝你看到這裏,咱們不是代碼的搬運工,要理解其內在也是一名優秀的程序員。有什麼疑問能夠在評論區留言,謝謝 🙏apache