結構上:
核心模塊:HTTP模塊、EVENT模塊、MAIL模塊。 基礎模塊:HTTP access模塊、HTTP FastCGI模塊、HTTP Proxy模塊、HTTP Rewrite模塊。 第三方模塊:HTTP Upstream Request Hash模塊。
功能上:
Handlers:處理請求,進行輸出內容和修改headers信息等。 Filters:主要對其他處理模塊輸出的內容進行修改操作。 Proxies:Nginx的HTTP Upstream之類的模塊,與后端服務如fastcgi等進行交互。
工作模式:
單工作進程 多工作進程
Nginx與Apache最大區別:
Nginx的模塊直接編譯進Nginx,屬于靜態編譯方式,啟動Nginx自動加載。 Apache將模塊編譯成一個so文件,需要在配置文件中指定是否加載。
靜態文件處理:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root /web/wwwroot/; expires 30d; -----指定靜態文件過期時間 }
配置獲取nginx的運行狀態:
location /NginxStatus { stub_status on; ---啟用StubStatus的工作狀態統計功能 access_log logs/NginxStatus.log; ---指定訪問日志文件 auth_basic "NginxStatus"; ---Nginx的一種認證機制 auth_basic_user_file ../htpasswd; ---指定認證密碼文件 } # /usr/local/apache/bin/htpasswd -c /usr/local/nginx/conf/htpasswd webadmin Nginx信息: Active connection: 1 ----當前活躍的鏈接數 server accepts handled reqests 393411 393411 393799 ---總共處理了393411個連接,創建393411次握手,處理393799個請求 reading: 0 Writiong: 1 Waiting: 0
Nginx啟動、關閉重啟
QUIT:處理完當前請求后關閉進程。 HUP:重新加載配置沒平滑重啟Nginx。 USR1:Nginx日志切換,重親打開一個日志文件。 USR2:平滑升級執行。 WINCH:從容關閉工作進程。
nginx反向代理根問題測試:
訪問192.168.8.104: 192.168.8.103目錄test下有index.html測試用 location / { proxy_pass http://192.168.8.103/test; } 出現301 location / { proxy_pass http://192.168.8.103/test/; } 正常訪問 訪問192.168.8.104/test/test.html: location /test { proxy_pass http://192.168.8.103/app; } 訪問到/app/test.html location /test/ { proxy_pass http://192.168.8.103/app; } 訪問到/apptest.html location /test { proxy_pass http://192.168.8.103/app/; } 訪問到/app//test.html location /test/ { proxy_pass http://192.168.8.103/app/; } 訪問到/app/test.html location /test { proxy_pass http://192.168.8.103; } 訪問到/test/test.html location /test { proxy_pass http://192.168.8.103/; } 訪問到//test.html location /test/ { proxy_pass http://192.168.8.103; } 訪問到/test/test.html location /test/ { proxy_pass http://192.168.8.103/; } 訪問到/test.html 總結 如果location路徑是根,那么proxy_pass 路徑必須加根。 如果proxy_pass 沒有路徑, location路徑會被完整的傳遞下來。 如果proxy_pass有路徑,location路徑排除匹配所用的部分會完整的傳遞下來。
別名:
location /i { alias /var/www/html/imags/; } url請求 /i/logo.gif 服務器會查找/var/www/html/imags/logo.gif 如果是root 會查找/var/www/thml/imags/i/logo.gif location ~ ^/download/(.*)$ { alias /home/webdata/www/$1; } url請求 /download/ebook.tar.gz ,Nginx服務器會請求 /home/webdata/www/ebook.tar.gz 文件 如果是root 會查找/home/webdata/www/ebook.tar.gz/download/ebook.tar.gz
rewrite:
語法: rewrite regex flag 默認:none 使用字段:server 、 location 、 if flag種類: last:rewrite之后搜索相應的URI或location break:終止匹配,不在匹配后面規則 redirect:返回302,游覽器顯式跳轉后的地址 permanent:返回301,游覽器顯式跳轉后的地址 重定向實現域名過度: server { server_name com; if ($host != ' { rewrite ^/(.*)$ permanent; } }
緩存:
proxy_cache_path /backup/proxy_cache_dir levels=1:2 keys_zone=cache_one:4096m inactive=1d max_size=3g; proxy_temp_path /backup/proxy_temp_dir; 需跟proxy_cache_path分在一個磁盤
location中配置 proxy_cache cache_one; proxy_cache_valid 200 304 12h proxy_cache_key $host$url$is_args$args;
清楚緩存:
location ~ /purge(/.*) { allow 127.0.0.1; allow 192.168.88.0/24 deny all; proxy_cache_purge cache_one $host$1$is_args$args } 的緩存可以通過訪問
清除
原創文章,作者:心肝壞了,如若轉載,請注明出處:http://www.www58058.com/47953