A. nginx 怎麼讀
nginx [engine x] is a HTTP server and mail proxy server 只是一個搜索
引擎
B. 怎麼用nginx輸出helloworld
nginx模塊的處理流程:
a.客戶端發送http請求道nginx伺服器
b.nginx基於配置文件中的位置選擇一個合適的處理模塊
c.負載均衡模塊選擇一台後端伺服器(反向代理情況下)
d.處理模塊進行處理並把輸出緩沖放到第一個過濾模塊上
e.第一個過濾模塊處理後輸出給第二個過濾模塊
f.然後第二個過濾模塊又到第三個過濾模塊
g.第N個過濾模塊。。。
h.發處理結果發給客戶端
2.nginx模塊編寫
a、創建模塊文件夾
<span style="font-size:16px;">mkdir -p /opt/nginx_hello_world
cd /op/nginx_hello_word</span>
b、創建模塊配置文件
<span style="font-size:16px;">vi /opt/nginx_hello_word/config</span>
寫入如下內容:
<span style="font-size:16px;">ngx_addon_name=ngx_http_hello_world_mole
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_mole"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_mole.c"
CORE_LIBS="$CORE_LIBS -lpcre"</span>
c、創建模塊主文件
<span style="font-size:16px;">vi /opt/nginx_hello_world/ngx_http_hello_world_mole.c</span>
寫入如下內容:
<span style="font-size:16px;">#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
/* Commands */
static ngx_command_t ngx_http_hello_world_commands[] = {
{ ngx_string("hello_world"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hello_world,
0,
0,
NULL },
ngx_null_command
};
static u_char ngx_hello_world[] = "hello world";
static ngx_http_mole_t ngx_http_hello_world_mole_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
/* hook */
ngx_mole_t ngx_http_hello_world_mole = {
NGX_MODULE_V1,
&ngx_http_hello_world_mole_ctx, /* mole context */
ngx_http_hello_world_commands, /* mole directives */
NGX_HTTP_MODULE, /* mole type */
NULL, /* init master */
NULL, /* init mole */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
/* Http Output Buffer */
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = b;
out.next = NULL;
b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(ngx_hello_world);
ngx_http_send_header(r);
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf ;
/* register hanlder */
clcf = ngx_http_conf_get_mole_loc_conf(cf, ngx_http_core_mole);
clcf->handler = ngx_http_hello_world_handler;
return NGX_CONF_OK;
}
</span>
d、下載nginx源碼包,我下載的是nginx-1.0.13.tar.gz
這里注意在編譯helloworld模塊前首先確認,nginx是否可以獨立編譯成功,是否安裝了所需的所有模塊
與helloworld模塊一起編譯nginx:
<span style="font-size:16px;">./configure --prefix=/usr/local/nginx --add-mole=/opt/nginx_hello_world/
make
make install</span>
e、配置nginx.conf
<span style="font-size:16px;">location= /hello {
hello_world;
}</span>
f、啟動nginx,訪問http://localhost/hello ,可以看到編寫的helloworld模塊輸出的文字。
3.hello world模塊分析
a.ngx_command_t函數用於定義包含模塊指令的靜態數組ngx_http_hello_world_commands
<span style="font-size:16px;">static ngx_command_t ngx_http_hello_world_commands[] = {
{ ngx_string("hello_world"), //設置指令名稱字元串,注意不能包含空格,數據類型ngx_str_t之後會詳細講解。
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,這里表示:location部分合法,並且指令沒有參數。
ngx_http_hello_world,//回調函數,三個參數(ngx_conf_t *cf,ngx_command_t *cmd, void *conf)
0,//後面的參數有待發掘,我還沒有用到
0,
NULL },
ngx_null_command
};</span>
b.static u_char ngx_hello_world[] ="hello world" 則是輸出到屏幕的字元串。
c.ngx_http_mole_t用來定義結構體ngx_http_hello_world_mole_ctx:
<span style="font-size:16px;">static ngx_http_mole_t ngx_http_hello_world_mole_ctx = {
NULL, /* 讀入配置前調用*/
NULL, /* 讀入配置後調用*/
NULL, /* 創建全局部分配置時調用 */
NULL, /* 初始化全局部分的配置時調用*/
NULL, /* 創建虛擬主機部分的配置時調用*/
NULL, /* 與全局部分配置合並時調用 */
NULL, /* 創建位置部分的配置時調用 */
NULL /* 與主機部分配置合並時調用*/
};</span>
d.ngx_mole_t定義結構體ngx_http_hello_world_mole
<span style="font-size:16px;">ngx_mole_t ngx_http_hello_world_mole = {
NGX_MODULE_V1,
&ngx_http_hello_world_mole_ctx, /* mole context */
ngx_http_hello_world_commands, /* mole directives */
NGX_HTTP_MODULE, /* mole type */
NULL, /* init master */
NULL, /* init mole */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};</span>
他包含有模塊的主要內容和指令的執行部分,下一節會詳細講解。
e.處理函數,ngx_http_hello_world_handler,也是hello world 模塊的核心部分。
<span style="font-size:16px;">static ngx_int_t
ngx_http_hello_world_handler(ngx_http_request_t *r)//ngx_http_request_t *r 可以訪問到客戶端的頭部和不久要發送的回復頭部
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
/* Http Output Buffer */
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = b;
out.next = NULL;
b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(ngx_hello_world);
ngx_http_send_header(r);
return ngx_http_output_filter(r, &out);
}</span>
C. 怎麼讀Nginx
網路一下,你就知道怎麼發音了,多聽幾遍,多讀幾次,面試官一定能聽懂!
D. php怎麼讀取設置nginx緩存
nginx緩存nginx有兩種緩存機制:fastcgi_cache和proxy_cache下面我們來說說這兩種緩存機制的區別吧proxy_cache作用是緩存後端伺服器的內容,可能是任何內容,包括靜態的和動態的fastcgi_cache作用是緩存fastcgi生成的內容,很多情況是php生成的動php怎麼讀取設置nginx緩存
E. nginx 怎麼讀取正則表達式
為區分大小寫匹配
~* 為不區分大小寫匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
last 相當於Apache里的[L]標記,表示完成rewrite,呵呵這應該是最常用的
break 終止匹配, 不再匹配後面的規則
redirect 返回302臨時重定向 地址欄會顯示跳轉後的地址
permanent 返回301永久重定向 地址欄會顯示跳轉後的地址
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
1、if指令
所有的Nginx內置變數都可以通過if指令和正則表達式來進行匹配,並且根據匹配結果進行一些操作,如下:
if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; } if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) { set $id $1; }
使用符號~*和~模式匹配的正則表達式:
~為區分大小寫的匹配。
~*不區分大小寫的匹配(匹配firefox的正則同時匹配FireFox)。
!~和!~*意為「不匹配的」。
Nginx在很多模塊中都有內置的變數,常用的內置變數在HTTP核心模塊中,這些變數都可以使用正則表達式進行匹配。
2、可以通過正則表達式匹配的指令
location
查看維基:location
可能這個指令是我們平時使用正則匹配用的最多的指令:
location ~ .*\.php?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwsite/test.com/$fastcgi_script_name; include fcgi.conf; }
幾乎每個基於LEMP的主機都會有如上一段代碼。他的匹配規則類似於if指令,不過他多了三個標識符,^~、=、@。並且它沒有取反運算符!,這三個標識符的作用分別是:
^~ 標識符後面跟一個字元串。Nginx將在這個字元串匹配後停止進行正則表達式的匹配(location指令中正則表達式的匹配的結果優先使用), 如:location ^~ /images/,你希望對/images/這個目錄進行一些特別的操作,如增加expires頭,防盜鏈等,但是你又想把除了這個目錄的圖片外的所有圖 片只進行增加expires頭的操作,這個操作可能會用到另外一個location,例如:location ~* \.(gif|jpg|jpeg)$,這樣,如果有請求/images/1.jpg,nginx如何決定去進行哪個location中的操作呢?結果取決 於標識符^~,如果你這樣寫:location /images/,這樣nginx會將1.jpg匹配到location ~* \.(gif|jpg|jpeg)$這個location中,這並不是你需要的結果,而增加了^~這個標識符後,它在匹配了/images/這個字元串後 就停止搜索其它帶正則的location。
= 表示精確的查找地址,如location = /它只會匹配uri為/的請求,如果請求為/index.html,將查找另外的location,而不會匹配這個,當然可以寫兩個 location,location = /和location /,這樣/index.html將匹配到後者,如果你的站點對/的請求量較大,可以使用這個方法來加快請求的響應速度。
@ 表示為一個location進行命名,即自定義一個location,這個location不能被外界所訪問,只能用於Nginx產生的子請求,主要為error_page和try_files。
注意,這3個標識符後面不能跟正則表達式,雖然配置文件檢查會通過,而且沒有任何警告,但是他們並不會進行匹配。
綜上所述,location指令對於後面值的匹配順序為:
標識符「=」的location會最先進行匹配,如果請求uri匹配這個location,將對請求使用這個location的配置。
進行字元串匹配,如果匹配到的location有^~這個標識符,匹配停止返回這個location的配置。
按照配置文件中定義的順序進行正則表達式匹配。最早匹配的location將返回裡面的配置。
如果正則表達式能夠匹配到請求的uri,將使用這個正則對應的location,如果沒有,則使用第二條匹配的結果。
F. nginx怎樣讀取session
nginx是沒有session的。
session這東西是開發語言(php/asp/jsp)的一個功能,就是用戶的一個對話區。
而nginx是一個應用軟體,主要用來代理轉發網路的請求,沒有session的。
如果想解決nginx均衡所帶來的session問題,用redis或者memcache。
Nginx負載均衡一些基礎知識:
nginx 的 upstream目前支持 4 種方式的分配
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
2)、weight
指定輪詢幾率,weight和訪問比率成正比,用於後端伺服器性能不均的情況。
2)、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
3)、fair(第三方)
按後端伺服器的響應時間來分配請求,響應時間短的優先分配。
4)、url_hash(第三方)
配置:
在http節點里添加:
#定義負載均衡設備的 Ip及設備狀態
G. 如何在nginx中讀取POST上來的數據
1.框架搭建
1.1 將struts2中的jar文件導入到項目中
commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,freemarker-2.3.15.jar,ognl-2.7.3.jar
struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar
1.2 將struts.xml文件拷貝到項目的src目錄下
1.3 修改web.xml文件
添加:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
H. 如何解讀Nginx源碼
前提:
1、首先nginx是C語言編寫的,你必須知識要有C語言的編程基礎,否則很痛苦
2、了解web伺服器,反向代理的基本知識,以及HTTP協議,TCP/IP協議的基本知識
如果你已經有豐富的經驗,或者是大牛,那前面的前提就是廢話,可以略過。
看源碼准備:
1、找官網,找貢獻者的博客去了解NGINX是做什麼的,有什麼特性,性能,功能,架構等
2、下載源代碼,從分析main函數開始,大致了解啟動流程,初始化以及一些程序的啟動准備
3、建議找到request邏輯,分析下對請求的整個處理流程,不用很細,慢慢來,一口吃不了大胖子,有問題就先記上再說
4、根據分析request的經驗,拓展分析下nginx的模塊,處理鏈,以及封裝的數據結構如ngx_str_t,ngx_event_t等數據結構
5、到網上找個例子,自己動手去寫個模塊,或修改某個處理邏輯,你一定會遇到問題,這時你可以通過GDB等工具進行分析和調試,這樣加深了你的理解
6、動手寫代碼,看源碼,調試,重復這個過程。
其他
多在網路上找資源和志同道和的技術愛好者或牛人,多交流溝通。
堅持一年,你會有突飛猛進的成績。good luck
I. nginx怎麼發音
nginx=Engine X = 引擎X = 引擎愛克斯 (參考官方解釋以及網路術語的習慣可以參考以上發音)
J. 怎麼使用nginx的vhost
nginx為了實現反向代理的需求而增加了一個ngx_http_proxy_mole模塊。其中proxy_set_header指令就是該模塊需要讀取的配置文件。在這里,所有設置的值的含義和http請求同中的含義完全相同,除了Host外還有X-Forward-For。
Host的含義是表明請求的主機名,因為nginx作為反向代理使用,而如果後端真是的伺服器設置有類似防盜鏈或者根據http請求頭中的host欄位來進行路由或判斷功能的話,如果反向代理層的nginx不重寫請求頭中的host欄位,將會導致請求失敗【默認反向代理伺服器會向後端真實伺服器發送請求,並且請求頭中的host欄位應為proxy_pass指令設置的伺服器】。
同理,X_Forward_For欄位表示該條http請求是有誰發起的?如果反向代理伺服器不重寫該請求頭的話,那麼後端真實伺服器在處理時會認為所有的請求都來在反向代理伺服器,如果後端有防攻擊策略的話,那麼機器就被封掉了。因此,在配置用作反向代理的nginx中一般會增加兩條配置,修改http的請求頭:
proxy_set_header Host $http_host;
proxy_set_header X-Forward-For $remote_addr;
這里的$http_host和$remote_addr都是nginx的導出變數,可以再配置文件中直接使用。如果Host請求頭部沒有出現在請求頭中,則$http_host值為空,但是$host值為主域名。因此,一般而言,會用$host代替$http_host變數,從而避免http請求中丟失Host頭部的情況下Host不被重寫的失誤。