可以支持高達 50,000 個併發鏈接數的響應,感謝Nginx爲咱們選擇了 epoll and kqueue做爲開發模型。php
1.1 執行安裝
css
worker_processes指明瞭nginx要開啓的進程數。據官方說法,通常開一個就夠了,多開幾個,可以下降機器io帶來的影響。據實踐代表。nginx的這個參數在普通狀況下開4個或8個就可以了。再往上開的話優化不太大。據還有一種說法是,nginx開啓太多的進程,會影響主進程調度,因此佔用的cpu會增高。
html
00:01:32 nginx: worker process
node
00:01:32 nginx: worker process
linux
00:01:23 nginx: worker process
android
00:01:32 nginx: worker process
nginx
00:01:32 nginx: worker process
git
00:01:33 nginx: worker process
web
00:01:24 nginx: worker process
vim
00:01:25 nginx: worker process
00:01:26 nginx: worker process
nginx不正確url作編碼。所以請求爲/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。
~ 開頭表示區分大寫和小寫的正則匹配
~* 開頭表示不區分大寫和小寫的正則匹配
!~和!~*分別爲區分大寫和小寫不匹配及不區分大寫和小寫不匹配 的正則
/ 通用匹配,不論什麼請求都會匹配到。
多個location配置的狀況下匹配順序爲(參考資料而來,還未實際驗證。試試就知道了。沒必要拘泥,僅供參考):
首先匹配 =。其次匹配^~, 其次是按文件中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,中止匹配。按當前匹配規則處理請求。
樣例,有例如如下匹配規則:
location = / {
#規則A
}
location = /login {
#規則B
}
location ^~ /static/ {
#規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
#規則D
}
location ~* \.png$ {
#規則E
}
location !~ \.xhtml$ {
#規則F
}
location !~* \.xhtml$ {
#規則G
}
location / {
#規則H
}
那麼產生的效果例如如下:
訪問根文件夾/, 比方http://localhost/ 將匹配規則A
訪問 http://localhost/login 將匹配規則B,http://localhost/register 則匹配規則H
訪問 http://localhost/static/a.html 將匹配規則C
訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E。但是規則D順序優先,規則E不起做用,而 http://localhost/static/c.png 則優先匹配到規則C
訪問 http://localhost/a.PNG 則匹配規則E,而不會匹配規則D。因爲規則E不區分大寫和小寫。
訪問 http://localhost/a.xhtml 不會匹配規則F和規則G。http://localhost/a.XHTML不會匹配規則G,因爲不區分大寫和小寫。規則F,規則G屬於排除法,符合匹配規則但是不會匹配到。因此想一想看實際應用中哪裏會用到。
訪問 http://localhost/category/id/1111 則終於匹配到規則H,因爲以上規則都不匹配,這個時候應該是nginx轉發請求給後端應用服務器,比方FastCGI(php),tomcat(jsp)。nginx做爲方向代理服務器存在。
因此實際使用中。我的認爲至少有三個匹配規則定義,例如如下:
#直接匹配站點根,經過域名訪問站點首頁比較頻繁,使用這個會加速處理,官網如是說。
#這裏是直接轉發給後端應用服務器了。也可以是一個靜態首頁
# 第一個必選規則
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二個必選規則是處理靜態文件請求,這是nginx做爲http服務器的強項
# 有兩種配置模式,文件夾匹配或後綴匹配,任選其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三個規則就是通用規則。用來轉發動態請求到後端應用服務器
#非靜態文件請求就默認是動態請求。本身依據實際把握
#畢竟眼下的一些框架的流行。帶.php,.jsp後綴的狀況很是少了
location / {
proxy_pass http://tomcat:8080/
}
2.3 ReWrite語法
last – 基本上都用這個Flag。
break – 停止Rewirte,不在繼續匹配
redirect – 返回暫時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態301
一、如下是可以用來推斷的表達式:
-f和!-f用來推斷是否存在文件
-d和!-d用來推斷是否存在文件夾
-e和!-e用來推斷是否存在文件或文件夾
-x和!-x用來推斷文件是否可執行
二、如下是可以用做推斷的全局變量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
2.4 Redirect語法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ 「^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
2.5 防盜鏈
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
2.6 依據文件類型設置過時時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
2.7 禁止訪問某個文件夾
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
一些可用的全局變量:
$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
2.8 Nginx靜態文件(css,js,jpg等等web靜態資源)
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
open_file_cache max=10000 inactive=60s;
location /group1/M00 {
root /data/fastdfs/data;
ngx_fastdfs_module;
}
location /css {
root plocc_static;
include gzip.conf;
}
location /common {
root plocc_static;
include gzip.conf;
}
2.9 nginx 轉發project的日誌文件
去nginx.conf配置文件中面去看訪問日誌,例如如下:
vim nginx.conf
location ~* ^/mobileWeb/.*$ {
include deny.conf;
proxy_pass http://mobilewebbackend;
include proxy.conf;
error_log logs/mobileweb_error.log error;
access_log logs/mobileweb_access.log main;
include gzip.conf;
}
再去logs文件夾查看日誌文件。例如如下:
[root@xx logs]# ll /usr/local/nginx/logs/mobileweb*
-rw-r--r--. 1 root root 10946 7月 18 10:36 /usr/local/nginx/logs/mobileweb_access.log
-rw-r--r--. 1 root root 1628 7月 18 10:36 /usr/local/nginx/logs/mobileweb_error.log
3 加入啓動服務
[y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]#
[root@localhost ssl]# openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for ca.key:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Jun 18 04:10:40 2014 GMT
Not After : Jun 18 04:10:40 2015 GMT
Subject:
countryName = cn
stateOrProvinceName = shanghai
organizationName = baolong
organizationalUnitName = business
commonName = ops
emailAddress = mch@gongsilong.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
E2:64:97:DC:A6:2B:85:53:5F:6C:5C:8D:1F:EB:59:C8:2C:66:C5:10
X509v3 Authority Key Identifier:
keyid:4E:F5:29:7F:6B:AD:11:EF:FC:44:CC:76:1D:B0:B9:F7:4B:9D:CB:93
Certificate is to be certified until Jun 18 04:10:40 2015 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]#
[PS]:附帶功能:
另外,這個certificate是BASE64形式的,要轉成PKCS12才幹裝到IE,/NETSCAPE上.轉換例如如下:
雙擊安裝便可
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
這個是ISO 需要的證書格式
openssl x509 -in client.crt -out client.cer
這個是android 需要的證書格式。
[root@mail ssl]# openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx
Enter pass phrase for client.key: //客戶端私鑰password
Enter Export Password: //pfx文件導入要求的password
Verifying - Enter Export Password:
[root@localhost conf]# service nginx stop
stop nginx
Enter PEM pass phrase:
phrase is too short, needs to be at least 4 chars
Enter PEM pass phrase:
phrase is too short, needs to be at least 4 chars
Enter PEM pass phrase:
nginx啓動SSL默認不輸入password
假設nginx配置了SSL,在每次啓動nginx的時候都會需要你手動輸入證書的password,假設不想輸入,可以
cp server.key server.key.orig
openssl rsa -in server.key.orig -out server.key
這樣啓動nginx的時候就不需要輸入password了。
[root@localhost ssl]# cp server.key server.key.orig
[root@localhost ssl]# openssl rsa -in server.key.orig -out server.key
Enter pass phrase for server.key.orig:
unable to load Private Key
20487:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:325:
20487:error:0906A065:PEM routines:PEM_do_header:bad decrypt:pem_lib.c:425:
[root@localhost ssl]#
這裏奇怪,一開始通只是,但是過了15分鐘後,在執行一遍,輸入password,又經過了,例如如下所看到的:
[root@localhost ssl]# openssl rsa -in server.key.orig -out server.key
Enter pass phrase for server.key.orig:
writing RSA key
[root@localhost ssl]#
固然也可以保留password。改用expect的方式,這個可以參考expect本身主動登陸SSH的方法。下次有時間再整理貼上來
5 靜態文件地址映射 nginx
location = userWeb/userCenter/findConsultList.htm {
rewrite ^.*$ http://xx.gongsilong.com/xx/xx/findConsultList.htm;
}
# add by tim begin ...
location ~* ^/svn/(.*) {
rewrite ^.*$ https://192.123.11.12/$1;
}
# add by tim end .. 來源地址:http://blog.itpub.net/26230597/abstract/1/