server { listen 80; server_name localhost; index index.html index.htm index.php; root /data/wwwroot; location /wordpress{ <span style="color:#ff0000;"><strong><em>try_files $uri $uri/ /wordpress/index.php?$args;</em></strong></span> } location ~ .*\.(php|php7)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #偽靜態設置路徑 和日志文件路徑 #include /etc/nginx/rewrite/default.conf; access_log /data/wwwlogs/wordpress_access.log; error_log /data/wwwlogs/wordpress_error.log; }
根據上面的標准紅色的字體“try_files $uri $uri/ /wordpress/index.php?$args;”為例,我們做如下說明:
當用戶請求 http://blog.csdn.net/example 時,這里的 $uri 就是 /example。try_files 會到硬盤里嘗試找這個文件。
如果存在名為 /$root/example(其中 $root 是 WordPress 的安裝目錄)的文件,就直接把這個文件的內容發送給用戶。
如果不存在名為 叫 example 的文件。然后就看 $uri/,增加了一個 /,也就是看有沒有名為 /$root/example/ 的目錄。又找不到,就會 fall back 到 try_files 的最后一個選項 /index.php,發起一個內部 “子請求”,也就是相當於 nginx 發起一個 HTTP 請求到 http://blog.csdn.net/index.php。這個請求會被 location ~ \.php$ { ... } catch 住,也就是進入 FastCGI 的處理程序。而具體的 URI 及參數是在 REQUEST_URI 中傳遞給 FastCGI 和 WordPress 程序的,因此不受 URI 變化的影響。
