Nginx的2種用途
靜態內容的web服務器;
反向代理服務器;
Nginx作爲反向代理的特點
接收用戶請求是異步的,即先將用戶請求全部接收下來,再一次性發送後後端web服務器,極大的減輕後端web服務器的壓力;
nginx代理和後端web服務器間無需長連接;
發送響應報文時,是邊接收來自後端web服務器的數據,邊發送給客戶端的;
涉及的模塊
Proxy:標準的HTTP模塊,實現反向代理功能
Upstream:標準的HTTP模塊,對後端web服務器調度做負載均衡功能;
FastCGI:標準HTTP模塊,將php動態請求代理至後端PHP服務器;
配置部署
說明:本篇中,Nginx負責靜態訪問處理,動態訪問將會被代理至後端PHP服務器;
# vi /etc/nginx/nginx.html worker_processes 2; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; upstream backend { server 172.16.25.112:9000; } server { listen 80; server_name xxrenzhe.lnmmp.com; access_log/var/log/nginx/lnmmp.access.log; error_log/var/log/nginx/lnmmp.errors.log notice; root /www/lnmmp.com; location / { try_files $uri @missing; # 先訪問本地的靜態資源,若失敗,則轉入missing處理塊; } location @missing { rewrite ^(.*[^/])$ $1/ permanent; # 直接訪問域名或IP地址時,在其後增加結尾符/,並返回301 rewrite ^ /index.php last; # 將始終無法訪問到的資源(如404錯誤),全部重定向至首頁 } # 禁止system目錄訪問,但允許指定類型的靜態文件訪問 location ~* ^/system/.+\.(jpg|jpeg|png|gif|css|js|swf|flv|ico)$ { expires max; tcp_nodelay off; tcp_nopush on; } # 訪問/system/時,則直接跳轉回首頁 location ~ /system/ { rewrite ^ /index.php last; } location ~* \.php$ { default_type text/html; charset utf-8; fastcgi_pass backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
訪問驗證
靜態文件訪問由nginx直接返回
訪問動態文件時轉入後端php服務器
直接訪問域名或IP地址時跳轉至主頁
不存在文件訪問跳轉至主頁
訪問system目錄下的靜態文件正常
訪問system路徑跳轉至主頁
上一篇:如何測試Nginx的高性能