Nginx 是一個輕量級高性能的 Web 服務器, 併發處理能力強, 對資源消耗小, 不管是靜態服務器仍是小網站, Nginx 表現更加出色, 做爲 Apache 的補充和替代使用率愈來愈高.php
絕大多數的 Nginx 運行在 Linux 機器上, 雖然有 Windows 移植版, 但我也沒搭建過. 因此本文將以 Linux 爲例講解, 而 Mac OS 或其餘 Unix like 機器上的操做應該是同樣的.html
##增長 Nginx 虛擬主機nginx
這裏假設你們的 Nginx 服務器已經安裝好, 不懂的請閱讀各 Linux 發行版的官方文檔或者 LNMP 的安裝說明. 配置 Virtual host 步驟以下:服務器
進入 /usr/local/nginx/conf/vhost 目錄, 建立虛擬主機配置文件 demo.neoease.com.conf ({域名}.conf).併發
打開配置文件, 添加服務以下:wordpress
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; }
打開 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 範圍引入虛擬主機配置文件以下:高併發
include vhost/*.conf;
重啓 Nginx 服務, 執行如下語句.post
service nginx restart
##讓 Nginx 虛擬主機支持 PHP性能
在前面第 2 步的虛擬主機服務對應的目錄加入對 PHP 的支持, 這裏使用的是 FastCGI, 修改以下. server {
listen 80; server_name demo.neoease.com;
index index.html index.htm index.php;
root /var/www/demo_neoease_com;
location ~ .*.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php; i nclude fcgi.conf;
}
log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; }網站
##圖片防盜鏈 圖片做爲重要的耗流量大的靜態資源, 可能網站主並不但願其餘網站直接引用, Nginx 能夠經過 referer 來防止外站盜鏈圖片.
server {
listen 80;
server_name demo.neoease.com;
index index.html index.htm index.php;
root /var/www/demo_neoease_com; # 這裏爲圖片添加爲期 1 年的過時時間, 而且禁止 Google, 百度和本站以外的網站引用圖片
location ~ .*.(ico|jpg|jpeg|png|gif)$ { expires 1y;
valid_referers none blocked demo.neoease.com *.google.com *.baidu.com;
if ($invalid_referer) {
return 404;
}
}
log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; }
##WordPress 僞靜態配置
若是將 WordPress 的連接結構設定爲 /%postname%/, /%postname%.html 等格式時, 須要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建議, 但沒告知 Nginx 該如何修改. 咱們能夠將 WordPress 的虛擬主機配置修改以下:
server {
listen 80;
server_name demo.neoease.com;
index index.html index.htm index.php;
root /var/www/demo_neoease_com;
location / { if (-f $request_filename/index.html){ rewrite (.) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.) $1/index.php;
}
if (!-f $request_filename){
rewrite (.) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ ..(php|php5)?$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log
/var/log/demo.neoease.com.log demo.neoease.com; }
LNMP 套件在提供了 WordPress 爲靜態配置文件 /usr/local/nginx/conf/wordpress.conf, 在虛擬主機配置的 server 範圍引用以下便可.
include wordpress.conf;
若是你使用 LNMP 套件, 進入 WordPress 後臺發現會出現 404 頁面, wp-admin 後面缺乏了斜杆 /, 請在 wordpress.conf 最後添加如下語句:
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
相對 Apache, Nignx 有更增強大的併發能力, 而由於他對進程管理耗用資源也比較少. 而 Apache 比 Nginx 有更多更成熟的可用模塊, bug 也比較少. 賣主機的 IDC 選擇 Nignx, 由於高併發容許他們建立更多虛擬主機空間更來錢; 淘寶也所以改造 Nignx (Tengine) 做爲 CDN 服務器, 可承受更大壓力.