-------------------- 寫在這是方便你們找php
nginx的安裝配置三部曲:點擊便可查看css
一、安裝和配置文件的基礎配置,ctrl+f搜索nginxhtml
二、nginx文檔如何查看 + 經常使用的配置方式(僞靜態,反向代理,動靜分離,防盜鏈,圖片緩存,gzip圖片壓縮)linux
三、高級配置-》負載均衡nginx
---------------------web
Nginx從新讀取配置的命令swift
nginx -s reload
後端
gzip壓縮文件模塊的使用:緩存
參考:nginx官方文檔-》Modules reference-》ngx_http_gzip_moduletomcat
語法規則: location [=|~|~*|^~] /uri/ { … }
=
開頭表示精確匹配
^~
開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑便可。nginx不對url作編碼,所以請求爲/static/20%/aa,能夠被規則^~ /static/ /aa匹配到(注意是空格)。以xx開頭
~
開頭表示區分大小寫的正則匹配 以xx結尾
~*
開頭表示不區分大小寫的正則匹配 以xx結尾
!~
和!~*
分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則
/
通用匹配,任何請求都會匹配到。
多個location配置的狀況下匹配順序爲(參考資料而來,還未實際驗證,試試就知道了,沒必要拘泥,僅供參考):
首先精確匹配 =-》其次以xx開頭匹配^~-》而後是按文件中順序的正則匹配-》最後是交給 / 通用匹配。
當有匹配成功時候,中止匹配,按當前匹配規則處理請求。
例子,有以下匹配規則:
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/ { //以xx開頭 root /webroot/static/; } location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { //以xx結尾 root /webroot/res/; } #第三個規則就是通用規則,用來轉發動態請求到後端應用服務器 #非靜態文件請求就默認是動態請求,本身根據實際把握 location / { proxy_pass http://tomcat:8080/ }
nginx的其餘配置信息介紹
3、ReWrite語法
last
– 基本上都用這個Flag。break
– 停止Rewirte,不在繼續匹配redirect
– 返回臨時重定向的HTTP狀態302permanent
– 返回永久重定向的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
附:一些可用的全局變量
$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
一、普通的(靜態的)http服務器
這樣若是訪問http://localhost 就會默認訪問到E盤wwwroot目錄下面的index.html,若是一個網站只是靜態頁面的話,那麼就能夠經過這種方式來實現部署。
-
server {
-
listen
80;
-
server_name localhost;
-
client_max_body_size
1024M;
-
-
-
location / {
-
root e:wwwroot;
//思路:經過/將全部的請求,轉發給root處理
-
index index.html;
-
}
-
}
二、反向代理
localhost的時候,就至關於訪問localhost:8080了
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port; //思路:經過/,將全部的請求,轉發給第3方處理
}
}
既然服務器能夠直接HTTP訪問,爲何要在中間加上一個反向代理,不是畫蛇添足嗎?反向代理有什麼做用?
負載均衡、虛擬主機等,都基於反向代理實現,固然反向代理的功能也不只僅是這些。
三、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; } }
四、防盜鏈
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.ttlsa.com server_names ~\.google\. ~\.baidu\.;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.ttlsa.com/403.jpg;
}
}
五、根據文件類型設置過時時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { //只能是文件,由於這用-f判斷了 expires 1h; break; } }
六、設置圖片緩存(過時)時間
七、禁止訪問某個目錄
location ~* \.(txt|doc)${ root /data/www/wwwroot/linuxtone/test; #全部用戶都禁止訪問這個目錄 deny all; }
八、隱藏版本號的做用
經過你所用的版本,找其漏洞,進行攻擊你
在http中添加該配置:server_tokens off;
九、配置https
一、去阿里雲/騰訊雲申請免費的
二、下載證書
三、證書放到/usr/local/nginx目錄下(就是和conf同級,nginx.conf默認的配置文件的上一級)
四、在vhost目錄下加入配置文件
server {
listen 443;
server_name lampol.edu0532.cn; #改域名
ssl on;
root /home/www/xcxtp5/public; #改項目路徑
ssl_certificate ../certbo/1523694051089.pem; #改證書路徑
ssl_certificate_key ../certbo/1523694051089.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 index.php;
autoindex on;
# 僞靜態配置
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
十、動靜分離
思路:動、靜態的文件,請求時匹配不一樣的目錄
當訪問gif,jpeg時 直接訪問e:wwwroot;
,正則自行配置
server {
listen 80;
server_name localhost;
location / {
root e:wwwroot;
index index.html;
}
# 全部靜態請求都由nginx處理,存放目錄爲html
location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root e:wwwroot;
}
# 全部動態請求都轉發給tomcat處理
location ~ .(jsp|do)$ {
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { root e:wwwroot; } }
參考:nginx官方文檔-》Modules reference-》ngx_http_upstream_module