Nginx 配置
content_by_lua ‘’; set_by_lua $c "return ngx.var.a + ngx.var.b"; rewrite_by_lua "ngx.var.a = ngx.var.a + 1"; access_by_lua 'if ngx.var.remote_addr == "127.0.0.1" thenreturnendngx.exit(403)';
虛擬主機
server_name .a.org; #可以用正則表達式 *.a.org 跟a.org 可以合并為 .a.org 多個域名并排,空格分隔
顯示目錄或文件
autoindex on;
autoindex_exact_size on; #顯示文件大小
autoindex_localtime on; #顯示最后修改時間
?壓縮返回
gzip on;
gzip_types text/plain application/x-javascript text/css application/xml; #指定類型文件壓縮
gzip_min_length 1000;
gzip_buffers 4 8k;
默認讀取的文件
index index.php index.html index.htm;
設置網站的根目錄
root /var/nginx/avdata/public_html; #確保有目錄權限x
默認請求處理
location / { #默認的處理try_files $uri $uri/ index.php?$args;
}
默認處理的匹配方式 配置的$uri 即不包含參數以/開頭的請求地址
~ 區分大小寫正則匹配
~* 不區分大小寫正則匹配
^~ 如果匹配之后將停止往下檢查
= 完全相等
例:
location ~ \.php$ {#匹配.php的請求
?try_files $uri $uri/ index.php?$args;#$args顯示傳遞GET參數
?????? include fastcgi.conf;
?????? fastcgi_pass?? 127.0.0.1:9000;
?????? fastcgi_index? index.php; }
location = index.php {#匹配index.php ,非頁面上輸入index.php的請求.頁面上index.php的請求為/index.phpinclude fastcgi_params;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;#$fastcgi_script_name默認跟$uri相同fastcgi_index index.php;
}
靜態文件超時
location ~* \.(jpg|jpeg|gif|png|js|css)$ {#匹配指定結尾文件access_log off;#錯誤不記錄日志expires 30d;#客戶端緩存30天
} 通過規則:
{}里面的設置不影響外部的設置,
location / {root /var/nginx/a/public_html/hi; }
外部的設置影響{}里的設置,所以可以在虛擬主機的外部定義root,全局共用
IP監聽
listen 80; #IP:端口 或者直接端口,監聽全部IP 簡單重寫代替
# 最后一個是處理方式或者是處理地址,之前的為文件或目錄
# 找不到對應文件或目錄將用最后一個作為請求代替
# $uri/ 以/結尾表示目錄
try_files $uri $uri/ index.php;#文件或目錄找不到的時候用index.php代替$uri
別名
location /i/ {alias /good/; #但$uri為/i/*規則的時候,$uri將被修改為/good/* 不能用于正則表達式內的location }
禁止訪問
location ~ \.db$ {#符合.db 結尾的文件禁止訪問deny all;
} 指定IP訪問
location ~ \.db$ {#本機可訪問.db 結尾的文件allow 127.0.0.1;
} 其他權限:
location /hello {allow 127.0.0.1; deny all; echo "hello world"; }
?
傳輸速度限制
#超過傳輸指定文件后限制速度
limit_rate_after 1m;
limit_rate 100k;
404錯誤記錄
log_not_found off;# 404錯誤不記錄
?定義靜態404錯誤
location / {//找不到文件定義狀態嗎try_files $uri $uri/ =404; } location ~ \.php$ {fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_intercept_errors on;//讓錯誤顯示到前端fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 404 /404.html;//自定義404
規則:
~ 為區分大小寫匹配
~* 為不區分大小寫匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
#ReWrite語法
last - 基本上都用這個Flag。
break - 中止Rewirte,不在繼續匹配
redirect - 返回臨時重定向的HTTP狀態302
permanent - 返回永久重定向的HTTP狀態301 ?
一些核心及內置變量:http://nginx.org/cn/docs/http/ngx_http_core_module.html
附屬:
一般Nginx 變量值容器的生命期是與當前請求相關聯的,有特殊的。
變量是聲明的,非過程的
設置變量
set $a aaa; 輸出變量
echo $a;
echo "a $a;" 正常下,$都將被轉義 所以 通過以下辦法定義有$字符的變量
geo $do { default "$"; }
執行某個地址:
echo_exec /bar;
echo_location /bar "a=1&b=2";
獲得參數:
$arg_*
輸出指令不能有多條
配合echo的前后
echo_before_body "before..."; echo_after_body "after...";
處理順序:
rewrite access content (echo proxy_pass 等) index #(地址不為/棄權處理) autoindex #(地址不為/棄權處理) ngx_static find-config 。。。 try-files /file1 /file2 =403 #(挨個嘗試文件是否存在)
LUA 調用:
content_by_lua ‘ ngx.exit(403)’; set_by_lua $c "return ngx.var.a + ngx.var.b"; rewrite_by_lua "ngx.var.a = ngx.var.a + 1"; access_by_lua 'if ngx.var.remote_addr == "127.0.0.1" thenreturnendngx.exit(403)';
示例:
server {listen 80; server_name xx.com www.xx.com; gzip on; gzip_types text/plain application/x-javascript text/css application/xml; gzip_min_length 1000; gzip_buffers 4 8k; root /var/www/xx/public_html/; error_page 500 502 503 504 /50x.html; location = /50x.html {root html; }location / {index index.php index.html index.htm; try_files $uri $uri/ index.php?$args; }location ~ \.(ico|gif|bmp|jpg|jpeg|png|swf|js|css) {expires 7d; try_files $uri $uri/ index.php?$args; }location ~ \.php$ {fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param KOHANA_ENV "TESTING"; fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/"; include fastcgi_params; }location = index.php {include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root/index.php; fastcgi_param KOHANA_ENV "TESTING"; fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/"; fastcgi_index index.php; }}
一般情況下記得把PHP某些函數禁用
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,parse_ini_file,show_source,dl
disable_classes = perl
轉載于:https://www.cnblogs.com/liushannet/archive/2013/01/07/2849585.html
總結
- 上一篇: 黄山风景区换乘中心可以寄存行李吗
- 下一篇: JavaScript 关闭窗口事件