Nginx的2種用途
靜態內容的web伺服器;
反向 Proxy伺服器;
Nginx作為反向 Proxy的特點
接收使用者請求是非同步,即先將使用者請求全部接收下來,再一次性發送後後端web伺服器,極大的減輕後端web伺服器的壓力;
nginx代理和後端web伺服器間無需長串連;
發送響應報文時,是邊接收來自後端web伺服器的資料,邊發送給用戶端的;
涉及的模組
Proxy:標準的HTTP模組,實現反向 Proxy功能
Upstream:標準的HTTP模組,對後端web伺服器調度做負載平衡功能;
FastCGI:標準HTTP模組,將php動態請求代理至後端PHP伺服器;
配置部署
說明:本篇中,Nginx負責靜態訪問處理,動態訪問將會被代理至後端PHP伺服器;
# vi /etc/nginx/nginx.htmlworker_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; } }}